微信小程序获取复选框全选,反选选中的值

wxml文件 <view class="tr"> <view class="th"> <checkbox bindtap="selectall" />全选 </view> <view class="th">id</view>...

wxml文件

<view class="tr">
    <view class="th">
      <checkbox  bindtap="selectall" />全选
    </view>
      <view class="th">id</view>
      <view class="th">名称</view>
</view>

<checkbox-group bindchange="checkboxChange">
    <view class="tr" wx:for="{{listData}}" wx:key="">
        <view class="td">
        <checkbox value="{{item.code}}" checked="{{item.checked}}" />
        </view>
        <view class="td" value="{{item.text}}">{{item.code}}</view>
        <view class="td" value="{{item.text}}">{{item.text}}</view>
    </view>
</checkbox-group>

wxss文件

.table{
background-color: #fff;
border:1px solid #dadada;
width:1200rpx;
margin-left:0rpx;
}

.tr{
background-color: #dadada;
white-space: nowrap;
width:100%;
display: flex;
text-align: center;
justify-content: center;
}

.th{
background-color: #fff999;
text-align: center;
justify-content: center;
width: 100%;
border-bottom: 1px solid #dadada;
border-right: 1px solid #dadada;
}

.td{
background-color: #fff;
text-align: center;
justify-content: center;
width: 100%;
border-bottom: 1px solid #dadada;
border-right: 1px solid #dadada;

}

js文件

Page({
  data: {
    select_all: false,
    listData: [
      { code: "1", text: "测试1" },
      { code: "2", text: "测试2"},
      { code: "3", text: "测试3"}
    ],
    batchIds: '',    //选中的ids
  },

//全选与反全选
selectall: function (e) {
  console.log(e)
    var that = this;
    var arr = [];   //存放选中id的数组
    for (let i = 0; i < that.data.listData.length; i++) {

      that.data.listData[i].checked = (!that.data.select_all)

      if (that.data.listData[i].checked == true){
        // 全选获取选中的值
        arr = arr.concat(that.data.listData[i].code.split(','));
      }
    }
  console.log(arr)
    that.setData({
      listData: that.data.listData,
      select_all: (!that.data.select_all),
      batchIds:arr
    })
  },

  // 单选
  checkboxChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      batchIds: e.detail.value  //单个选中的值
    })
  },
})

attachments-2020-07-cUNj0cXr5f210c929ae30.png

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
王凯
王凯

92 篇文章

作家榜 »

  1. admin 651 文章
  2. 粪斗 185 文章
  3. 王凯 92 文章
  4. 廖雪 78 文章
  5. 牟雪峰 12 文章
  6. 李沁雪 9 文章
  7. 全易 2 文章
  8. Stevengring 0 文章