thymeleaf+SpringMVC 疑问解惑

qq_36268672 2018-12-27 11:25:06
问题描述:
controller 返回从DB查的数据List<obj>
假设该List里数据格式为
index0 A,1,2,3
index1 A,4,5,6
index2 A,7,8,9
index3 A,1,2,3
index4 B,10,11,12
index5 C,1,4,5
index6 D,1,2,3
页面预呈现
A
1 2 3
4 5 6
7 8 9
1 2 3
B
10 11 12
C
1 4 5
D
1 2 3

html代码如下:
                          <tbody id="resultData" >
<tr th:each="result, stat : ${resultList}">
<td th:if="判定条件(業者の場合)" th:text="${result.companySearchName}"><input type="checkbox" class="check-row check-g1"></td><!--業者名 -->
<td th:if="判定条件(業者の場合)" th:text="${result.spec}"></td><!-- 仕様 -->
<td th:if="判定条件(業者の場合)" th:text="${result.quantity}"></td><!-- 数量 -->
<td th:if="判定条件(業者の場合)" th:text="${result.quantityUnitName}"></td><!-- 単位 -->
<td th:if="判定条件(業者の場合)" class="right-position"><input type="text" class="no-border input-money-yen" th:text="${result.unitPriceBudget}" readonly></td><!-- 単価 -->
<td th:if="判定条件(業者の場合)" class="right-position"><input type="text" class="no-border input-money-yen" th:text="${result.amount}" readonly></td><!-- 金額 -->
<td th:if="判定条件(業者の場合)" th:text="${result.orderedFlag}"></td><!-- 発注済を非表示-->

<td th:if="判定条件(品目の場合)" class="check-row check-g1-row" ><input type="checkbox" class="check-all">[[${result.goodsName}]]</td><!-- 品名 -->
<td th:if="判定条件(品目の場合)" th:text="${result.spec}"></td><!-- 仕様 -->
<td th:if="判定条件(品目の場合)" th:text="${result.quantity}"></td><!-- 数量 -->
<td th:if="判定条件(品目の場合)" th:text="${result.quantityUnitName}"></td><!-- 単位 -->
<td th:if="判定条件(品目の場合)" class="right-position"><input type="text" class="no-border input-money-yen" th:text="${result.unitPriceBudget}" readonly></td><!-- 単価 -->
<td th:if="判定条件(品目の場合)" class="right-position"><input type="text" class="no-border input-money-yen" th:text="${result.amount}" readonly></td><!-- 金額 -->
<td th:if="判定条件(品目の場合)" th:text="${result.orderedFlag}"></td><!-- 発注済を非表示-->
</tr>
</tbody>


该代码呈现出来结果是这样



想要的是这样:


描述: 已经知道 LIST里的数据是从DB里查出来的,而且有顺序,
根据model里的 字母ID(也就是上图的ABC) 把从属于他们的数据汇总到一起
在画面中 字母字段 独占 一行。 从下一行 的首个方格开始排列从属于它们的数据
使用模板:thymeleaf。 返回数据是呈现在<TBODY>标签里。循环在<TR>里循环。SQL文 controller 皆可改动。

希望赐教:
代码不在身边,明天可以附上代码,越详细越好,越简单越好。 求解药。
...全文
102 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
另外前端要做处理也不难, 其实你的思维固化了,我给你思维疏导疏导 比如:你先输出满足A的哪一行,那么就是tr 做判断处理,如果满足条件A,那么A那一行都是空的,然后再遍历 集合,输出满足A的所有行。 依次类推,输出所有。 但是我个人建议还是对数据格式先进行处理,这样你数据结构清晰了,其实前端循环两次展示即可解决!
  • 打赏
  • 举报
回复
这个简单啊,其实你觉得复杂,应该在后端把数据处理一下再返回的 比如你现在的数据是List。 你可以处理成Map<String, List<Object>> 返回。 Map的key就是 你的a、b、c、d 等等 ,value就是你的key的List对象。 至于写法,去copy吧https://www.cnblogs.com/cxxjohnson/p/6395665.html
十八道胡同 2018-12-28
  • 打赏
  • 举报
回复
初步研究了下

是否可以用循环的方式来解决,后台数据分别写3个List就好
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<!-- 不存在则忽略,显示hello null!(可以通过默认值进行设置)-->
<p th:text="'Hello ' + (${name}?:'admin')">3333</p>
<table>
<tr>
<th>ID</th>
<th>NAME</th>
<th>AGE</th>
</tr>
<tr th:each="emp : ${empList}">
<td th:text="${emp.id}">1</td>
<td th:text="${emp.name}">海</td>
<td th:text="${emp.age}">18</td>
</tr>
</table>
</body>
</html>


@GetMapping(value = "/hello")
public String hello(Model model) {
List<Emp> empList = new ArrayList<>();
empList.add(new Emp(1, "校长", 24));
empList.add(new Emp(2, "书记", 28));
empList.add(new Emp(3, "小海", 25));
model.addAttribute("empList", empList);
return "hello";
}

HelloController


https://www.cnblogs.com/jiangbei/p/8462294.html
十八道胡同 2018-12-28
  • 打赏
  • 举报
回复
不会前端,帮你顶下
qq_36268672 2018-12-28
  • 打赏
  • 举报
回复
此贴依然有效

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧