【问题标题】:Thymeleaf- Create dynamic id for table rowThymeleaf - 为表行创建动态 id
【发布时间】:2017-08-26 16:11:29
【问题描述】:

我是 Thymeleaf 的新手,我正在尝试为 tr 创建 id 并获取动态行。

成功获取表行,但我不知道如何为 Thymeleaf 中的每一行创建 id。

<table class="table table-hover" id="table">
  <thead style="background-color:#CCE5FF">
  <tr>
    <th>ID</th>
    <th>Code</th>
    <th>Created Date</th>
    <th></th>
  </tr>
  </thead>
  <tbody>
  <tr th:each="emp,iterStat : ${empList}">
    <td th:text="${emp.id}">ID</td>
    <td th:text="${emp.mdrcode}">Code</td>
    <td th:text="${emp.createDate}">Created Date</td>
    <td>
      <a id="editview" class="btn btn-sm btn-default" th:href="@{/}"><i class="fa fa-edit"></i> View</a>
    </td>
  </tr>
  </tbody>
</table>

【问题讨论】:

    标签: spring-boot thymeleaf


    【解决方案1】:

    您可以为此使用th:id

    <!-- this would assign the emp.id to the id attribute of the tr.
    <tr th:id="${emp.id}" th:each="emp,iterStat : ${empList}">
        <td th:text="${emp.id}">ID</td>
        <td th:text="${emp.mdrcode}">Code</td>
        <td th:text="${emp.createDate}">Created Date</td>
        <td><a  id="editview" class="btn btn-sm btn-default" th:href="@{/}"><i class="fa fa-edit"></i> View</a></td>
    </tr>
    

    我们也可以给id添加一些文字:

    <!-- this would assign someText + emp.id to the id attribute of the tr.
    <tr th:id="'someText' + ${emp.id}" th:each="emp,iterStat : ${empList}">
        <td th:text="${emp.id}">ID</td>
        <td th:text="${emp.mdrcode}">Code</td>
        <td th:text="${emp.createDate}">Created Date</td>
        <td><a  id="editview" class="btn btn-sm btn-default" th:href="@{/}"><i class="fa fa-edit"></i> View</a></td>
    </tr>
    

    【讨论】:

    • 谢谢它工作正常。但是使用 jquery 是否有可能在警报框中显示此内容?
    • 你可以为那些tr标签添加一个类,添加一个点击监听器并使用.attr('id')获取id
    • 这不是最佳实践。此解决方案存在安全漏洞,因为 id 对黑客可见。
    • @ImaMiri 你能说得更具体些吗?
    • @TommySchmidt 您可以用这种方式编写教程,但对于实际应用程序,您应该使用 randomId 或类似的东西。因为 Id 通常是连续的,容易受到 xss 攻击。
    猜你喜欢
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    相关资源
    最近更新 更多