微信小程序之多选功能

思路:把向得到的数组中添加一个布尔值,默认都为false,然后通过数组的映射功能把选中的布尔值,存储到数组中,在组件属性中,用三元运算符做判断即可

data:{

sampleArray: [{ id: '0001'}, { id: '0002'}, { id: '0003'}, { id: '0004'}], //样品数组

}

js代码:

//页面加载的时候执行

onLoad(){
let {sampleArray } = this.data;
sampleArray.forEach(x => {
x.selected = false
})
},
//选中数据是执行
select(e){
let that=this;
let { index}=e.currentTarget.dataset;
let currentItem = this.data.sampleArray[index];
let { selectSample}=this.data;
currentItem.select = !currentItem.select
selectSample.concat(selectSample.push(index));
let newArray=selectSample.filter((val,index,arr)=>{
return arr.indexOf(val)==index
})
console.log(newArray)
this.setData({
sampleArray: that.data.sampleArray,
selectSample
})
},
wxml代码:
<image class="select" src="{{item.select?'/images/zx1.png':'/images/xz2.png'}}"></image>
posted @ 2019-09-05 11:38  KG-work-space  阅读(5513)  评论(1编辑  收藏  举报