需求:要求对三级菜单有增加和删除的功能,但只有一级和二级的菜单有增加的功能,空的没有子菜单的菜单才能进行删除。
A、页面部分
注意圈红圈的为新增的内容
B、方法部分
C、代码
<template>
<el-tree
:data="menus"
:props="defaultProps"
:expand-on-click-node="false"
show-checkbox="true"
node-key="catId"
:default-expanded-keys="expandedKey"
><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
v-if="node.childNodes.length == 0"
type="text"
size="mini"
@click="() => remove(node, data)"
>
Delete
el-button>
span>
span>el-tree
>
template>
<script>
export default {
data() {
return {
menus: [],
expandedKey: [],
defaultProps: {
children: "children",
label: "name",
},
};
},
created() {
this.getMenus();
},
methods: {
getMenus() {
this.$http({
url: this.$http.adornUrl("/product/category/list/tree"),
method: "get",
}).then(({ data }) => {
console.log("成功获取后台数据", data.data);
this.menus = data.data;
});
},
append(data) {
const newChild = { id: id++, label: "testtest", children: [] };
if (!data.children) {
this.$set(data, "children", []);
}
data.children.push(newChild);
},
remove(node, data) {
this.$confirm(`是否删除【${data.name}】菜单?`, "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
})
.then(() => {
var ids = [data.catId];
this.$http({
url: this.$http.adornUrl("/product/category/delete"),
method: "post",
data: this.$http.adornData(ids, false),
}).then(({ data }) => {
this.$message({
message: "菜单删除成功",
type: "success",
});
// 刷新出新的菜单
this.getMenus();
// 设置需要默认展开的菜单
this.expandedKey = [node.parent.data.catId]
});
})
.catch(() => {});
},
},
};
script>
逻辑删除:表中某一字段可以标志该条数据是否被删除,不是真正的从数据库表中直接删除,就比如 category 表中的 show_status 字段,0表示删除,1表示显示
物理删除:直接把该条数据从数据库中 delete 掉
这里我们编写的删除则是使用逻辑删除。。。。。
CategoryController 中:
/**
* 删除
* @RequestBody : 获取请求体, 必须发送 post 请求
* SpringMVC 自动将请求体的数据(json), 转为对应的对象
*/
@RequestMapping("/delete")
public R delete(@RequestBody Long[] catIds){
//1、检查当前删除的菜单, 是否被别的地方引用
//categoryService.removeByIds(Arrays.asList(catIds));
categoryService.removeByMenuIds(Arrays.asList(catIds));
return R.ok();
}
CategoryService 及 CategoryServiceImpl 中:
@Override
public void removeByMenuIds(List<Long> asList) {
// TODO 1、检查当前删除的菜单, 是否在别的地方被引用
// 逻辑删除
baseMapper.deleteBatchIds(asList);
}
逻辑删除步骤:
A、配置全局的逻辑删除规则
B、配置逻辑删除的组件(省略)
C、加上逻辑删除注解 @TopicLogic
MyBatis-Plus 中配置逻辑删除:
实体类属性加上 @TableLogic 注解
主要是编写前端的代码,后端我们调用框架生成的 save 方法去保存
<template>
<div>
<el-tree
:data="menus"
:props="defaultProps"
:expand-on-click-node="false"
show-checkbox="true"
node-key="catId"
:default-expanded-keys="expandedKey"
><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
v-if="node.childNodes.length == 0"
type="text"
size="mini"
@click="() => remove(node, data)"
>
Delete
</el-button>
</span>
</span></el-tree
>
<el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
<el-form :model="form">
<el-form-item label="分类名称">
<el-input v-model="category.name" 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="addCategory">确 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
export default {
data() {
return {
category: { name: "", parentCid: 0, catLevel: 0, showStatus: 1, sort: 0 },
dialogVisible: false,
menus: [],
expandedKey: [],
defaultProps: {
children: "children",
label: "name",
},
};
},
created() {
this.getMenus();
},
methods: {
getMenus() {
this.$http({
url: this.$http.adornUrl("/product/category/list/tree"),
method: "get",
}).then(({ data }) => {
console.log("成功获取后台数据", data.data);
this.menus = data.data;
});
},
append(data) {
this.dialogVisible = true;
this.category.parentCid = data.catId;
this.category.catLevel = data.catLevel * 1 + 1;
},
addCategory() {
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();
// 设置需要默认展开的菜单
this.expandedKey = [this.category.parentCid];
});
},
remove(node, data) {
this.$confirm(`是否删除【${data.name}】菜单?`, "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
})
.then(() => {
var ids = [data.catId];
this.$http({
url: this.$http.adornUrl("/product/category/delete"),
method: "post",
data: this.$http.adornData(ids, false),
}).then(({ data }) => {
this.$message({
message: "菜单删除成功",
type: "success",
});
// 刷新出新的菜单
this.getMenus();
// 设置需要默认展开的菜单
this.expandedKey = [node.parent.data.catId];
});
})
.catch(() => {});
},
},
};
</script>
后台代码如图: