上一篇中实现了三级分类的基本页面,下来就完成三级分类的的修改删除及其批量操作。
前端开发
增加和删除
使用Vue中的插槽机制,在el-tree中添加一个spn,slot-scop中使用JS解构传入node和data,在里面添加两个button一个为Append一个为Remove,然后为其绑定单击响应函数分别为append(data)和remove(data)
<!-- 使用vue slot插槽机制 解构传入当前节点和节点数据 -->
<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>
编写一个弹框,供添入具体信息,通过控制vue实例中的变量来和edit等操作复用弹框
<el-dialog
:title="title"
:visible.sync="dialogVisible"
width="30%"
:close-on-click-modal="false"
>
<el-form :model="category">
<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>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="submitData">确 定</el-button>
</span>
</el-dialog>
绑定的单机响应函
//添加节点
append(data) {
console.log(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) {
var ids = [data.catId];
// 删除弹框相关
this.$confirm(
`此操作将永久删除【${data.name}】, 是否删除当前菜单?`,
"提示",
{
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
}
)
.then(() => {
// 发送post请求删除数据
this.$http({
url: this.$http.adornUrl("/product/category/delete"),
method: "post",
data: this.$http.adornData(ids, false)
}).then(({ data }) => {
this.$message({
message: "删除成功",
type: "success"
});
// 重新获取Menus
this.getMenus();
// 设置需要默认展开的菜单 将父节点的ID绑定到expendedKey
this.expendedKey = [node.parent.data.catId];
});
})
.catch(() => {});
}
数据增加的提交操作
// 为提交按钮绑定的函数
submitData() {
if (this.dialogType == "add") {
this.addCategory();
}
if (this.dialogType == "edit") {
this.editCategory();
}
}
// 添加三级分类(提交添加的节点)
addCategory() {
console.log("提交的数据" + this.category);
this.$http({
url: this.$http.adornUrl("/product/category/save"),
method: "post",
data: this.$http.adornData(this.category, false)
}).then(({ data }) => {
this.$message({
message: "菜单创建成功",
type: "success"
});
// 关闭对话框
(this.dialogVisible = false),
// 刷新菜单
this.getMenus();
// 设置需要默认展开的菜单 将父节点的ID绑定到expendedKey
this.expendedKey = [this.category.parentCid];
});
}
修改
和前面操作一样
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();
// 设置需要默认展开的菜单 将父节点的ID绑定到expendedKey
this.expendedKey = [this.category.parentCid];
});
},
edit(data) {
console.log(data);
this.dialogVisible = true;
this.dialogType = "add";
this.title = "修改分类";
// 发送请求获取最新的数据(否则用以前的数据可能会导致数据不一致的问题)
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;
});
}
下面为完整代码,包括批量删除及其修改,还有拖拽效果实现及其优化
<template>
<div>
<el-switch v-model="draggable" active-text="开启拖拽功能" inactive-text="关闭拖拽功能"></el-switch>
<el-button v-if="draggable" type="success" round @click="batchSave">批量保存</el-button>
<el-button type="danger" @click="batchDelete">批量删除</el-button>
<el-tree
:data="menus"
:props="defaultProps"
show-checkbox
node-key="catId"
:draggable="draggable"
:default-expanded-keys="expendedKey"
:expand-on-click-node="false"
:allow-drop="allowDrop"
ref="menuTree"
>
<!-- draggable 拖拽 ref用来标识这个组件,为了后续能够使用这个组件里自带的方法-->
<!-- 使用vue slot插槽机制 解构传入当前节点和节点数据 -->
<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>
<!-- 对话框 close-on-click-modal 鼠标溢出对话框对话框不消失 -->
<el-dialog
:title="title"
:visible.sync="dialogVisible"
width="30%"
:close-on-click-modal="false"
>
<el-form :model="category">
<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>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="submitData">确 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
export default {
data() {
// menus就是菜单全部内容 defaultProps就是指定子树的属性 label就是真正显示的属性 expendedKey:绑定要展开的数组
// dialogVisible 对话框默认关闭 title:提示标题
return {
draggable: false,
// 因为是批量删除,因此页面批量展开是需要之前所有的pCid
pCid: [],
updateNodes: [],
maxLevel: 0,
title: "",
dialogType: "", //edit add
category: {
name: "",
parentCid: 0,
catLevel: 0,
showStatus: 1,
sort: 0,
catId: null,
productUnit: "",
icon: ""
},
dialogVisible: false,
menus: [],
expendedKey: [],
defaultProps: {
children: "children",
label: "name"
}
};
},
methods: {
// 获取三级三级分类菜单
getMenus() {
this.dataListLoading = false;
this.$http({
url: this.$http.adornUrl("/product/category/list/tree"),
method: "get"
// 将data结构出来,代表的是data.data,避免加载一些不需要的内容
}).then(({ data }) => {
console.log(data.data);
// 给菜单元素设置值
this.menus = data.data;
});
},
submitData() {
if (this.dialogType == "add") {
this.addCategory();
}
if (this.dialogType == "edit") {
this.editCategory();
}
},
//添加节点
append(data) {
console.log(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;
},
// 添加三级分类(提交添加的节点)
addCategory() {
console.log("提交的数据" + this.category);
this.$http({
url: this.$http.adornUrl("/product/category/save"),
method: "post",
data: this.$http.adornData(this.category, false)
}).then(({ data }) => {
this.$message({
message: "菜单创建成功",
type: "success"
});
// 关闭对话框
(this.dialogVisible = false),
// 刷新菜单
this.getMenus();
// 设置需要默认展开的菜单 将父节点的ID绑定到expendedKey
this.expendedKey = [this.category.parentCid];
});
},
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();
// 设置需要默认展开的菜单 将父节点的ID绑定到expendedKey
this.expendedKey = [this.category.parentCid];
});
},
edit(data) {
console.log(data);
this.dialogVisible = true;
this.dialogType = "add";
this.title = "修改分类";
// 发送请求获取最新的数据(否则用以前的数据可能会导致数据不一致的问题)
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;
});
},
// 删除节点
remove(node, data) {
var ids = [data.catId];
// 删除弹框相关
this.$confirm(
`此操作将永久删除【${data.name}】, 是否删除当前菜单?`,
"提示",
{
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
}
)
.then(() => {
this.$http({
url: this.$http.adornUrl("/product/category/delete"),
method: "post",
data: this.$http.adornData(ids, false)
}).then(({ data }) => {
this.$message({
message: "删除成功",
type: "success"
});
// 重新获取Menus
this.getMenus();
// 设置需要默认展开的菜单 将父节点的ID绑定到expendedKey
this.expendedKey = [node.parent.data.catId];
});
})
.catch(() => {});
},
// type:prev inner next 通过返回true false
allowDrop(draggingNode, dropNode, type) {
//1、被拖动的当前节点以及所在的父节点总层数不能大于3
//1)、被拖动的当前节点总层数
console.log("allowDrop:", draggingNode, dropNode, type);
//
this.countNodeLevel(draggingNode);
//当前正在拖动的节点+父节点所在的深度不大于3即可
let deep = Math.abs(this.maxLevel - draggingNode.level) + 1;
console.log("深度:", deep);
// this.maxLevel
if (type == "inner") {
// console.log(
// `this.maxLevel:${this.maxLevel};draggingNode.data.catLevel:${draggingNode.data.catLevel};dropNode.level:${dropNode.level}`
// );
return deep + dropNode.level <= 3;
} else {
return deep + dropNode.parent.level <= 3;
}
},
// 计算当前被拖拽节点的最大深度
countNodeLevel(node) {
//找到所有子节点,求出最大深度
if (node.childNodes != null && node.childNodes.length > 0) {
for (let i = 0; i < node.childNodes.length; i++) {
if (node.childNodes[i].level > this.maxLevel) {
this.maxLevel = node.childNodes[i].level;
}
this.countNodeLevel(node.childNodes[i]);
}
}
},
// 处理拖动
handleDrop(draggingNode, dropNode, dropType, ev) {
console.log("handleDrop: ", draggingNode, dropNode, dropType);
//1、当前节点最新的父节点id
let pCid = 0;
// 所有兄弟节点
let siblings = null;
// 如果是前后拖动,那就父节点Id就是目标节点的父ID,那兄弟节点就是目标节点父Id的所有子节点
// 如果是拖动到里面,那父节点Id就是目标节点ID,那兄弟节点就是目标节点的子节点
if (dropType == "before" || dropType == "after") {
pCid =
dropNode.parent.data.catId == undefined
? 0
: dropNode.parent.data.catId;
siblings = dropNode.parent.childNodes;
} else {
pCid = dropNode.data.catId;
siblings = dropNode.childNodes;
}
this.pCid.push(pCid);
//2、当前拖拽节点的最新顺序 也就是将对应的兄弟节点重新遍历
// 如果层级未变,那被拖的节点的子节点层级也不变,否则需要对其对应的子节点的层级进行递归修改
for (let i = 0; i < siblings.length; i++) {
if (siblings[i].data.catId == draggingNode.data.catId) {
//如果遍历的是当前正在拖拽的节点
let catLevel = draggingNode.level;
// 如果被拖动的节点层级变化,那就调用updateChildNodeLevel修改它子节点的层级
if (siblings[i].level != draggingNode.level) {
//当前节点的层级发生变化
catLevel = siblings[i].level;
//修改他子节点的层级
this.updateChildNodeLevel(siblings[i]);
}
// 将需要修改的节点放入updateNodes
this.updateNodes.push({
catId: siblings[i].data.catId,
sort: i,
parentCid: pCid,
catLevel: catLevel
});
} else {
this.updateNodes.push({ catId: siblings[i].data.catId, sort: i });
}
}
//3、当前拖拽节点的最新层级
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;
// 将这些节点放入到updateNodes中
this.updateNodes.push({
catId: cNode.catId,
catLevel: node.childNodes[i].level
});
// 递归调用
this.updateChildNodeLevel(node.childNodes[i]);
}
}
},
batchSave() {
this.$http({
url: this.$http.adornUrl("/product/category/update/sort"),
method: "post",
data: this.$http.adornData(this.updateNodes, false)
}).then(({ data }) => {
this.$message({
message: "菜单顺序等修改成功",
type: "success"
});
//刷新出新的菜单
this.getMenus();
//设置需要默认展开的菜单
this.expandedKey = this.pCid;
this.updateNodes = [];
this.maxLevel = 0;
// this.pCid = 0;
});
},
batchDelete() {
let checkedNodes = this.$refs.menuTree.getCheckedNodes();
let catIds = [];
console.log("checked" + checkedNodes);
for (let i = 0; i < checkedNodes.length; i++) {
catIds.push(checkedNodes[i].catId);
}
this.$confirm(`是否批量删除【${catIds}】菜单?`, "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
})
.then(() => {
this.$http({
url: this.$http.adornUrl("/product/category/delete"),
method: "post",
data: this.$http.adornData(catIds, false)
}).then(({ data }) => {
this.$message({
message: "菜单批量删除成功",
type: "success"
});
this.getMenus();
});
})
.catch(() => {});
}
},
created() {
this.getMenus();
}
};
</script>
<style>
</style>
后台开发
增加
/**
* 保存
*/
@RequestMapping("/save")
//@RequiresPermissions("product:category:save")
public R save(@RequestBody CategoryEntity category){
categoryService.save(category);
return R.ok();
}
删除
/**
* 删除分类及其相关
* @RequestBody:获取请求体,只存在于post中,因此必须发post
* SpringMvc会自动将里面的JSON数据转为对象
*/
@RequestMapping("/delete")
public R delete(@RequestBody Long[] catIds){
categoryService.removeMenuByIds(Arrays.asList(catIds));
return R.ok();
}
修改
/**
* 修改
*/
@RequestMapping("/update")
public R update(@RequestBody CategoryEntity category){
categoryService.updateById(category);
return R.ok();
}
/**
* 批量拖拽
* @param categoryEntities
* @return
*/
@RequestMapping("/update/sort")
public R updateSort(@RequestBody CategoryEntity[] categoryEntities){
categoryService.updateBatchById(Arrays.asList(categoryEntities));
return R.ok();
}
这样就彻底完成了三级分类的相关内容!