element-ui 里面el-checkbox多选框,实现全选单选

data里面定义了

       data:[],
        actionids:[],//选择的那个actionid
        num1:0,//没选择的计数
        num2:0,//选中的计数
        checkAllBig: [],
        checkAll:[],
        checkOne:[],
 获取所有数据:

                      this.data=res.data;
                     for(var i=0;i<this.data.length;i++){//遍历选择的内容
                         if(this.data[i].ischecked==1){
                                 this.checkAllBig.push(this.data[i].affairtypeid)
                             }
                         for(var j=0;j<this.data[i].eventtype.length;j++){
                             if(this.data[i].eventtype[j].ischecked==1){
                                 this.checkAll.push(this.data[i].eventtype[j].eventtypeid)
                             }
                             for(var k=0;k<this.data[i].eventtype[j].operation.length;k++){
                                 if(this.data[i].eventtype[j].operation[k].ischecked==1){
                                 this.checkOne.push(this.data[i].eventtype[j].operation[k].actionid);}
                             }
                         }
                     }
         

<div class="checkbox-table" v-for="(item,indexkey) in data">
                            <template>
        <el-checkbox  class="check1" v-model="checkAllBig"  :label="item.affairtypeid" @change="handleCheckedCitiesChange(1,indexkey)">{{item.affairtypename}}{{item.ischecked}}</el-checkbox>
                                 
                                 <div v-for="(list,index2) in item.eventtype" class="line-check" :key="list.eventtypeid">
                                      <el-checkbox   class="check2" @change="handleCheckedCitiesChange(2,indexkey,index2)"  v-model="checkAll" :label="list.eventtypeid"  v-bind:style="{ 'float':list.operation.length>0?'left':'none'}">{{list.eventtypename}}{{list.ischecked}}</el-checkbox>
                                      <el-checkbox-group v-model="checkOne" class="checkGroup" >
                                        <el-checkbox  v-for="(operate,index1) in list.operation" :label="operate.actionid"   :key="operate.actionid"  @change="handleCheckedCitiesChange(3,indexkey,index2,index1)">{{operate.actionname}}{{operate.ischecked}}</el-checkbox>
                                      </el-checkbox-group>
                                  </div>
                            </template>
                            </div>

handleCheckedCitiesChange(type,a=0,b=0,c=0) {//多选框
if(type==1){//最高级全选
this.actionids=[];
if(this.data[a].ischecked==1){
this.data[a].ischecked=0;
for(var i=0;i<this.data[a].eventtype.length;i++){
this.checkAll=this.checkAll.map(res=>{
if(res!=this.data[a].eventtype[i].eventtypeid){return res}
});
for(var j=0;j<this.data[a].eventtype[i].operation.length;j++){
this.checkOne=this.checkOne.map(res=>{
if(res!=this.data[a].eventtype[i].operation[j].actionid){return res;}
});
}
}
}else{
this.data[a].ischecked=1;
for(var i=0;i<this.data[a].eventtype.length;i++){
this.checkAll.push(this.data[a].eventtype[i].eventtypeid);
for(var j=0;j<this.data[a].eventtype[i].operation.length;j++){
this.checkOne.push(this.data[a].eventtype[i].operation[j].actionid);
this.actionids.push(this.data[a].eventtype[i].operation[j].actionid)
}
}

}

this.api(this.data[a].ischecked);//调用接口把选择的传过去


}else if(type ==2){//第二级全选
this.actionids=[];
this.data[a].eventtype[b].ischecked=this.data[a].eventtype[b].ischecked==1?0:1;

for(var i=0;i<this.data[a].eventtype[b].operation.length;i++){
if(this.data[a].eventtype[b].ischecked==1){
this.checkOne.push(this.data[a].eventtype[b].operation[i].actionid);
this.$set(this.data[a].eventtype[b].operation[i],"ischecked",1);
} else{
this.$set( this.data[a].eventtype[b].operation[i],"ischecked",0);
this.checkOne=this.checkOne.map(res=>{
if( res!=this.data[a].eventtype[b].operation[i].actionid){return res;}
})
}

this.actionids.push(this.data[a].eventtype[b].operation[i].actionid);

}

this.api(this.data[a].eventtype[b].ischecked);

}else{//单选
var num1=0;
var num2=0;
var len=this.data[a].eventtype[b].operation.length;//单选框长度

this.data[a].eventtype[b].operation[c].ischecked=this.data[a].eventtype[b].operation[c].ischecked==1?0:1;
for(var i=0;i<len;i++){
if(this.data[a].eventtype[b].operation[i].ischecked==1){
num2++;//选中计数
}
else{
num1++;//没选计数
}
}

if(num1==len){
this.checkAll=this.checkAll.map(res=>{
if(res!=this.data[a].eventtype[b].eventtypeid){return res;}
});
this.data[a].eventtype[b].ischecked=0;
}
if(num2==len){
this.checkAll.push(this.data[a].eventtype[b].eventtypeid)
this.data[a].eventtype[b].ischecked=1;
this.$set(this.data[a].eventtype[b],"ischecked",1);
}


this.actionids=this.data[a].eventtype[b].operation[c].actionid;

this.api(this.data[a].eventtype[b].operation[c].ischecked)
}

api(ischecked){//权限选择接口
let para={
roleid:this.roleid,
ischecked:ischecked,
actionid:this.actionids.toString(),
}
addRolePower(para).then((res)=>{//取消权限
this.$notify({
message:res.msg,
type: 'success'
});
})
},

引用自夏天想

 

转载于:https://www.cnblogs.com/zhouyideboke/p/10097992.html

当使用 ElementUI 的 Table 组件进行多选时,可以通过配置 `show-selection` 和 `selection-type` 属性来实现多选功能。 其中,`show-selection` 属性用于控制是否显示选择列,如果设置为 `false` 则不会显示选择列,如果设置为 `true` 则会显示选择列。 `selection-type` 属性用于指定选择类型,可以设置为 `'checkbox'` 或 `'radio'`。当设置为 `'checkbox'` 时,会显示复选框,允许用户选择多行数据,当设置为 `'radio'` 时,会显示单选框,只允许用户选择一行数据。 在选中数据后,可以通过 `@selection-change` 事件获取选中的数据,或者通过 `this.$refs.table.getSelectedRows()` 方法获取选中的数据。另外还可以通过 `@select-all` 和 `@select` 事件监听全选单选操作。 示例代码如下: ```html <template> <div> <el-table :data="tableData" :show-selection="true" :selection-type="'checkbox'" @selection-change="handleSelectionChange"> <el-table-column type="selection"></el-table-column> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="gender" label="性别"></el-table-column> </el-table> </div> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 18, gender: '男' }, { name: '李四', age: 20, gender: '女' }, { name: '王五', age: 22, gender: '男' }, { name: '赵六', age: 24, gender: '女' } ] } }, methods: { handleSelectionChange(rows) { console.log(rows) // 获取选中的数据 } } } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值