springboot+thymeleaf遍历所有大类以及大类下的小类

控制层

 /**
     * 导航
     *
     * @param model
     * @return
     */
    @RequestMapping("/navs")
    public ModelAndView navs(Model model) {
//        List<Map<String, List<Platform>>> list = dicTypeService.showAllPlatfrom();
        //        model.addAttribute("platfrom", list);
        List<Map<String, Object>> dicTypes = dicTypeService.showAllAndCount();
        model.addAttribute("area", dicTypes);
        return new ModelAndView("navs");
    }

Service层

 //遍历导航
    public List<Map<String, Object>> showAllAndCount() {
    //大类查询
        List<Map<String, Object>> allp = dicTypeMapper.showAllAndCount();
        for (Map<String, Object> map : allp) {
         //小类查询
            String pid = (String) map.put("pid", map.get("pid"));
            List<Map<String, Object>> platforms = platformMapper.selectByArea(pid);
            for (Map<String, Object> p : platforms) {
            //小类信息多表封装
                String rid = (String) p.put("gid", p.get("gid"));
                DbPicture dbPicture = dbPictureMapper.selectByRid(rid);
                if (dbPicture == null) {
                //添加到小类
                    p.put("pic", "");
                } else {
                //小类添加到大类下
                    p.put("pic", dbPicture.getUrl());
                }
            }
            map.put("platfrom", platforms);
        }
        return allp;
    }

前端

 <th:block th:each="areas : ${area}">
        <!--        循环地区-->
        <div class="result">
            <h3>
                <th:block th:text="${areas.name}">
                </th:block>
                (
                <th:block th:text="${areas.count}">
                </th:block>
                )
            </h3>
            <div class="item_box">
                <!--        循环地区下的交易所-->
                <th:block th:each="are : ${areas.platfrom}">
                    <a th:href="@{/nav-details(gid=${are.gid})}" target='_blank'>
                        <img th:src="${are.pic}" alt=''>
                        <p class="xcx">
                            <th:block th:text="${are.name}">
                            </th:block>
                        </p>
                    </a>
                </th:block>
            </div>
            <div class="more">
                <p>展开更多</p>
                <img src="../images/arrows-bottom.svg" alt="">
            </div>
        </div>
    </th:block>

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

发布了62 篇原创文章 · 获赞 21 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_40618664/article/details/100930673