1.开启拖拽,拖拽时判定目标节点能否被放置,拖拽成功完成时触发的事件
allow-drop 拖拽时判定目标节点能否被放置。type 参数有三种情况:‘prev’、‘inner’ 和 ‘next’,分别表示放置在目标节点前、插入至目标节点和放置在目标节点后 Function(draggingNode, dropNode, type)
node-drop 拖拽成功完成时触发的事件 共四个参数,依次为:被拖拽节点对应的 Node、结束拖拽时最后进入的节点、被拖拽节点的放置位置(before、after、inner)、event
draggingNode-当前正在拖拽的节点
dropNode-进入到哪个节点
dropType-进入到节点的什么位置
2.执行流程
被拖拽节点draggingNode的node对象
node对象:在拖动时,被拖拽节点draggingNode中存放的数据是:拖动之前所在位置的级别(正确的1/2/3/4,与数据库中的data数据【不管咋拖动都不会变】的级别不一定相同),包括子节点的级别也是正确的(1/2/3/4)
拖拽时判定目标节点能否被放置
1.获取当前被拖动节点的最大深度:通过递归的方式,找到所有子节点,找到最大深度
2.获取当前被拖动节点所在的父节点的深度
3.被拖动的当前节点深度,以及所在的父节点深度,总层数不能大于三
即:当前正在拖动的节点的深度+父节点所在的深度,不大于3即可
拖拽成功完成时
1.根据被拖拽节点的放置位置,获取被拖拽的节点最新父节点ID和被拖拽的节点最新父节点的所有子节点
2.遍历所有子节点
2.1如果遍历到的节点是:被拖拽的节点
a.遍历到的节点是,被拖拽的节点,且当前节点的层级!=被拖拽的节点之前的层级
需要修改他子节点层级
需要更新当前遍历节点的顺序sort属性、父节点parentCid属性和层级
b.遍历到的节点是,被拖拽的节点,且当前节点的层级=被拖拽的节点之前的层级
只需要更新当前遍历节点的顺序sort属性和父节点parentCid属性
2.2如果遍历到的节点不是:被拖拽的节点
只需要更新当前遍历节点的顺序sort属性
3.category.vue
<template>
<div>
<el-tree
:data="menus"
:props="defaultProps"
:expand-on-click-node="false"
show-checkbox
node-key="catId"
:default-expanded-keys="expandedkey"
draggable
:allow-drop="allowDrop"
@node-drop="handleDrop"
>
<span class="custom-tree-node" slot-scope="{ node, data }">
<span>{{ node.label }}</span>
<span>
<el-button
v-if="node.level <= 2"
type="text"
size="mini"
@click="() => append(data)"
>Append</el-button>
<el-button type="text" size="mini" @click="() => edit(data)">Edit</el-button>
<el-button
v-if="node.childNodes.length==0"
type="text"
size="mini"
@click="() => remove(node, data)"
>Delete</el-button>
</span>
</span>
</el-tree>
<el-dialog :title="title" :visible.sync="dialogVisible" width="30%">
<el-form :model="category" label-width="80px">
<el-form-item label="分类名称">
<el-input v-model="category.name" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="图标">
<el-input v-model="category.icon" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="计量单位">
<el-input v-model="category.productUnit" autocomplete="off"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="submitData">确 定</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
//这里可以导入其他文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)
//例如:import 《组件名称》 from '《组件路径》';
export default {
//import引入的组件需要注入到对象中才能使用
components: {},
data() {
return {
updateNodes: [],
maxLevel: 0,
title: "",
dialogType: "", //edit,add
dialogVisible: false,
category: {
name: "",
parentCid: 0,
catLevel: 0,
showStatus: 1,
sort: 0,
productUnit: "",
icon: "",
catId: null,
},
expandedkey: [],
menus: [],
defaultProps: {
children: "children",
label: "name",
},
};
},
methods: {
handleDrop(draggingNode, dropNode, dropType, ev) {
//this.updateNodes = [];
console.log("handleDrop: ", draggingNode, dropNode, dropType);
//1.根据被拖拽节点的放置位置,获取被拖拽的节点最新父节点ID和被拖拽的节点最新父节点的所有子节点
var pId = 0;
let siblings = null;
if (dropType == "inner") {
pId = dropNode.key;
siblings = dropNode.childNodes;
} else {
pId = dropNode.parent.key ? dropNode.parent.key : 0;
siblings = dropNode.parent.childNodes;
}
//2.遍历所有子节点
for (let i = 0; i < siblings.length; i++) {
// 2.1如果遍历到的节点是:被拖拽的节点
if (siblings[i].data.catId == draggingNode.data.catId) {
if (siblings[i].level != draggingNode.level) {
// a.遍历到的节点是,被拖拽的节点,且当前节点的层级!=被拖拽的节点之前的层级
// 需要修改他子节点层级
updateChildNodeLevel(siblings[i]);
// 需要更新当前遍历节点的顺序sort属性、父节点parentCid属性和层级
this.updateNodes.push({
catId: siblings[i].data.catId,
sort: i,
parentCid: pId,
catLevel: siblings[i].level,
});
} else {
// b.遍历到的节点是,被拖拽的节点,且当前节点的层级=被拖拽的节点之前的层级
// 只需要更新当前遍历节点的顺序sort属性和父节点parentCid属性
this.updateNodes.push({
catId: siblings[i].data.catId,
sort: i,
parentCid: pId,
});
}
} else {
// 2.2如果遍历到的节点不是:被拖拽的节点
// 只需要更新当前遍历节点的顺序sort属性
this.updateNodes.push({ catId: siblings[i].data.catId, sort: i });
}
}
//打印当前拖拽节点的最新层级
console.log("updateNodes:", this.updateNodes);
},
//递归更新子节点层级
updateChildNodeLevel(node) {
if (node.childNodes.length > 0) {
for (let i = 0; i < node.childNodes.length; i++) {
var cNode = node.childNodes[i].data;
this.updateNodes.push({
catId: cNode.catId,
catLevel: node.childNodes[i].level,
});
this.updateChildNodeLevel(node.childNodes[i]);
}
}
},
allowDrop(draggingNode, dropNode, type) {
//console.log("allowDrop: ", draggingNode, dropNode, type);
//定义节点的最大深度对象
var nodeMaxLevel = { maxLevel: draggingNode.level };
//1.获取当前被拖动节点的最大深度:通过递归的方式,找到所有子节点,找到最大深度
this.countNodeLevel(draggingNode, nodeMaxLevel);
//求出当前节点深度
var deep = nodeMaxLevel.maxLevel - draggingNode.level + 1;
// console.log(
// "深度_" + deep,
// "当前正在拖动的节点最深节点_" + nodeMaxLevel.maxLevel,
// "当前正在拖动的节点所在的深度_" + draggingNode.level
// );
//2.获取当前被拖动节点所在的父节点的深度
var parentLevel = 0;
if (type == "inner") {
parentLevel = dropNode.level;
} else {
parentLevel = dropNode.parent.level;
}
//console.log("父节点的深度_" + parentLevel);
//3.被拖动的当前节点深度,以及所在的父节点深度,总层数不能大于三
//即:当前正在拖动的节点的深度+父节点所在的深度,不大于3即可
return deep + parentLevel <= 3;
},
countNodeLevel(node, nodeMaxLevel) {
if (node.childNodes != null && node.childNodes.length > 0) {
for (let i = 0; i < node.childNodes.length; i++) {
var data = node.childNodes[i];
//如果当前节点的级别比较大,就赋值给最大深度
if (data.level > nodeMaxLevel.maxLevel) {
nodeMaxLevel.maxLevel = data.level;
}
//通过递归的方式,找到所有子节点
this.countNodeLevel(data, nodeMaxLevel);
}
}
},
//修改三级分类数据
editCategory() {
var { catId, name, icon, productUnit } = this.category;
this.$http({
url: this.$http.adornUrl("/product/category/update"),
method: "post",
data: this.$http.adornData({ catId, name, icon, productUnit }, false),
}).then(({ data }) => {
this.$message({
message: "菜单修改成功",
type: "success",
});
//关闭对话框
this.dialogVisible = false;
//刷新出新的菜单
this.getMenus();
//设置需要默认展开的菜单
//this.expandedKey = [this.category.parentCid];
this.expandedkey = [this.category.parentCid];
console.log("this.expandedKey=", this.expandedkey);
});
},
addCategory() {
this.$http({
url: this.$http.adornUrl("/product/category/save"),
method: "post",
data: this.$http.adornData(this.category, false),
}).then(({ data }) => {
this.$message({
type: "success",
message: `添加菜单成功`,
});
this.dialogVisible = false;
this.getMenus();
this.expandedkey = [this.category.parentCid];
console.log("this.expandedKey", this.expandedkey);
});
},
submitData() {
console.log("submitData........");
if (this.dialogType == "add") {
this.addCategory();
}
if (this.dialogType == "edit") {
this.editCategory();
}
},
edit(data) {
console.log("要修改的数据", data);
this.dialogType = "edit";
this.title = "修改分类";
this.dialogVisible = true;
//发送请求获取当前节点最新的数据
this.$http({
url: this.$http.adornUrl(`/product/category/info/${data.catId}`),
method: "get",
}).then(({ data }) => {
//请求成功
console.log("要回显的数据", data);
this.category.name = data.data.name;
this.category.catId = data.data.catId;
this.category.icon = data.data.icon;
this.category.productUnit = data.data.productUnit;
this.category.parentCid = data.data.parentCid;
this.category.catLevel = data.data.catLevel;
this.category.sort = data.data.sort;
this.category.showStatus = data.data.showStatus;
console.log("this.expandedKey......", this.expandedkey);
/**
* parentCid: 0,
catLevel: 0,
showStatus: 1,
sort: 0,
*/
});
},
append(data) {
console.log("append", data);
//添加时,初始化相关属性
this.dialogType = "add";
this.title = "添加分类";
this.dialogVisible = true;
this.category.parentCid = data.catId;
this.category.catLevel = data.catLevel * 1 + 1;
this.category.catId = null;
this.category.name = "";
this.category.icon = "";
this.category.productUnit = "";
this.category.sort = 0;
this.category.showStatus = 1;
},
//删除分类
remove(node, data) {
console.log("remove", node, data);
var catIds = [data.catId];
//删除时危险操作,需要提示用户 使用MessageBox 弹框
this.$confirm(`确认删除【${data.name}】菜单`, "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
})
.then(({ value }) => {
this.$http({
url: this.$http.adornUrl("/product/category/delete"),
method: "post",
data: this.$http.adornData(catIds, false),
}).then(({ data }) => {
console.log("删除成功");
//Message 消息提示成功
this.$message({
type: "success",
message: `删除【${data.name}】菜单成功`,
});
//刷新页面 展开以前的父节点
this.getMenus();
//默认展开删除的节点
this.expandedkey = [node.parent.data.catId];
console.log("this.expandedkey", this.expandedkey);
});
})
.catch(() => {});
},
getMenus() {
this.$http({
url: this.$http.adornUrl("/product/category/list/tree"),
method: "get",
}).then(({ data }) => {
console.log("成功获取到分类数据", data);
this.menus = data.data;
});
},
},
//监听属性 类似于data概念
computed: {},
//监控data中的数据变化
watch: {},
//生命周期 - 创建完成(可以访问当前this实例)
created() {
console.log("获取到分类数据");
this.getMenus();
},
//生命周期 - 挂载完成(可以访问DOM元素)
mounted() {},
beforeCreate() {}, //生命周期 - 创建之前
beforeMount() {}, //生命周期 - 挂载之前
beforeUpdate() {}, //生命周期 - 更新之前
updated() {}, //生命周期 - 更新之后
beforeDestroy() {}, //生命周期 - 销毁之前
destroyed() {}, //生命周期 - 销毁完成
activated() {}, //如果页面有keep-alive缓存功能,这个函数会触发
};
</script>
<style scoped>
</style>