8

HTMLテンプレートを使用して行テーブルのリストとして表示したい「製品」のリストがあります。

html テンプレートは次のようになります。

<tr th:fragment="productTemplate">
    <td th:text="${productName}">product name</td>
    <td th:text="${productprice}>product price</td>
</tr>

これが私がしたことです:

<table>
  <tr th:each="product : ${products}" th:substituteby="product :: productTemplate" th:with="productName=*{name}, productPrice=*{price}" />
</table>

を 使用th:includeし た 場合 , それぞれ に 入れ子 が あり ます使用 し たtr 場合th:substituteby, 代替 が 優先th:each

ループ項目を別のものに置き換える方法が見つかりません。

誰かがこれを行うための解決策を持っていますか?

4

2 に答える 2

13

わかった:

<table>
  <tr th:each="product : ${products}" th:include="product :: productTemplate" 
      th:with="productName=${product.name}, productPrice=${product.price}" 
      th:remove="tag" />
</table>

そして、ここで、テンプレート クラスをtr要素に保持できます (これは私が望んでいたことです)。

<tbody th:fragment="productTemplate">
  <tr class="my-class">
    <td th:text="${productName}">product name</td>
    <td th:text="${productPrice}">product price</td>
  </tr>
</tbody>

結果は次のとおりです。

<table>
  <tr class="my-class">
    <td>Lettuce</td>
    <td>12.0</td>
  </tr>
  <tr class="my-class">
    <td>Apricot</td>
    <td>8.0</td>
  </tr>
</table>

公式 thymeleaf フォーラムdanielfernandezに感謝します

于 2013-06-18T09:50:33.453 に答える