前端:
<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 "视图路径";
}