<template>
|
<div class="wrap">
|
<div class="bread">
|
<el-breadcrumb separator="el-icon-arrow-right">
|
<el-breadcrumb-item>{{ breadLabel }}</el-breadcrumb-item>
|
</el-breadcrumb>
|
</div>
|
<div class="treeBox">
|
<el-tree
|
ref="tree"
|
:props="defaultProps"
|
node-key="id"
|
:data="dirData"
|
:expand-on-click-node="false"
|
:default-expanded-keys="[1, 2, 3, 4, 5, 6, 7, 8, 9]"
|
draggable
|
@node-click="handleNodeClick"
|
@node-drop="handleDrop"
|
>
|
<span class="custom-tree-node" slot-scope="{ node, data }">
|
<span>{{ node.label }}</span>
|
<span class="btnBox" v-show="showCata">
|
<el-button
|
type="text"
|
size="mini"
|
@click="() => append(data, node)"
|
>
|
<i class="el-icon-circle-plus"></i>
|
</el-button>
|
<el-button
|
type="text"
|
size="mini"
|
@click="() => remove(node, data)"
|
>
|
<i class="el-icon-delete-solid"></i>
|
</el-button>
|
</span>
|
</span>
|
</el-tree>
|
<div style="margin-left: 130px">
|
<el-button @click="sendChange">保存</el-button>
|
</div>
|
</div>
|
</div>
|
</template>
|
|
<script>
|
import { queryDirTree, updateDirTree } from "../api/api";
|
export default {
|
name: "catalogueTree",
|
props: ["showBtn"],
|
data() {
|
return {
|
showCata: false,
|
showBread: false,
|
oriData: [], //原始树数据
|
dirData: [], //el树数据
|
newData: [], //拖动后数据
|
breadList: [], //面包屑数组
|
breadLabel: "", //面包屑文字
|
filterText: "",
|
defaultProps: {
|
children: "children",
|
label: "name",
|
},
|
};
|
},
|
|
methods: {
|
// 请求目录树
|
getDirTree() {
|
queryDirTree().then((res) => {
|
if (res.status == 200) {
|
this.oriData = res.data;
|
this.newData = res.data;
|
this.dirData = this.treeData(res.data);
|
} else {
|
console.log("接口报错");
|
}
|
});
|
},
|
treeData(source) {
|
let cloneData = JSON.parse(JSON.stringify(source)); // 对源数据深度克隆
|
return cloneData.filter((father) => {
|
// 循环所有项
|
let branchArr = cloneData.filter((child) => father.id == child.pid); // 对比ID,分别上下级菜单,并返回数据
|
branchArr.length > 0 ? (father.children = branchArr) : ""; // 给父级添加一个children属性,并赋值
|
// 属于同一对象问题,例如:令 a=b、c=1 ,然后再令 b.c=c , 那么 a.c=b.c=c=1 ;同理,后续令 c.d=2 ,那么 a.c.d 也是=2;
|
// 由此循环多次后,就能形成相应的树形数据结构
|
return father.pid == -1; // 返回一级菜单
|
});
|
},
|
append(data, parentNode) {
|
this.$prompt("请输入名称", "提示", {
|
confirmButtonText: "确定",
|
cancelButtonText: "取消",
|
})
|
.then(({ value }) => {
|
const newChild = {
|
id: value,
|
name: value,
|
pid: data.id,
|
// children: [],
|
oid: data.children ? data.children.length + 1 : 1,
|
};
|
if (!data.children) {
|
this.$set(data, "children", []);
|
}
|
data.children.push(newChild);
|
this.newData.push(newChild);
|
})
|
.catch(() => {
|
this.$message({
|
type: "info",
|
message: "取消输入",
|
});
|
});
|
},
|
remove(node, data) {
|
const parent = node.parent;
|
const children = parent.data.children || parent.data;
|
const index = children.findIndex((d) => d.id === data.id);
|
let res = children.splice(index, 1);
|
|
},
|
flaten(arr) {
|
return arr.reduce((p, v, i) => {
|
for (let i = 0; i < p.length; i++) {
|
if (p[i].children) {
|
delete p[i].children;
|
}
|
}
|
return p.concat(v.children ? this.flaten(v.children).concat(v) : v);
|
}, []);
|
},
|
handleNodeClick(data) {
|
// console.log(data);
|
this.$store.commit("changeNode", data); //传递点击的节点
|
this.breadList = [];
|
this.getTreeNode(this.$refs.tree.getNode(data.id));
|
},
|
getTreeNode(node) {
|
if (node && node.label) {
|
this.breadList.unshift(node.label);
|
this.getTreeNode(node.parent); //递归
|
this.breadLabel = this.breadList.join(">");
|
this.$store.commit("changeCata", this.breadLabel); //传递面包屑路径
|
}
|
},
|
handleDrop(draggingNode, dropNode, dropType, ev) {
|
console.log("被拖拽节点", draggingNode);
|
// console.log("进入的节点", dropNode);
|
// console.log("放置位置", dropType);
|
// console.log("事件", ev);
|
// if (dropType !== "inner") {
|
// // 1.修改父节点pid
|
// draggingNode.data.pid = dropNode.data.pid;
|
// dropNode.parent.childNodes.forEach((item, index) => {
|
// // 2.修改自身顺序oid
|
// item.data.oid = index + 1;
|
// });
|
// }
|
// // console.log(draggingNode.data.id);
|
// let res = this.oriData.filter((item) => item.id == draggingNode.data.id);
|
// console.log(res);
|
|
let paramData = [];
|
let data = dropType != "inner" ? dropNode.parent.data : dropNode.data;
|
let nodeData =
|
dropNode.level == 1 && dropType != "inner" ? data : data.children;
|
let pid = "";
|
nodeData.forEach((item, i) => {
|
if (dropType != "inner") {
|
if (draggingNode.data.pid === dropNode.data.pid) {
|
pid = item.pid;
|
} else {
|
pid = dropNode.data.pid;
|
}
|
} else {
|
pid = data.id;
|
}
|
let collection = {
|
id: item.id,
|
name: item.name,
|
pid,
|
oid: i + 1,
|
};
|
paramData.push(collection);
|
});
|
// console.log(paramData);
|
let arr = [];
|
this.oriData.forEach((e) => {
|
paramData.forEach((item) => {
|
if (item.id === e.id) {
|
e = item;
|
}
|
});
|
arr.push(e);
|
});
|
this.newData = arr;
|
},
|
|
sendChange() {
|
updateDirTree(this.newData).then((res) => {
|
console.log(res);
|
if (res.status == 200) {
|
this.$message({
|
type: "success",
|
message: "更新成功!",
|
});
|
}
|
});
|
},
|
},
|
mounted() {
|
this.getDirTree();
|
},
|
watch: {
|
showBtn: {
|
immediate: true,
|
handler(val) {
|
if (val) this.showCata = val;
|
},
|
},
|
showBread: {
|
immediate: true,
|
handler(val) {
|
if (val) this.showBread = val;
|
},
|
},
|
},
|
};
|
</script>
|
|
<style lang="less" scoped>
|
.wrap {
|
width: 98.5%;
|
height: 100%;
|
.bread {
|
width: 100%;
|
height: 5%;
|
margin: 0 auto;
|
overflow: auto;
|
}
|
.treeBox {
|
margin-top: 1%;
|
width: 100%;
|
height: 94%;
|
overflow: auto;
|
.el-tree {
|
background: transparent;
|
font-size: 15px;
|
font-family: Microsoft YaHei;
|
font-weight: 400;
|
color: #000000;
|
// /deep/ .el-tree-node__label {
|
// font-size: 18px;
|
// }
|
/deep/ .el-tree-node {
|
padding-top: 10px;
|
// padding-bottom: 10px;
|
}
|
/deep/ .el-tree-node:focus > .el-tree-node__content {
|
background-color: #b9b9b9;
|
}
|
/deep/ .el-tree-node__content:hover {
|
background-color: rgb(153, 153, 153);
|
}
|
.btnBox {
|
margin-left: 5px;
|
.el-button + .el-button {
|
margin-left: 5px;
|
}
|
}
|
}
|
}
|
}
|
</style>
|