是用thymeleaf中,在th:each循环中,怎么能够在事件里拿到一行的id值?
好久没见过这个东西了.
我记得点击事件也是可以用th表述的,忘了具体是什么,可以试一下(th:onclick),
然后传值时只能传基本类型, 是可以拼接进去的, 具体忘了, 很早了, 百度下应该都有
这个属性非常常用,比如从后台传来一个对象集合那么就可以使用此属性遍历输出,它与 JSTL 中的<c: forEach>类似,此属性既可以循环遍历集合,也可以循环遍历数组及 Map
th:each="user , iterStat : ${userList}"
或者th:each="user : ${userList}"
index
:当前迭代对象的 index(从 0 开始计算)count
:当前迭代对象的个数(从 1 开始计算) 这两个用的较多,
“隔开,第二个参数和第三个参数之间使用”:
"隔开<h1>遍历集合或者数组</h1>
<div th:each="user,userStat:${userList}">
<span th:text="${userStat.index}"></span>
<span th:text="${user.id}"></span>
<span th:text="${user.name}"></span>
</div>
<h1>遍历map,每个元素就是一个键值对,通过key和value来取出键或者值</h1>
<tr th:each="userMap:${userMaps}">
<td th:text="${userMap.key}"></td>
<td th:text="${userMap.value}"></td>
<td th:text="${userMap.value.name}"></td>
</tr>
可以使用th:attr获取当前行的ID值,具体步骤如下: 1.在th:each循环中添加th:attr进行ID绑定,例如:th:attr="data-id=${row.id}" 2.在事件中通过jquery获取当前行的ID值,例如:var id = $(this).closest("tr").attr("data-id"); 具体代码示例如下:
<tr th:each="row : ${list}" th:attr="data-id=${row.id}">
<td th:text="${row.id}"></td>
<td th:text="${row.name}"></td>
<td><button class="btn btn-primary" onclick="getRowId()">获取ID</button></td>
</tr>
<script>
function getRowId() {
var id = $(this).closest("tr").attr("data-id");
alert(id);
}
</script>