vue elementUi tree lazy loaded using details
background:
Under vue use elementUI
Documents:
http://element-cn.eleme.io/#/zh-CN/component/tree#tree-shu-xing-kong-jian
demand:
Saves only two nodes selected data; a selected node data is not saved.
effect:
Data Sources:
Background two interfaces are provided for data taking a first stage and a second stage node of the node;
Ideas:
Tag list is clicked, the trigger selectLabelList first level node data acquisition trigger lodeNode filled, expand a node; click on any node of the down arrow eleven filled == 1 node.level loadNode acquired by the two nodes. Each selection will trigger handleCheckChange get selected or deselected delete content; save all secondary data will eventually be selected by the user to labelCheckedList this array.
important point:
node-key、ref、lazy这3个属性一定要有
一级节点传递给二级节点的值是用node.data里面的id即node.data.id而不是用官网案例上的node.id(被坑过)
- 1
- 2
Combat:
html:
<el-button @click="selectLabelList">标签列表</el-button>
<el-tree
node-key="id"
ref="tree"
highlight-current
:props="props"
:load="loadNode"
lazy=""
show-checkbox
@check-change="handleCheckChange">
</el-tree>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
js: This is the core part of the code, not all, and some fields have not been defined.
data() {
return {
labelCheckedList:[],
props: {
label: 'name',
children: 'zones',
}}
// labelCheckedList接收被勾选的数据
handleCheckChange(data){
this.currTreeId=data.id
setTimeout(() => {
let currtData = this.$refs.tree.getCheckedNodes(true)
this.labelCheckedList = currtData;
}, 100);
},
//懒加载时触发
loadNode(node, resolve) {
if (node.level === 0) {
return resolve(this.categorylist);
}
if (node.level > 1) return resolve([]);
if(node.level === 1) { // 二级节点
this.getChildrenNode(node,resolve)
}
},
// 二级节点
getChildrenNode(node,resolve) {
let categoryId = node.data.id;
this.$http.post('/api/getLabelListByCategoryId', {
categoryId:categoryId
},
{
emulateJSON: true,
emulateHTTP: true
}).then(res => {
this.childrenList = res.body;
if(this.childrenList.length==0){
this.$message.error('数据拉取失败,请刷新再试!');
return;
}
resolve(this.childrenList);
});
},
// 收起、展示tree
selectLabelList() {
if(!this.isShowTree){
this.getCategorylist();
}else{
this.$refs.tree.$data.store.lazy = false
this.isShowTree=false;
}
},
//获取一级节点
getCategorylist(){
this.$http.post('/api/categorylist', {
searchInfo: this.searchLabelInfo,
}).then(res => {
let data = res.body.data.list;
if(data.length > 0){
data.forEach(item => {
item.disabled= true;
})
}
this.categorylist = data;
this.isShowTree=true;
})
},