8. 谷粒商城树形列表添加过滤、属性分组查询全部、级联选择器

树形列表添加过滤

src\views\modules\common\category.vue

添加了树形菜单过滤,但是清空过滤条件时,节点无法收起

在这里插入图片描述

在原有的基础上修改

template

<!-- 树形列表 -->
<template>
  <div>
    <el-input placeholder="输入关键字进行过滤" v-model="filterText"></el-input>
    <el-tree
      ref="categoryTree"
      :filter-node-method="filterNode"
      :highlight-current="true"
      style="margin-top: 5px"
    ></el-tree>
  </div>
  <!-- 添加点击事件 -->
</template>

script

  data() {
    return {
      filterText: '',
    }
  },
  
  watch: {
    filterText(val) {
      this.$refs.categoryTree.filter(val)
    },
  },
  
  methods: {
    //树节点过滤
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
  },        

属性分组添加查询全部

src\views\modules\product\attrgroup.vue

改动的较多,建议直接拷贝

添加级联选择器

新增分组中,选择属性分类,使用级联选择器

在这里插入图片描述

前端

src\views\modules\product\attrgroup-add-or-update.vue

template

      <el-form-item label="所属分类id" prop="categoryId">
        <!-- filterable 打开搜索功能 -->      
        <el-cascader
          v-model="dataForm.categoryPath"
          :options="categoryList"
          :props="props"
          filterable
        ></el-cascader>
      </el-form-item>

script

  data() {
    return {
      props: {
        value: 'catId',
        label: 'name',
        children: 'childrenList',
      },
      categoryList: [],
    }
  },

methods:

    // 关闭对话框时,清空所选分类
    dialogClose() {
      this.dataForm.categoryPath = []
    },

在这里插入图片描述
在这里插入图片描述

级联选择器回显

修改属性分组时,根据分类id,回显所属分类

在这里插入图片描述

后端

entity

CategoryEntity.java

    @JsonInclude(Include.NON_EMPTY) // 为空则不返回这个字段
    private List<CategoryEntity> childrenList;

AttrGroupEntity.java

	/**
	 *	分类的全路径
	 */
	@TableField(exist = false)
	private Long[] categoryPath;

controller

AdminAttrGroupController.java

    @Autowired
    private CategoryService categoryService;
    
	/**
     * 信息
     */
    @GetMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long categoryId = attrGroup.getCategoryId();
        Long[] path = categoryService.findCategoryPath(categoryId);
        attrGroup.setCategoryPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

service

CategoryService.java

    /**
     * 根据分类id,查找分类id的全路径
     * @param categoryId
     * @return
     */
    Long[] findCategoryPath(Long categoryId);

CategoryServiceImpl.java

    @Override
    public Long[] findCategoryPath(Long categoryId) {
        List<Long> pathList = new ArrayList<>();

        List<Long> parentPathList = findParentPath(categoryId,pathList);
        // 将数据反转过来
        Collections.reverse(parentPathList);
        return pathList.toArray(new Long[parentPathList.size()]);
    }

    /**
     *     225,25,2
     *     只要父节点不为0,就说明有父级,
     *     则继续往上找
     */

    private List<Long> findParentPath(Long categoryId, List<Long> pathList) {
        // 1、收集当前节点id
        pathList.add(categoryId);
        // 先根据id,查出当前分类的信息
        CategoryEntity category = this.getById(categoryId);
        if (category.getParentCid() != 0){
            // 如果父id不为0,则说明有父级,继续往上查
            findParentPath(category.getParentCid(),pathList);
        }

        // 最后查到1级分类,父id为0了,则会返回整个列表
        return pathList;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值