单选为例
<div class="comment-container gy-table-sort">
<el-table
v-loading="loading"
:data="tableData"
row-key="id"
border
lazy
:load="load"
:tree-props="{children: 'children', hasChildren: 'hasChildren'}"
>
<el-table-column type="selection" width="55">
<template slot-scope="scope">
<span v-if="scope.row.children">--</span>
<el-checkbox v-else :value="scope.row.checked" @change="handleSelectionChange(scope.row)"></el-checkbox>
</template>
</el-table-column>
<el-table-column prop="date" label="日期" width="180"/>
<el-table-column prop="name" label="姓名" width="180"/>
<el-table-column prop="address" label="地址"/>
</el-table>
<div class="mt20 dis-flex justify-center">
<el-button type="primary" :disabled="submitDisable" @click="submit()">确认</el-button>
</div>
</div>
data() {
return {
tableData: [{
id: 1,
date: '2016-05-02',
name: '王小虎',
address: '上海市普陀区金沙江路 1518 弄'
}, {
id: 2,
date: '2016-05-04',
name: '王小虎',
address: '上海市普陀区金沙江路 1517 弄'
}, {
id: 3,
date: '2016-05-01',
name: '王小虎',
address: '上海市普陀区金沙江路 1519 弄',
children: [{
id: 31,
date: '2016-05-01',
name: '王小虎',
address: '上海市普陀区金沙江路 1519 弄',
children: [{
id: 311,
date: '2016-05-01',
name: '王小虎',
address: '上海市普陀区金沙江路 1519 弄'
}]
}, {
id: 32,
date: '2016-05-01',
name: '王小虎',
address: '上海市普陀区金沙江路 1519 弄'
}]
}, {
id: 4,
date: '2016-05-03',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄'
}],
loading: true,
selectedList: [],
submitDisable: false
}
},
methods: {
load(tree, treeNode, resolve) {
console.log('tree, treeNode, resolve', tree, treeNode, resolve)
},
// 表单选择器
handleSelectionChange(row) {
this.findChild(this.tableData, row)
if (row.checked) {
this.selectedList = [row]
} else {
this.selectedList = []
}
},
findChild(child, row) {
child.map(x => {
if (x.id === row.id) {
if (x.checked) {
x.checked = !x.checked
} else {
this.$set(x, 'checked', true)
}
} else {
if (x.children) {
this.findChild(x.children, row)
} else {
this.$set(x, 'checked', false)
}
}
})
},
}