谷粒商城之商品服务-三级分类(新增与修改)

目录

三级类目新增效果完成

三级类目修改效果完成


三级类目新增效果完成

需求:点击append按键弹出一个对话框

visible.sync:双向绑定,如果为true则显示对话框否则不显示

  

思路:当我们调用append方法时,就要将visible.sync修改为true,并且为category对象属性赋值,当我们点击确定按钮时就要调用新增函数请求调用后台处理

出现问题: 新增成功但是有些字段没有值

控制台打印category对象,发现有值

问题原因:发现自动绑定过程失败,前端发送的category对象属性名称与后台方法的参数对象属性名称不一致

解决方案:将后台方法的参数对象属性名称与前端对象的属性名称保持一致

 

<!--  -->
<template>
  <!-- 使用v-bind/':'才能为属性赋值成功 -->
  <div>
    <el-tree
      show-checkbox
      :data="menu"
      :props="defaultProps"
      :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>
          <!--箭头函数调用append方法-->
          <!-- 一级目录和二级目录才允许append -->
          <el-button
            v-if="node.level <= 2"
            type="text"
            size="mini"
            @click="() => append(data)"
          >
            Append
          </el-button>
          <!-- 没有子节点才允许delete -->
          <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="dialogFormVisible"
      width="30%"
    >
      <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="addCategory">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    //这里存放数据
    return {
      category: {
        name: "",
        parentCid:"",
        catLevel:"",
        showStatus: 1,
        sort: 0,
        icon: "",
        productUnit: "",
        productCount: "",
      },
      dialogFormVisible: false,
      menu: [],
      expandKey: [],
      defaultProps: {
        children: "children", //封装子节点,prop会自动进行一个遍历
        label: "name", //显示数据,prop会自动进行一个遍历
      },
    };
  },
  methods: {
    //查询分类
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/product/category/list/tree"),
        method: "get",
      }).then(({ data }) => {
        // 1.使用解构即{},将data对象解构出来
        // 2.将data中的data赋值给menu
        this.menu = data.data;
        // 3.将data中的data对象的属性children和name赋值给defaultProps中的children和label
      });
    },
    append(data) {
      console.log("append", data);
      this.dialogFormVisible = "true";
      this.category.parentCid = data.catId;
      this.category.catLevel = data.catLevel * 1 + 1;
      console.log(this.category);
    },
    //删除分类
    remove(node, data) {
      let catId = [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(catId, false),
          }).then(({ data }) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            // 刷新菜单
            this.getDataList();
            // 默认菜单展示
            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.getDataList();
        // 默认菜单展示
        this.expandKey = [this.category.parentCid];
      });
    },
  },

};

三级类目修改效果完成

需求:当我们点击修改按钮时,会进行一个数据的修改

我们要点击edit出现一个对话框,需要进行一个对话框的复用 

有一个问题:我们点击确定调用的是添加方法 ,因此,我们需要设定一个对话框的类型

 

问题:没有数据的回显 ,那能不能用slot传进来的data呢?

显然是不行的,这是上一次查询的数据,高并发的情况下数据很可能发生了变化

使用此方法查询单个类目的信息

 将查询的数据进行一个打印 

进行一个信息的回显

 更新的是通过catId进行更新的所有catId是必须的

因为,扩展默认菜单要用parentCid需要补上

出现问题:

Uncaught (in promise) TypeError: _this4.editCategory is not a function

出现原因:editCategory没有写在methods中

问题现象:回显类目的信息在新增也会出现

问题解决:更新完成后将category对象置为默认值

editCategory() {
      // 将category中的属性解构成对象,将解构的对象赋值给新的对象的属性,如果属性与对象名相同则':对象名'可以省略->let data={catId,name,icon,productUnit}
      var { catId, name, icon, productUnit } = this.category;
      var data = {
        catId: catId,
        name: name,
        icon: icon,
        productUnit: productUnit,
      };
      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.getDataList();
        // 默认菜单展示
        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

 

需求: 当我们不小心点到对话框外边,对话框会自动的关闭,非常不友好

解决方案:将点击关闭模式关闭

 

<!--  -->
<template>
  <!-- 使用v-bind/':'才能为属性赋值成功 -->
  <div>
    <el-tree
      show-checkbox
      :data="menu"
      :props="defaultProps"
      :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>
          <!--箭头函数调用append方法-->
          <!-- 一级目录和二级目录才允许append -->
          <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>
          <!-- 没有子节点才允许delete -->
          <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="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.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="cancel">取 消</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: {},
  props: {},
  data() {
    //这里存放数据
    return {
      title: "",
      dialogType: "",
      category: {
        catId: null,
        name: "",
        parentCid: "",
        catLevel: "",
        showStatus: 1,
        sort: 0,
        icon: "",
        productUnit: "",
        productCount: "",
      },
      dialogFormVisible: false,
      menu: [],
      expandKey: [],
      defaultProps: {
        children: "children", //封装子节点,prop会自动进行一个遍历
        label: "name", //显示数据,prop会自动进行一个遍历
      },
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    //查询分类
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/product/category/list/tree"),
        method: "get",
      }).then(({ data }) => {
        // 1.使用解构即{},将data对象解构出来
        // 2.将data中的data赋值给menu
        this.menu = data.data;
        // 3.将data中的data对象的属性children和name赋值给defaultProps中的children和label
      });
    },
    // 新增类目
    append(data) {
      console.log("append", data);
      this.title = "商品分类新增";
      this.dialogType = "add";
      this.dialogFormVisible = "true";
      this.category.parentCid = data.catId;
      this.category.catLevel = data.catLevel * 1 + 1;
      console.log(this.category);
    },
     //添加分类
    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.getDataList();
        // 默认菜单展示
        this.expandKey = [this.category.parentCid];
      });
    },
    // 修改分类
    edit(data) {
      this.title = "商品分类修改";
      console.log("edit", data);
      this.dialogType = "edit";
      this.dialogFormVisible = true;
      this.$http({
        url: this.$http.adornUrl(`/product/category/info/${data.catId}`),
        method: "get",
        params: this.$http.adornParams({}),
      }).then(({ data }) => {
        /* 
         catId:"",
        name: "",
        parentCid: "",
        catLevel: "",
        showStatus: 1,
        sort: 0,
        icon: "",
        productUnit: "",
        productCount: "",
          */
        console.log("data", data);
        this.category.catId = data.category.catId;
        this.category.name = data.category.name;
        this.category.parentCid = data.category.parentCid;
        this.category.icon = data.category.icon;
        this.category.productUnit = data.category.productCount;
        console.log("category", this.category);
      });
    },
    editCategory() {
      // 将category中的属性解构成对象,将解构的对象赋值给新的对象的属性,如果属性与对象名相同则':对象名'可以省略->let data={catId,name,icon,productUnit}
      var { catId, name, icon, productUnit } = this.category;
      var data = {
        catId: catId,
        name: name,
        icon: icon,
        productUnit: productUnit,
      };
      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.getDataList();
        // 默认菜单展示
        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;
      });
    },
     //删除分类
    remove(node, data) {
      let catId = [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(catId, false),
          }).then(({ data }) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            // 刷新菜单
            this.getDataList();
            // 默认菜单展示
            this.expandKey = [node.parent.data.catId];
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      console.log("remove", node, data);
    },
    submitData() {
      if (this.dialogType == "add") {
        this.addCategory();
      }
      if (this.dialogType == "edit") {
        this.editCategory();
      }
    },
    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.getDataList();
  },
  //生命周期 - 挂载完成(可以访问DOM元素)
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能,这个函数会触发
};
</script>
<style  scoped>
</style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值