您当前的位置: 首页 >  前端

java持续实践

暂无认证

  • 3浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

谷粒商城51- 前端-商品服务-API-三级分类-删除-批量删除&小结

java持续实践 发布时间:2020-08-09 11:36:06 ,浏览量:3

文章目录
      • 需求
      • 代码实现
      • 测试

需求

页面上新增批量删除的按钮, 把被选中的分类进行批量的删除

代码实现

组件的 树形控件 中, 有getCheckedNodes , 用于获取所有被选中的节点信息. 方法的调用示例如下图, 用ref声明组件 this. r e f s 代 表 当 前 v u e 实 例 里 面 所 有 的 组 件 t h i s . refs 代表当前vue实例里面所有的组件 this. refs代表当前vue实例里面所有的组件this.refs.tree 调用组件. this.$refs.tree.filter(val); 用于方法的调用. 在页面上声明一个 批量删除的button. 并且在rl-tree中, 声明ref="menuTree" 点击批量删除时, 触发的batchDelete() 方法如下. 通过this.$refs.menuTree.getCheckedNodes(); 获取所有选中的节点的对象数组信息 . 把此数组遍历 , 获取其id ,放入数组中, 用于传递后端的接口参数去. catNames 用于给出提示, 选中了哪些要删除的菜单的名称. 给出弹框, 提示是否要删除. 完整代码如下



  
    
    批量保存
    批量删除

    
      

      
        
        {{ node.label }}
        
           箭头函数 调用指定的方法 -->
          Append

          
          update

          Delete
        
      
    

    
      
        
          
        

        
          
        

        
          
        
      

      
        取 消
        确 定
      
    
  



//这里可以导入其他文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)
//例如:import 《组件名称》 from '《组件路径》';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},

  data() {
    return {
      //是否能进行拖拽
      draggable: false,
      pCid: [],
      //拖拽后, 要修改的节点信息数组信息
      updateNodes: [],
      // 存储树的最大深度
      maxLevel: 0,
      // 弹框的标题
      title: "",
      // 操作类型 是修改还是新增
      dialogType: "",
      //表单的值
      category: {
        name: "",
        parentCid: 0,
        catLevel: 0,
        showStatus: 1,
        sort: 0,
        catId: null,
        productUnit: "",
        icon: "",
      },
      dialogVisible: false,
      menus: [],
      expandedkey: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
    };
  },
  methods: {
    getMenus() {
      this.$http({
        url: this.$http.adornUrl("/product/category/list/tree"),
        method: "get",
      }).then(({ data }) => {
        console.log("成功获取到菜单数据 .... ", data.data);
        this.menus = data.data;
      });
    },

    // 能否进行拖拽
    allowDrop(draggingNode, dropNode, type) {
      // 1. 被拖动的当前节点 以及所在的父节点总层数不能大于3

      // 1) 被拖动的当前节点的深度
      console.log("allowDrop:   ", draggingNode, dropNode, type);

      //递归计算 当前正在拖动的节点深度, 存储到maxLevel 中
      this.countNodeLevel(draggingNode);

      //  2) 当前正在拖动的节点深度+ 父节点所在的深度 不大于3
      console.log("当前正在拖动节点的深度:  ", this.maxLevel);

      // 实际的深度
      let deep = Math.abs(this.maxLevel - draggingNode.level) + 1;
      console.log("深度:", deep);

      if (type == "inner") {
        //打印拖到内部时的 深度
        console.log(
          `this.maxLevel:${this.maxLevel};draggingNode.data.catLevel:${draggingNode.data.catLevel};dropNode.level:${dropNode.level}`
        );

        // 把某个菜单, 拖进某个菜单里面的时候, 需要进行当前节点的深度和被拖进去的节点的深度相加是否大于3
        return deep + dropNode.level  {
        this.$message({
          message: "菜单顺序等修改成功",
          type: "success",
        });
        //刷新出新的菜单
        this.getMenus();
        //设置需要默认展开的菜单
        this.expandedKey = this.pCid;
        this.updateNodes = [];
        this.maxLevel = 0;
        // this.pCid = 0;
      });
    },

    //拖拽成功后 触发的方法
    handleDrop(draggingNode, dropNode, dropType, ev) {
      console.log("handleDrop: ", draggingNode, dropNode, dropType);

      //1、当前节点最新的父节点id
      let pCid = 0;
      let siblings = null;

      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  0) {
        for (let i = 0; i  {
        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;
      });
    },

    //点击确定后,发送修改分类数据
    editCategory() {
      //修改发送的数据.  注意只提交要修改的数据和id , 不能提交其他数据, 否则数据库中会修改其他数据为默认值,导致数据丢失.
      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({
          type: "success",
          message: "分类修改成功!",
        });

        this.getMenus();
        this.expandedkey = [this.category.parentCid];
        this.category.name = "";
        this.category.productUnit = "";
        this.category.icon = "";
        this.dialogVisible = false;
      });
    },
    //添加分类
    addCategory() {
      //点击添加分类,  关闭表单
      this.dialogVisible = false;
      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({
          type: "success",
          message: "分类添加成功!",
        });

        this.getMenus();
        this.expandedkey = [this.category.parentCid];
        this.category.name = "";
      });
    },

    // 点击树形的 append, 触发的方法, data为点击的父节点数据
    append(data) {
      console.log("append ", data);

      this.dialogType = "add";

      this.title = "添加分类";
      //点击 append 弹出添加分类的对话框
      this.dialogVisible = true;
      //父级的id
      this.category.parentCid = data.catId;
      //分类的层级, 当前层级, 加一
      this.category.catLevel = data.catLevel * 1 + 1;

      //赋初始化值
      this.category.name = "";
      this.category.productUnit = "";
      this.category.icon = "";
      this.category.catId = null;
      this.category.sort = 0;
    },

    //批量删除
    batchDelete() {
      let catIds = [];
      let catNames = [];
      let checkedNodes = this.$refs.menuTree.getCheckedNodes();
      console.log("被选中的元素", checkedNodes);
      for (let i = 0; i  {
          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(() => {});
    },

    remove(node, data) {
      //console.log("remove ", node, data);

      this.$confirm(`是否删除 [ ${data.name} ] 菜单?`, "提示", {
        confirmButtonText: "确定", // 点击确定, 触发then
        cancelButtonText: "取消", // 点击取消 触发 catch ,由于取消不需要任何的操作, 但删除catch会报错, 因此保留空方法
        type: "warning",
      })
        .then(() => {
          var idArr = [data.catId];

          this.$http({
            url: this.$http.adornUrl("/product/category/delete"),
            method: "post",
            data: this.$http.adornData(idArr, false),
          }).then(({ data }) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            //expandedkey = node.parent.data.catId;
            this.getMenus();
            this.expandedkey = [node.parent.data.catId];
          });
        })
        .catch(() => {});
    },
  }
 
};



测试

选中某些分类, 点击批量删除 发送的数据如下 . 把id作为数组传递过去了. 后端打印的sql如下, 由于是逻辑删除, 因此使用的是update 页面上可以看到被删除了.

关注
打赏
1658054974
查看更多评论
立即登录/注册

微信扫码登录

0.1787s