el-table 树形表格+多选

el-table 树形表格+多选

参考文章:https://blog.csdn.net/Yzt_199626/article/details/117171822

在这里插入图片描述

 <el-table
    ref="ruletable"
    :data="ruleData"
    style="width: 100%"
    row-key="deptId"
    :default-expand-all="true"
    :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    @selection-change="handleSelectionChange"
    @select-all="handleSelectAll"
    @select="handleRowSelect"
></el-table>
 handleEcho(rows) {
      this.oldRuleData.filter((item) => {
        for (let i = 0; i < rows.length; i++) {
          if (rows[i].deptId == item.deptId) {
            this.$refs.ruletable.toggleRowSelection(item, true);
          }
        }
      });
    },
    /* 树形数据 */
    // // 规则适用范围列表
    getRuleList() {
      this.oldRuleData = this.ruleData;
      this.ruleData = this.handleTree(this.ruleData, "deptId");
    },
    setChildren(children, type) {
      // 编辑多个子层级
      children.map((j) => {
        this.toggleSelection(j, type);
        if (j.children) {
          this.setChildren(j.children, type);
        }
      });
    },
    toggleSelection(row, select) {
      if (row) {
        this.$nextTick(() => {
          this.$refs.ruletable &&
            this.$refs.ruletable.toggleRowSelection(row, select);
        });
      }
    },
    // 规则适用范围多选
    handleSelectionChange(selection) {
      let arr = selection;
      let parArr = [];
      for (let item of arr) {
        let { children, ...params } = item;
        parArr.push(params);
      }
      this.selection = parArr;
    },
    // 规则适用范围勾选数据行
    handleRowSelect(selection, row) {
      console.log(selection, row);
      const hasSelect = selection.some((el) => {
        return row.deptId === el.deptId;
      });
      if (hasSelect) {
        if (row.children) {
          // 解决子组件没有被勾选到
          this.setChildren(row.children, true);
        }
      } else {
        if (row.children) {
          this.setChildren(row.children, false);
        }
      }
    },
    // 规则适用范围全选
    handleSelectAll(selection) {
      // tabledata第一层只要有在selection里面就是全选
      const isSelect = selection.some((el) => {
        const tableDataIds = this.ruleData.map((j) => j.deptId);
        return tableDataIds.includes(el.deptId);
      });
      // tableDate第一层只要有不在selection里面就是全不选
      const isCancel = !this.ruleData.every((el) => {
        const selectIds = selection.map((j) => j.deptId);
        return selectIds.includes(el.deptId);
      });
      if (isSelect) {
        selection.map((el) => {
          if (el.children) {
            // 解决子组件没有被勾选到
            this.setChildren(el.children, true);
          }
        });
      }
      if (isCancel) {
        this.ruleData.map((el) => {
          if (el.children) {
            // 解决子组件没有被勾选到
            this.setChildren(el.children, false);
          }
        });
      }
    },
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值