el-table勾选子级半选父级别
时间: 2023-11-16 13:05:22 浏览: 153
可以通过设置 el-table 的 show-checkbox 属性为 true,同时设置 el-table-column 的 type 为 selection 来实现勾选功能。而要实现勾选子级半选父级的效果,可以通过以下步骤来实现:
1. 在 el-table-column 中设置 scopedSlot,用于自定义每一行的 checkbox。
2. 在自定义的 checkbox 中,绑定一个 change 事件,用于更新当前行的勾选状态,并递归更新其所有子节点的勾选状态。
3. 在 el-table 中监听 selection-change 事件,用于更新所有父节点的勾选状态。
具体实现可以参考以下代码:
```html
<template>
<el-table :data="data" show-checkbox @selection-change="handleSelectionChange">
<el-table-column type="selection" width="55">
<template slot-scope="{ row }">
<el-checkbox :indeterminate="row.indeterminate" v-model="row.checked" @change="handleCheckChange(row)"></el-checkbox>
</template>
</el-table-column>
<el-table-column prop="name" label="名称"></el-table-column>
<el-table-column prop="children" label="子节点">
<template slot-scope="{ row }">
<el-table :data="row.children" show-header="false" :row-style="{ 'padding-left': '20px' }">
<el-table-column type="selection" width="55">
<template slot-scope="{ row }">
<el-checkbox :indeterminate="row.indeterminate" v-model="row.checked" @change="handleCheckChange(row)"></el-checkbox>
</template>
</el-table-column>
<el-table-column prop="name" label="名称"></el-table-column>
</el-table>
</template>
</el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
data: [
{
name: '节点1',
children: [
{ name: '子节点1-1' },
{ name: '子节点1-2' },
{ name: '子节点1-3' }
]
},
{
name: '节点2',
children: [
{ name: '子节点2-1' },
{ name: '子节点2-2' },
{ name: '子节点2-3' }
]
}
]
}
},
methods: {
handleCheckChange(row) {
this.updateChildrenCheckState(row)
this.updateParentCheckState(row)
},
updateChildrenCheckState(row) {
if (row.children && row.children.length > 0) {
row.children.forEach(child => {
child.checked = row.checked
this.updateChildrenCheckState(child)
})
}
},
updateParentCheckState(row) {
let parent = this.getParentNode(row)
while (parent) {
const siblings = parent.children
const checkedCount = siblings.filter(sibling => sibling.checked).length
if (checkedCount === 0) {
parent.checked = false
parent.indeterminate = false
} else if (checkedCount === siblings.length) {
parent.checked = true
parent.indeterminate = false
} else {
parent.checked = false
parent.indeterminate = true
}
parent = this.getParentNode(parent)
}
},
getParentNode(node) {
const { data, store } = this.$refs.table
const { parent } = store.getNode(node)
return data[parent]
},
handleSelectionChange(selection) {
selection.forEach(row => {
this.updateParentCheckState(row)
})
}
}
}
</script>
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)