<template>
<el-table
:data="tableData"
row-key="id"
:expand-row-keys="expandRowKeys"
:tree-props="{ children: 'children' }"
@expand-change="handleExpandChange"
>
<el-table-column type="expand">
<template slot-scope="scope">
<tree-table :data="scope.row.children" />
</template>
</el-table-column>
<el-table-column
type="selection"
width="55"
:reserve-selection="true"
:selectable="checkSelectable"
>
<template slot-scope="scope">
<el-checkbox
:value="isSelected(scope.row)"
@change="handleCheckChange(scope.row, $event)"
></el-checkbox>
</template>
</el-table-column>
<!-- 其他列定义 -->
</el-table>
</template>
<script>
export default {
name: 'TreeTable',
props: {
data: Array
},
data() {
return {
expandRowKeys: [],
selected: new Set()
};
},
methods: {
handleExpandChange(row, expandedRows) {
if (expandedRows.length) {
this.expandRowKeys = [row.id];
} else {
this.expandRowKeys = [];
}
},
handleCheckChange(row, checked) {
if (checked) {
this.selectParent(row);
this.selectChildren(row, checked);
} else {
this.unselectParent(row);
this.unselectChildren(row);
}
},
selectParent(row) {
if (row.parentId) {
const parent = this.findById(row.parentId);
if (parent && !this.isSelected(parent)) {
this.selected.add(parent);
}
}
},
selectChildren(row, checked) {
const children = (row.children || []).filter(child => !this.isSelected(child));
children.forEach(child => {
this.selected.add(child);
this.selectChildren(child, checked);
});
},
unselectParent(row) {
if (row.parentId) {
const parent = this.findById(row.parentId);
if (parent && this.isSelected(parent) && this.allChildrenUnselected(parent)) {
this.selected.delete(parent);
}
}
},
unselectChildren(row) {
const children = (row.children || []).filter(this.isSelected);
children.forEach(child => {
this.selected.delete(child);
this.unselectChildren(child);
});
},
findById(id) {
return this.data.find(item => item.id === id);
},
isSelected(row) {
return t
评论已关闭