Thymeleaf: clickable row

不羁的心 提交于 2019-12-07 03:09:58

问题


I want to generate clickable table rows within html + thymeleaf, but I have the following problem. AFAIK it's not able to wrap a tr element with a link (a-tag), because a table can only directly contain tr-subtags. So I have to wrap the content of each td-tag, but these values are dynamically created by thymeleaf!

What would be the best approach to link each row (link each td-tag of each row) to a generated url? Is there some th:text - prefix/suffix functionality?

<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>

回答1:


The least problematic way to do this is using javascript to create each row clickable.

for e.g.

$("#yourtablename tr").click(function() {
            //do more javascript code to meet your needs
      });

Personally i would attach a href to one of the tds then do something like below:

$("#yourtablename tr").click(function() {
                window.location = $(this).find('td:eq(5)').attr("href");
          });

Hope that helps




回答2:


I had to solve pretty similar problem with Tymeleaf, and I've also been needed to pass request parameter from item to the url, so I solved like this:

<tr th:each="item : ${itmes}" style="cursor: pointer"
     th:onclick="'javascript:rowClicked(\'' + ${item.someField} + '\');'">
    ...
    <td>Some data</td>
    ...
</tr>

then include somehow the script:

<script>
    function rowClicked(value) {
        location.href = "/myurl?param=" + value;
    }
</script>


来源:https://stackoverflow.com/questions/27484706/thymeleaf-clickable-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!