..._el-tree穿梭框">
赞
踩
基于element-ui的tree组件合成的穿梭框,包含上下位移删除子节点
<template> <div class="container"> <div class="trees"> <div class="treeP"> <p class="title"> <label class="la"> <span> <el-checkbox v-model="checked" @change="checkedAll()">全选</el-checkbox> </span> </label> </p> <div class="tree1"> <el-tree :data="data" show-checkbox node-key="id" default-expand-all ref="tree" > </el-tree> </div> </div> <div class="btn"> <el-button type="primary" @click="btn" icon="el-icon-arrow-right" circle></el-button> </div> <div class="treeP"> <p class="title"> <label class="la"> <span class="quit" @click="quit"> 清空 </span> </label> </p> <div class="tree1"> <el-tree :data="data1" node-key="id" draggable default-expand-all :expand-on-click-node="true"> <span class="custom-tree-node" slot-scope="{ node, data }"> <span>{{ node.label }}</span> <span> <el-button type="text" size="mini" @click="() => remove(node, data)"> 删除 </el-button> </span> </span> </el-tree> </div> </div> </div> <div style="width:50px;margin:30px auto;"> <el-button type="primary" @click="ti">提交</el-button> </div> </div> </template> <script> export default { data() { return { checked:false, checked2:false, data1:[], data: [{ id: 1, label: '一级 1', children: [ { id: 4, label: '二级 1-1', children:[ { id:11, label: '三级 1-1-1' }, { id:12, label: '三级 1-1-2' }, ] }, { id: 5, label: '二级 1-2', children:[ { id:13, label: '三级 1-2-1' }, { id:14, label: '三级 1-2-2' }, ] }, ] }, { id: 2, label: '一级 2', children: [ { id: 6, label: '二级 2-1', children:[ { id:15, label: '三级 2-1-1' }, { id:16, label: '三级 2-1-2' }, ] }, { id: 7, label: '二级 2-2', children:[ { id:17, label: '三级 2-2-1' }, { id:18, label: '三级 2-2-2' }, ] } ] }, { id: 3, label: '一级 3', children: [{ id: 8, label: '二级 3-1', children:[ { id:19, label: '三级 3-1-1' }, { id:20, label: '三级 3-1-2' }, ] }, { id: 9, label: '二级 3-2', children:[ { id:21, label: '三级 3-2-1' }, { id:22, label: '三级 3-2-2' }, ] }, { id: 10, label: '二级 3-3', children:[ { id:23, label: '三级 3-3-1' }, { id:24, label: '三级 3-3-2' }, ] },] }], vuetree: [], defaultProps: { children: 'children', label: 'label' }, }; }, methods: { remove(node, data) { const parent = node.parent; const children = parent.data.children || parent.data; const index = children.findIndex(d => d.id === data.id); children.splice(index, 1); }, checkedAll(){ this.checked ? this.$refs.tree.setCheckedNodes(this.data) : this.$refs.tree.setCheckedKeys([]); }, quit(){ this.data1 = [] }, btn(){ var arrs = JSON.parse(JSON.stringify(this.getSimpleCheckedNodes(this.$refs.tree.store))) // console.log(arrs) for(let i=arrs.length - 1; i>0;i--) { if(arrs[i].id === arrs[i-1].id){ arrs[i-1].children.push(...arrs[i].children) arrs.splice(i,1) } } this.data1 = arrs }, getSimpleCheckedNodes(store) { const checkedNodes = []; const traverse = function(node) { const childNodes = node.root ? node.root.childNodes : node.childNodes; childNodes.forEach(child => { if (child.checked) { // console.log(child) if(child.childNodes.length>0){ // console.log(child.data) if(child.parent.parent == null){ checkedNodes.push(child.data); }else{ var arr = JSON.parse(JSON.stringify(child.parent.data)) var obj = child.data arr.children.length = 0 arr.children.push(obj) checkedNodes.push(arr); } }else{ // 判断第二级菜单!!! if(!child.data.children){ var arrP = JSON.parse(JSON.stringify(child.parent.parent.data)) var arr = JSON.parse(JSON.stringify(child.parent.data)) var obj = child.data arrP.children.length = 0 arr.children.length = 0 arr.children.push(obj) arrP.children.push(arr) checkedNodes.push(arrP); } } } if (child.indeterminate) { traverse(child); } }); }; traverse(store) return checkedNodes; }, ti(){ var as = JSON.parse(JSON.stringify(this.data1)) for(var item of as){ delete item.label for(var list of item.children){ delete list.label for(var i of list.children){ delete i.label } } } console.log(JSON.stringify(as)) } } }; </script> <style> .container{ width: 1200px; height: 700px; margin:50px auto; } .trees{ width: 80%; margin: 0 auto; display: flex; justify-content: space-around; } .custom-tree-node { flex: 1; display: flex; align-items: center; justify-content: space-between; font-size: 14px; padding-right: 8px; } .tree1{ height: 500px; padding: 5px; overflow: scroll; overflow-x:hidden ; } .treeP{ width:40%;border: 1px solid #ebeef5;border-radius:5px; } .title{ height: 40px; background: #f5f7fa; } .la{ line-height: 40px;padding:0 10px 0 10px; } .quit{ font-size:14px;margin-left:83%;cursor: pointer; } .quit:hover{ opacity: .5; } .btn{ margin: auto; } </style>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。