微信小程序实现全选,半选功能

isSelected: false

},

]

},

{

id: 3,

name: ‘地点’,

isSelected: false,

arr: [{

id: 31,

name: ‘北京’,

isSelected: false

},

{

id: 32,

name: ‘上海’,

isSelected: false

},

{

id: 33,

name: ‘杭州’,

isSelected: false

},

]

},

]

//渲染数据时 判断是否半选

list.forEach(v => {

let flag = true

v.arr.forEach(arrItem => {

flag = arrItem.isSelected ? false : flag

})

if (!flag && !v.isSelected) {

checkedList.push(v.id)

}

})

this.setData({

list,

checkedList

})

},

//点击 全选按钮

selectAll(e) {

let {

list,

checkedList

} = this.data

list.forEach(v => {

if (v.id === e.currentTarget.dataset.state.id) {

v.isSelected = !v.isSelected

if (!v.isSelected) {

checkedList.includes(v.id) ? checkedList=checkedList.filter(check => check != v.id) : “”

}

v.arr.forEach(e => {

e.isSelected = v.isSelected

})

}

})

this.setData({

list,

checkedList

})

},

//checkbox-group 事件 被选中checkbox value 的数据 组成的数组

change(e) {

let id = e.currentTarget.dataset.id

let value = e.detail.value

let list = this.data.list

let checkedList = this.data.checkedList

console.log(checkedList)

list.forEach(v => {

if (v.id === id) {

//设置 全选按钮

v.isSelected = v.arr.length === value.length ? true : false

if (v.isSelected) {

checkedList.includes(id) ? checkedList=checkedList.filter(v => v != id) : ‘’

} else if (!v.isSelected && value.length) {

checkedList.includes(id) ? ‘’ : checkedList.push(id)

} else if (!v.isSelected && !value.length) {

checkedList.includes(id) ? checkedList=checkedList.filter(v => v != id) : ‘’

}

//设置 单个按钮

v.arr.filter(arrItem => arrItem.isSelected = false)

v.arr.forEach(arrItem => {

value.forEach(val => {

arrItem.isSelected = arrItem.id == val ? true : arrItem.isSelected

})

})

}

})

this.setData({

list,

checkedList

})

}

})

  • index.wxml

微信小程序实现全选,半选

{{item.name}}

{{func.name}}

  • index.wxss

.content{

padding: 20rpx;

}

.title{

width: 100%;

text-align: center;

}

.allBtn{

margin-bottom: 20rpx;

}

.half .wx-checkbox-input::before {

content: ‘’;

/CSS伪类用法/

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值