我想在 html + thymeleaf 中生成可点击的表格行,但我遇到了以下问题。据我所知,它无法用链接(a-tag)包装 tr 元素,因为表格只能 直接包含 tr-subtags。所以我必须包装每个 td-tag 的内容,但这些值是由 thymeleaf 动态创建的!
将每一行(将每一行的每个 td-tag 链接)链接到生成的 url 的最佳方法是什么?是否有一些 th:text - 前缀/后缀功能?
<tr th:each="item : ${itmes}">
<td th:text="${{item.name}}">some name</td>
<td th:text="${{item.date}}">01.03.2014</td>
<td>author</td>
<td>2</td>
<td>
<a th:href="@{/backend/items/{id}(id=${item.id})}" href="show.html"
role="button" class="btn btn-default btn-circle">
<i class="fa fa-info"></i>
</a>
<a th:href="@{/backend/items/{id}/update(id=${item.id})}" role="button" class="btn btn-warning btn-circle">
<i class="fa fa-edit"></i>
</a>
</td>
</tr>
最佳答案
我不得不解决与 Tymeleaf 非常相似的问题,而且我还需要将请求参数从项目传递到 url,所以我这样解决了:
<tr th:each="item : ${itmes}" style="cursor: pointer"
th:onclick="'javascript:rowClicked(\'' + ${item.someField} + '\');'">
...
<td>Some data</td>
...
</tr>
然后以某种方式包含脚本:
<script>
function rowClicked(value) {
location.href = "/myurl?param=" + value;
}
</script>
关于html - Thymeleaf:可点击的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27484706/