三级联动列表展示

  1. 由于业务的需求我们需要做成一个三级分类的列表并展示出来,类似于京东首页上的三级菜单栏。
  2. 具体的业务代码如下
    •   首先在实体类定义一个list集合来存储我们的子类信息(这里的jsoninclude注解是对应的当这个类别下没有数据时,这个字段就不会显示出来)

 

    •   代码如下

 

复制代码
 1  //分类组装
 2     @Override
 3     public List<CategoryEntity> tree() {
 4         //1.查询所有数据
 5         List<CategoryEntity> list = baseMapper.selectList(null);
 6         //2.查询出所有分类的子分类
 7         List<CategoryEntity> entityList = list.stream().filter(categoryEntity ->{
 8             return categoryEntity.getParentCid() == 0;
 9         }).map((meun)->{
10             //组装查询对象
11             meun.setChild(getChildren(meun,list));
12             return meun;
13         }).sorted((m1, m2) -> {
14             return (m1.getSort() == null ? 0 : m1.getSort()) - (m2.getSort() == null ? 0 : m2.getSort());
15         }).collect(Collectors.toList());
16         return entityList;
17     }
18 
19 
20     /*
21       递归查询所有的子级列表
22       root 表示当前数据
23       all 表示所有数据
24     */
25     private List<CategoryEntity> getChildren(CategoryEntity root,List<CategoryEntity> all){
26         List<CategoryEntity> entities = all.stream().filter(categoryEntity -> {
27             //表示当前数据的父节点等于分类id
28             return categoryEntity.getParentCid() == root.getCatId();
29         }).map(categoryEntity -> {
30             //应用递归查找
31             categoryEntity.setChild(getChildren(categoryEntity, all));
32             return categoryEntity;
33             //子级排序
34         }).sorted((m1, m2) -> {
35             return (m1.getSort()==null?0:m1.getSort()) - (m2.getSort()==null?0:m2.getSort());
36         }).collect(Collectors.toList());
37         return entities;
38     }
复制代码

 

posted @   fu!!!  阅读(74)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示