【问题标题】:Create a table in thymeleaf在百里香中创建一个表
【发布时间】:2014-08-28 03:26:24
【问题描述】:

我是 thymeleaf 的新手,正在尝试使用数组和 each 循环制作一个简单的表格。

我的代码如下所示:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Smoke Tests</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<table border="1" style="width:300px">
    <tr>
        <td>Test Name</td>
    </tr>
    <tr th:each="smokeTest : ${smokeTests}">
        <td>
            th:text="${smokeTest.name}">A Smoke Test'
        </td>
    </tr>
</table>
</body>
</html>

基本上我的问题是我不能在&lt;tr&gt;s 内以&lt;td&gt;s 的身份运行循环。这段代码有什么办法可以工作吗?

【问题讨论】:

    标签: html html-table each thymeleaf


    【解决方案1】:

    首先想到的简单解决方案:

    <th:block th:each="smokeTest : ${smokeTests}">
        <tr>
            <td th:text="${smokeTest.name}">A Smoke Test'</td>
        </tr>
    </th:block>
    

    详情:http://www.thymeleaf.org/whatsnew21.html#bloc

    【讨论】:

    • 这不起作用,因为 th:text 与循环无关。
    【解决方案2】:

    你必须把 th:text 作为标签的属性,所以

    <tr th:each="smokeTest : ${smokeTests}">
       <td th:text="${smokeTest.name}">A Smoke Test'</td>
    </tr>
    

    应该运行。

    【讨论】:

      【解决方案3】:

      虽然,这是迟到的答案。 它的工作更具体,比如

      <tr th:each="smokeTest : ${smokeTests}">
         <td><p th:text="${smokeTest.name}"></p></td>
      </tr>
      

      【讨论】:

        猜你喜欢
        • 2020-12-30
        • 2019-01-10
        • 2018-07-08
        • 2017-06-29
        • 1970-01-01
        • 1970-01-01
        • 2018-09-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多