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);
}
});
}
},