解决:el-table树型表格全选不选子级问题

104 阅读1分钟

监听表格 @select-all="selectAll" // 选择全部 selectAll (selection) { // tabledata第一层只要有在selection里面就是全选 const isSelect = selection.some(el => { const tableDataIds = this.tableData.map(j => j.id) return tableDataIds.includes(el.id) }) // tableDate第一层只要有不在selection里面就是全不选 const isCancel = !this.tableData.every(el => { const selectIds = selection.map(j => j.id) return selectIds.includes(el.id) }) console.log(isSelect, 'isSelect') if (isSelect) { selection.map(el => { if (el.children) { this.setChildren(el.children, true) } }) } if (isCancel) { this.tableData.map(el => { if (el.children) { this.setChildren(el.children, false) } }) } // this.emit('handleSelect', this.tableData) }, //全选子结构数据 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.tableRef && this.refs.tableRef.toggleRowSelection(row, select) }) } },