我们有一个参数maxexpandId
给tree_render
添加一个prop
:
//el-tree component renderContent(h,{node,data,store}){//加载节点 let that = this; return h(TreeRender,{ props: { ... maxexpandId: that.non_maxexpandId }, on: {...} }); }
根据id判断:
//tree_render component props: ['NODE', 'DATA', 'STORE', 'maxexpandId']
<!-- tree_render component --> <span v-show="!DATA.isEdit" :class="[DATA.id > maxexpandId ? 'tree-new tree-label' : 'tree-label']" :ref="'treeLabel'+DATA.id"> <span>{{DATA.name}}</span> </span> .tree-expand .tree-label.tree-new{ font-weight:600; }
6.删
跟新增同义:删除节点 =》删除一条数据
- 新增节点直接删除
- 已有节点需提示再删除
- 已有子级节点不能删除
handleDelete(s,d,n){//删除节点 console.log(s,d,n) let that = this; //有子级不删除 if(d.children && d.children.length !== 0){ this.$message.error("此节点有子级,不可删除!") return false; }else{ //删除操作 let delNode = () => { let list = n.parent.data.children || n.parent.data, //节点同级数据,顶级节点时无children _index = 99999;//要删除的index list.map((c,i) => { if(d.id == c.id){ _index = i; } }) let k = list.splice(_index,1); //console.log(_index,k) this.$message.success("删除成功!") } let isDel = () => { that.$confirm("是否删除此节点?","提示",{ confirmButtonText: "确认", cancelButtonText: "取消", type: "warning" }).then(() => { delNode()//此处可通过ajax做删除操作 }).catch(() => { return false; }) } //新增节点直接删除,否则要通过请求数据删除 d.id > this.non_maxexpandId ? delNode() : isDel() } }
7.拓展
还有一些特别的需求,例如:
1、点击高亮的时候显示icon
.expand-tree .is-current>.el-tree-node__content .tree-btn, .expand-tree .el-tree-node__content:hover .tree-btn{ display: inline-block; }
2、添加顶级节点
添加按钮:
<!-- el-tree component --> <el-button @click="handleAddTop">添加顶级节点</el-button>
添加methods:
//el-tree component methods: { handleAddTop(){ this.setTree.push({ id: ++this.maxexpandId, name: '新增节点', pid: '', isEdit: false, children: [] }) } }
3、默认展开树形第一级