2016-05-13 516 views
1

開始於Thymeleaf並有一個問題。我有Thymeleaf th:text如何將靜態文本添加到動態值

<tr th:each="it,row : ${res.answers}"> 
<td th:class="${row.even}? 'even' : 'odd'" th:text="${row.count}"> 
    1 
</td> 
<td th:class="${row.even}? 'even' : 'odd'" th:text="${it.question}"> 
    Value1 
</td> 
<td th:class="${row.even}? 'even' : 'odd'" th:text="${it.correctAnswer}"> 
    col2 
    <a href="" th:onclick="'showExplanation(\'' + ${it.comment} + '\');'"> 
    <sup>Explanation</sup> 
    </a> 
</td> 
<td th:class="${row.even}? 'even' : 'odd'" th:text="${it.answer}"> 
    col3 
</td> 
</tr> 

迭代與${it.correctAnswer}該生產線是一個應該被格式化爲以下幾點:

當且僅當沒有在${it.comment}那麼它應該被附加上標串「解釋」空字符串當我們點擊這個字符串時,會調用一些Javascript函數。

我上面的解決方案顯然不起作用,但有什麼方法可以在運行時爲Thymeleaf生成的動態值添加一些靜態html代碼。

我所試圖做的是:

Image

回答

3
在您的例子,你需要到Concat的文本鏈接

,做簡單的HTML改變來自:

<td th:class="${row.even}? 'even' : 'odd'" th:text="${it.correctAnswer}"> 
col2 
<a href="" th:onclick="'showExplanation(\'' + ${it.comment} + '\');'" > 
    <sup>Explanation</sup> 
</a> 
</td> 

<td th:class="${row.even}? 'even' : 'odd'" > 
<span th:text="${it.correctAnswer}">col2</span> 
<a href="" th:if="${it.comment}!=''" th:onclick="'showExplanation(\'' + ${it.comment} + '\');'"> 
    <sup>Explanation</sup> 
</a> 
</td> 

這應該顯示評論鏈接時,有一些。

+0

大衛它的工作。我不得不添加缺少的雙引號,然後它的工作。謝謝 – Tony