✅P53_三级分类-修改-基本修改效果完成
大约 6 分钟
src\views\modules\product\category.vue
添加Edit按钮
<el-tree :data="menu" :props="defaultProps" show-checkbox :expand-on-click-node="false" node-key="catId"
:default-expanded-keys="expandKey">
<span class="custom-tree-node" slot-scope="{ node, data }">
<span>{{ node.label }}</span>
<span>
//略......
<el-button type="text" size="mini" @click="edit(data)">Edit</el-button>
</span>
</span>
</el-tree>
对“确定”按钮请求进行操作判断
点击edit出现一个对话框,需要进行一个对话框的复用
//修改分类
edit(data) {
this.dialogFormVisible = true;
//略.......
},
我们点击确定调用的是添加方法 ,现在需要调用修改方法。因此,我们需要设定一个对话框的类型:
**dialogType**
export default {
data() {
return {
//区分新增和编辑的类型
dialogType: "",
//略.......
};
},
}
append(data) {
this.dialogType = "add";
//略.......
},
//修改分类
edit(data) {
this.dialogType = "edit";
//略.......
},
点击确定按钮执行方法进行区分新增
or 修改
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">取 消</el-button>
<el-button type="primary" @click="choose">确 定</el-button>
</div>
methods: {
choose() {
if (this.dialogType == "add") {
this.addCategory();
} else if (this.dialogType == "edit") {
this.editCategory();
}
},
}
点击 “edit”触发预检请求
问题:没有数据的回显 ,那能不能用slot传进来的data呢?
显然是不行的,这是上一次查询的数据,高并发的情况下数据很可能发生了变化。调用后端代码查询数据库进行数据回显。
com.gyz.cfmall.product.controller.CategoryController#info
/**
* 信息
*/
@RequestMapping("/info/{catId}")
public R info(@PathVariable("catId") Long catId) {
CategoryEntity category = categoryService.getById(catId);
//数据内容放在data里
return R.ok().put("category", category);
}
前端代码
methods: {
//修改分类
edit(data) {
console.log("要修改的分类数据对象为:", data);
this.dialogFormVisible = true;
this.dialogType = "edit";
this.$http({
url: this.$http.adornUrl(`/product/category/info/${data.catId}`),
method: "get",
params: this.$http.adornParams({})
}).then(({ data }) => {
console.log("编辑分类得数据回显:", data);
// 通过catId进行更新分类数据
// data:{data}解构出来的,修改的数据赋值给category
this.category.catId = data.category.catId;
this.category.name = data.category.name;
// 扩展默认菜单
this.category.parentCid = data.category.parentCid;
// this.category.catLevel = data.category.catLevel;
this.category.icon = data.category.icon;
// this.category.productUnit = data.category.productUnit;
this.category.productCount = data.category.productCount;
});
},
}
返回category对象
data() {
return {
category: {
name: "",
parentCid: "",
catLevel: "",
showStatus: 1,
sort: 0,
icon: "",
productUnit: "",
productCount: "",
},
......
};
},
editCategory()方法调用后端
更新完成后将category对象置为默认值
methods: {
editCategory() {
//es6语法
let { catId, name, icon, productCount } = this.category;
var data = {
catId: catId,
name: name,
icon: icon,
productCount: productCount
};
console.log("edit-data", data);
//发送修改请求
this.$http({
url: this.$http.adornUrl("/product/category/update"),
method: "post",
data: this.$http.adornData(data, false),
}).then(({ data }) => {
//关闭对话框
this.dialogFormVisible = false;
this.$message({
type: 'success',
message: '修改成功'
});
//刷新分类数据
this.getMenuList();
//操作完成后分类数据展开
this.expandKey = [this.category.parentCid];
// 属性恢复默认值
this.category.catId = null;
this.category.parentCid = 0;
this.category.catLevel = 0;
this.category.name = "";
this.category.icon = "";
this.category.productUnit = "";
this.category.productCount = 0;
});
},
cancel() {
this.dialogFormVisible = false;
this.category.catId = null;
this.category.parentCid = 0;
this.category.catLevel = 0;
this.category.name = "";
this.category.icon = "";
this.category.productUnit = "";
this.category.productCount = 0;
},
},
}
动态修改title
新增或编辑操作title动态显示
:title="title"
<el-dialog :title="title" :visible.sync="dialogFormVisible" width="30%" :close-on-click-modal="false">
/略......
</el-dialog>
data() {
return {
title:"",
//略......
};
},
methods: {
edit(data) {
this.title = "商品分类修改",
}
append(data) {
this.title = "商品分类新增",
},
//略......
}
点击 modal 关闭 Dialog
当我们不小心点到对话框外边,对话框会自动的关闭,解决如下
参考Attributes:
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
close-on-click-modal | 是否可以通过点击 modal 关闭 Dialog | boolean | — | true |
<el-dialog :title="title" :visible.sync="dialogFormVisible" width="30%" :close-on-click-modal="false">
//.....
</el-dialog>
前端代码
src\views\modules\product\category.vue
<!-- 三级分类页面 -->
<template>
<div>
<el-tree :data="menu" :props="defaultProps" show-checkbox :expand-on-click-node="false" node-key="catId"
:default-expanded-keys="expandKey">
<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>
<el-button type="text" size="mini" @click="edit(data)">Edit</el-button>
</span>
</span>
</el-tree>
<el-dialog :title="title" :visible.sync="dialogFormVisible" width="30%" :close-on-click-modal="false">
<el-form :model="category">
<el-form-item label="分类名称">
<el-input v-model="category.name"></el-input>
</el-form-item>
<el-form-item label="显示状态">
<el-input v-model="category.showStatus"></el-input>
</el-form-item>
<el-form-item label="排序">
<el-input v-model="category.sort"></el-input>
</el-form-item>
<el-form-item label="图标">
<el-input v-model="category.icon"></el-input>
</el-form-item>
<el-form-item label="计量单位">
<el-input v-model="category.productUnit"></el-input>
</el-form-item>
<el-form-item label="商品数量">
<el-input v-model="category.productCount"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">取 消</el-button>
<el-button type="primary" @click="choose">确 定</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
//这里可以导入其他文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)
//例如:import 《组件名称》 from '《组件路径》';
export default {
data() {
//这里存放数据
return {
title: "",
//区分新增和编辑的类型
dialogType: "",
dialogFormVisible: false,
category: {
name: "",
parentCid: "",
catLevel: "",
showStatus: 1,
sort: 0,
icon: "",
productUnit: "",
productCount: "",
},
menu: [],
expandKey: [],
defaultProps: {
children: "children",
label: "name"
},
};
},
//监听属性 类似于data概念
computed: {},
//监控data中的数据变化
watch: {},
//方法集合
methods: {
getMenuList() {
this.dataListLoading = true;
this.$http({
url: this.$http.adornUrl('/product/category/list/tree'),
method: 'get',
}).then(({ data }) => { // 1.使用解构即{},将data对象解构出来
console.log("获取三级分类数据:", data);
// 2.将data中的data赋值给menu
this.menu = data.data;
});
},
choose() {
if (this.dialogType == "add") {
this.addCategory();
} else if (this.dialogType == "edit") {
this.editCategory();
}
},
append(data) {
console.log("append", data);
this.title = "商品分类新增",
this.dialogType = "add";
this.dialogFormVisible = true;
this.category.parentCid = data.catId;
this.category.catLevel = data.cat_level * 1 + 1;
console.log("append this category:", this.category);
},
remove(node, data) {
var ids = [data.catId];
// 反引号是新标准es6的功能,可以用$配合大括号来实现变量拼接
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({
type: 'success',
message: '删除成功!'
});
this.getMenuList();
//默认菜单展示
this.expandKey = [node.parent.data.catId];
});
}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除'
});
});
console.log("remove", node, data);
},
// 添加分类
addCategory() {
this.$http({
url: this.$http.adornUrl("/product/category/save"),
method: "post",
data: this.$http.adornData(this.category, false)
}).then(({ data }) => {
this.dialogFormVisible = false;
this.$message({
type: 'success',
message: '新增成功'
});
//刷新分类数据
this.getMenuList();
this.expandKey = [this.category.parentCid];
}).catch({
type: 'info',
message: '已取消删除'
});
},
//修改分类
edit(data) {
console.log("要修改的分类数据对象为:", data);
this.title = "商品分类修改",
this.dialogFormVisible = true;
this.dialogType = "edit";
this.$http({
url: this.$http.adornUrl(`/product/category/info/${data.catId}`),
method: "get",
params: this.$http.adornParams({})
}).then(({ data }) => {
console.log("编辑分类得数据回显:", data);
// 通过catId进行更新分类数据
this.category.catId = data.category.catId;
this.category.name = data.category.name;
// 扩展默认菜单
this.category.parentCid = data.category.parentCid;
// this.category.catLevel = data.category.catLevel;
this.category.icon = data.category.icon;
// this.category.productUnit = data.category.productUnit;
this.category.productCount = data.category.productCount;
});
},
editCategory() {
let { catId, name, icon, productCount } = this.category;
var data = {
catId: catId,
name: name,
icon: icon,
productCount: productCount
};
console.log("edit-data", data);
//发送修改请求
this.$http({
url: this.$http.adornUrl("/product/category/update"),
method: "post",
data: this.$http.adornData(data, false),
}).then(({ data }) => {
//关闭对话框
this.dialogFormVisible = false;
this.$message({
type: 'success',
message: '修改成功'
});
//刷新分类数据
this.getMenuList();
//操作完成后分类数据展开
this.expandKey = [this.category.parentCid];
// 属性恢复默认值
this.category.catId = null;
this.category.parentCid = 0;
this.category.catLevel = 0;
this.category.name = "";
this.category.icon = "";
this.category.productUnit = "";
this.category.productCount = 0;
});
},
cancel() {
this.dialogFormVisible = false;
this.category.catId = null;
this.category.parentCid = 0;
this.category.catLevel = 0;
this.category.name = "";
this.category.icon = "";
this.category.productUnit = "";
this.category.productCount = 0;
},
},
//生命周期 - 创建完成(可以访问当前this实例)
created() {
this.getMenuList();
},
}
</script>
<style scoped></style>