当前位置:网站首页>微信小程序tab切换时保存checkbox状态
微信小程序tab切换时保存checkbox状态
2022-08-10 00:13:00 【Colesyn L.】
微信小程序tab切换时保存checkbox状态
直接上代码
javascript
//先处理后端返回的数据
//因为我这里是两层所以要 i.list 一层可以直接循环添加checked
let arr = data.data.data;
arr.forEach(i => {
i.show = false;
i.list.forEach(v => {
v.checked = false
})
})
arr[0].show = true;
this.setData({
list: arr })
//切换函数
bannav(e) {
let navid = e.currentTarget.dataset.id;
this.setData({
navid })
this.getData();
//主要是这里
//获取选中的id 和 已有的数据进行比较 然后 将checkd 重新赋值
setTimeout(() => {
this.data.list.forEach(i => {
i.list.forEach(v => {
this.data.selectIds.forEach(t => {
if (v.id == t) {
v.checked = true;
}
})
})
})
this.setData({
list: this.data.list })
}, 500)
},
//复选框选中状态
checkboxChange(e) {
let bigindex = e.target.dataset.bigindex;
let smallIndex = e.target.dataset.smallindex;
const items = this.data.list[bigindex].list;
const values = e.detail.value;
let carr = this.data.selectIds
//选中
if (values.length) {
let cid = items.findIndex(v => v.id == values[0]);
let T = `list[${
bigindex}].list[${
cid}].checked`;
this.setData({
[T]: !this.data.list[bigindex].list[smallIndex].checked
})
carr.push(...values)
this.setData({
selectIds: carr })
} else {
//取消选中
let T = `list[${
bigindex}].list[${
smallIndex}].checked`;
this.setData({
[T]: false
})
let ids = [this.data.list[bigindex].list[smallIndex].id.toString()]
let lists = carr.filter(items => {
if (!ids.includes(items)) return items
})
this.setData({
selectIds: lists })
}
},
html
//第一层
<view class="list" wx:for="{
{list}}" wx:key="index">
<view class="list-title" bindtap="go_view" data-name="{
{item.name}}">
<view>{
{
item.name}}</view>
<image src="../../images/down.png"></image>
</view>
<block wx:if="{
{item.show}}">
//第二层
<view class="list-item" wx:for="{
{item.list}}" wx:key="index" wx:for-index="childindex">
<checkbox-group data-bigindex="{
{index}}" data-smallIndex="{
{childindex}}" bindchange="checkboxChange" style="display: flex;">
<label class="checkbox">
<checkbox value="{
{item.id}}" checked="{
{item.checked}}" />
</label>
<view class="checkbox-items">{
{
item.content}}</view>
</checkbox-group>
</view>
</block>
</view>
边栏推荐
- C language pointer practice questions
- R语言使用coxph函数构建生存分析回归模型,使用forestmodel包的forest_model函数可视化生存回归模型对应的森林图
- What do you know about FITC-labeled biotin (FITC-biotin|CAS: 134759-22-1)?
- 宽带由20M换为100M
- 【CAS:41994-02-9 |Biotinyl tyramide】Biotinyl tyramide price
- Character Statistics Histogram
- 【毕业设计】基于ESP32的在线墨水屏桌面摆件 -物联网 单片机 嵌入式
- 【Django】缓存
- 深入理解Aarch64内存管理
- Win11怎么关闭系统保护功能?系统保护还原功能怎么关闭?
猜你喜欢

将string类对象中的内容格式化到字符串Buffer中时遇到的异常崩溃分析

03|Process Control
![[C language] Address book](/img/56/a72900c22b965947ee88256d8f6c21.jpg)
[C language] Address book "Static Memory Version"

游泳馆系统次卡的设置有哪些细节?

《痞子衡嵌入式半月刊》 第 60 期

365 days challenge LeetCode1000 questions - Day 052 Step by step summation to get the minimum value of positive numbers Greedy

使用C语言实现静态链表

基于FPGA的任意字节数的串口接收(含源码工程)

2022中高级Android面试题汇总来助你通过面试

XSS高级 svg 复现一个循环问题以及两个循环问题
随机推荐
@PostConsturct注解作用及特点
将string类对象中的内容格式化到字符串Buffer中时遇到的异常崩溃分析
How to turn off system protection in Win11?How to turn off the system protection restore function?
-象棋比赛-
Stanford CS143 Speed Pass PA1 Tutorial
-red and black-
R语言使用glm函数构建logistic回归模型,使用forestmodel包的forest_model函数可视化逻辑回归模型对应的森林图
什么是持续测试?
3.1 - 程序设计语言 3.2 - 高级语言的特点及引用 3.3 - 静态/动态类型语言
Are the numbers entered symmetrical?
删除表空间数据文件
Leetcode82. 删除排序链表中的重复元素 II
CMake 编译运行dpdk项目程序
渗透测试与攻防对抗——漏洞扫描&逻辑漏洞(Part1)
What should I do if there is no sound after reinstalling the system in win10?
GBJ1510-ASEMI机器人电源整流桥GBJ1510
flask——请求、响应、请求扩展、session、闪现、蓝图、g对象、flask-session
Tensor flow 踩坑记
收银管理软件如何做好员工管理?
Leetcode83. 删除排序链表中的重复元素