第一种:自定义多选
html:
<block wx:for="{{weekList}}" wx:key="index">
<text class="item {{item.selectedW?'active':''}}" bindtap="clickWeenk" data-index="{{index}}">{{item.name}}</text>
</block>
样式大家就根据需要自定义就好了,js中的data:
weekList: [{ name: '日' }, { name: '一' }, { name: '二' }, { name: '三' }, { name: '四' }, { name: '五' }, { name: '六' },],
weekArr:[],//用来装多选的星期值
js点击方法:
clickWeenk: function(e) {
var t = this;
var id = e.currentTarget.dataset.index
**//点击获取到点击的这条数据,通过给其加上一个selectedW属性来切换点中或是点不中的状态**
let sele = t.data.weekList[id].selectedW
let sel = 'weekList['+id +'].selectedW'
t.setData({
[sel]: !sele
})
let name = '周' +t.data.weekList[id].name
console.log(name)
**//在weekArr数组中寻找点击的值,找得到splice就删除,找不到就push添加进去**
if (t.data.weekArr.indexOf(name) == -1) {
t.data.weekArr.push(name)
} else {
t.data.weekArr.splice(t.data.weekArr.indexOf(name), 1)
}
console.log(t.data.weekArr)
},
第二种:checkbox实现全选
wxml:
<checkbox-group bindchange="checkboxChange">
<view class=" chexkItembox">
<view wx:for="{{mlist1}}" wx:key="index" class="chexkItem">
<checkbox value="{{item.id}}" checked="{{item.checked}}" />
<view class="billuser_le">
<view class="imgbox">
<image src="{{item.user.wxuser.headimgurl}}"></image>
</view>
</view>
</view>
</view>
</checkbox-group>
<!-- 全选 -->
<view>
<checkbox bindtap="selectAll" checked="{{select_all}}" />
<text>全选</text>
</view>
<!-- end -->
hs:
data:
select_all: false,//全选
batchIds: '', //选中的ids
方法:
// 单选
checkboxChange(e) {
var batchIds = e.detail.value;
var t = this;
t.setData({
batchIds: batchIds //单个选中的值
})
var list = t.data.mlist1;
for(var i = list.length-1; i>=0; i--){
if (batchIds.indexOf(list[i].id.toString())!=-1){
list[i].checked = true;
}else{
list[i].checked = false;
}
}
var select_all = t.data.batchIds.length == 0 || t.data.batchIds.length != t.data.mlist1.length ? false : true;
t.setData({
select_all: select_all,
mlist1: list,
})
},
// 全选
selectAll: function (e) {
var that = this;
var arr = []; //存放选中id的数组
for (let i = 0; i < that.data.mlist1.length; i++) {
that.data.mlist1[i].checked = (!that.data.select_all)
if (that.data.mlist1[i].checked == true) {
// 全选获取选中的值
arr.push(that.data.mlist1[i].id)
// arr = arr.concat(that.data.mlist[i].id.split(','));
}
}
that.setData({
mlist1: that.data.mlist1,
select_all: (!that.data.select_all),
batchIds: arr
})
console.log(that.data.batchIds)
var ids = that.data.batchIds.join(",");
},