所需参数设置:
data () {
return {
maps: new Map()
}
},
table:
设置:
ref="table"
ref需要设置,后面会用到
<el-table
v-loading="dataListLoading"
ref="table"
:data="dataList"
row-key="id"
border
style="width: 100%;"
:max-height="tableHeight"
lazy
:load="load"
:tree-props="{children: 'children', hasChildren: 'hasChildren'}"
>
核心代码:
const { pid } = row
const { tree, treeNode, resolve } = this.maps.get(pid)
this.$set(this.$refs.table.store.states.lazyTreeNodeMap, pid, [])
this.load(tree, treeNode, resolve)
删除功能完整代码如下:
deleteRow (row) {
console.log(row)
if (row.children.length !== 0) {
this.$message.warning('请先删除子节点!')
return
}
this.$confirm('确认进行删除操作?', {
confirmButtonText: this.$t('confirm'),
cancelButtonText: this.$t('cancel'),
type: 'warning'
}).then(() => {
this.$http.delete('url', { 'data': [row.id] }).then(({ data: res }) => {
if (res.code !== 0) {
return this.$message.error(res.msg)
}
this.$message({
message: this.$t('prompt.success'),
type: 'success',
duration: 500
})
this.getDataList()
const { pid } = row
const { tree, treeNode, resolve } = this.maps.get(pid)
this.$set(this.$refs.table.store.states.lazyTreeNodeMap, pid, [])
this.load(tree, treeNode, resolve)
}).catch(() => {
})
}).catch(() => { })
}
load核心代码
const pid = tree.id
this.maps.set(pid, { tree, treeNode, resolve })
load完整代码:
load (tree, treeNode, resolve) {
const pid = tree.id
this.maps.set(pid, { tree, treeNode, resolve })
this.$http.get('url', {
params: {
pid: tree.id
}
}).then(({ data: res }) => {
if (res.code !== 0) {
return this.$message.error(res.msg)
}
let list = res.data
let newArr = list.map(v => {
return { ...v, hasChildren: v.grade === 1 }
})
resolve(newArr)
}).catch(() => { })
}
参考地址:
https://blog.youkuaiyun.com/weixin_42225341/article/details/94740238