thymeleaf遍历生成复杂的表格

前端:

 <table class="layui-table" id="tabRank">
     <tr>
         <th colspan="2">机构</th>
         <th colspan="2">年份</th>
         <th colspan="2">得分</th>
         <th colspan="2">全球排名</th>
     </tr>
     <div th:remove="tag" th:if="*{#lists.isEmpty(institution)}">
         <tr>
             <td colspan="8" style="text-align: center">无排名信息</td>
         </tr>
     </div>
     <div th:remove="tag" th:if="*{not #lists.isEmpty(institution)}" th:each="institution:${institution}">
         <tr>
             <td colspan="2" rowspan="4" th:text="${institution.institution}"></td>
             <tr th:each="rank:${schoolRank}" th:if="${rank.schoolRankInstitution}==${institution.institution}">
                 <td colspan="2" th:text="${rank.schoolRankYears}"></td>
                 <td colspan="2" th:text="${rank.schoolRankScore}"></td>
                 <td colspan="2" th:text="${rank.schoolRankGlobal}"></td>
             </tr>
         </tr>
     </div>
 </table>

th:remove="tag"
它在这的作用是生成表格后把div删除,但不删除子元素

th:if="*{#lists.isEmpty(institution)}"
判断从后台获取的数据为空,空则不渲染 tr 标签

th:if="*{not #lists.isEmpty(institution)}"
判断从后台获取的数据不为空,不为空则渲染 tr 标签

用到双重th:each,生成表格

在这里插入图片描述

后端:

@RequestMapping("请求路径")
    public String schoolView(ModelMap map,@PathVariable Long schoolId) {
        School school=schoolService.searchSchoolByschoolId(schoolId);
        List<SchoolRank> schoolRank = schoolService.searchSchoolRankBySchoolName(school.getSchoolName());
        List<Institution> institutions = schoolService.searchSchoolRankAsInstitutionBySchoolName(school.getSchoolName());
        map.put("schoolRank",schoolRank);
        map.put("school", school);
        map.put("institution", institutions);
        return "视图路径";
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值