微信小程序之多选功能


思路:把向得到的数组中添加一个布尔值,默认都为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>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM