微信小程序全选多选效果
效果图:
wxml代码:
1 <view class="memberList"> 2 <view class="allSelect_box" bindtap='selectAll'> 3 <checkbox class="checkbox" checked="{{selectilall}}"/> 4 <text class="allSelect">全选</text> 5 </view> 6 <view wx:for="{{list}}"> 7 <view class="member_box" data-index="{{index}}" data-name="{{item.id}}" bindtap='select'> 8 <checkbox class="checkbox" checked="{{item.checked}}"/> 9 <image src="../../assets/img/user_img.jpg" mode="widthFix" class="avatar"></image> 10 <view> 11 <view class="applicant_name">汪小涵 <text>管理员</text> </view> 12 <view class="approval_state">总监</view> 13 </view> 14 </view> 15 </view> 16 </view>
wxss代码:
1 .memberList{ 2 background:#fff; 3 padding:0 35rpx 0 28rpx; 4 } 5 .allSelect_box{ 6 line-height:90rpx; 7 border-bottom:1rpx solid #F5F5F5; 8 } 9 .checkbox{ 10 transform: scale(0.7,0.7); 11 } 12 .member_box .checkbox{ 13 margin-top: 15rpx; 14 } 15 checkbox .wx-checkbox-input.wx-checkbox-input-checked { 16 color:#fff; 17 background: #6669e3; 18 border-color:#6669e3; 19 } 20 .allSelect{ 21 margin-left:15rpx; 22 vertical-align:middle; 23 } 24 .member_box{ 25 padding-bottom:30rpx; 26 display: flex; 27 border-bottom:2rpx solid #EBEBEB; 28 padding:20rpx 0 20rpx 0; 29 } 30 .avatar{ 31 width:100rpx; 32 border-radius:50%; 33 margin-left:15rpx; 34 } 35 .applicant_name{ 36 font-size:34rpx; 37 padding:0 0 15rpx 30rpx; 38 } 39 .applicant_name text{ 40 color:#fff; 41 background:#6669e3; 42 font-size:28rpx; 43 padding:5rpx 10rpx; 44 border-radius:10rpx; 45 margin-left:15rpx; 46 } 47 .approval_state{ 48 color:#8D8D8D; 49 font-size:28rpx; 50 padding-left: 30rpx; 51 }
js代码:
1 data: { 2 list: [ 3 { id: 1, name: 1, checked: false }, 4 { id: 2, name: 2, checked: false }, 5 { id: 3, name: 3, checked: false }, 6 { id: 4, name: 4, checked: false }, 7 ], 8 selectilall: false 9 },
//单选 select: function (e) { let selectValue = e.currentTarget.dataset.name let index = e.currentTarget.dataset.index; let list = this.data.list let newli = 'list[' + index + '].checked'; this.setData({ [newli]: !this.data.list[index].checked }) let num = 0; for(var i=0;i<this.data.list.length;i++){ if(this.data.list[i].checked){ num++; } } if(num == this.data.list.length){ this.setData({ selectilall: true }) }else{ this.setData({ selectilall: false }) } }, //全选,取消全选 selectAll: function (e) { let list = this.data.list; let selectilall = this.data.selectilall; if (selectilall == false) { for (let i = 0; i < list.length; i++) { let newli = 'list[' + i + '].checked'; this.setData({ [newli]: true, selectilall: true }) } } else { for (let i = 0; i < list.length; i++) { let newli = 'list[' + i + '].checked'; this.setData({ [newli]: false, selectilall: false }) } } }