最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Thymeleaf th:text How to add static text to dynamic value - Stack Overflow

matteradmin6PV0评论

Started with Thymeleaf and have one question. I have iteration with

<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(\'' + ${itment} + '\');'">
     <sup>Explanation</sup>
   </a>
 </td>
 <td th:class="${row.even}? 'even' : 'odd'" th:text="${it.answer}">
   col3
 </td>
</tr>

The line with ${it.correctAnswer} is one that should be formatted as following:

If and only if there is not empty String in ${itment} then it should be appended superscripted string "Explanation" and when we click this string some Javascript function is called.

My solution above obviously didn't work, but is there any way to add some static html code to dynamic value generated by Thymeleaf at runtime.

What I am trying to do is :

Started with Thymeleaf and have one question. I have iteration with

<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.ment} + '\');'">
     <sup>Explanation</sup>
   </a>
 </td>
 <td th:class="${row.even}? 'even' : 'odd'" th:text="${it.answer}">
   col3
 </td>
</tr>

The line with ${it.correctAnswer} is one that should be formatted as following:

If and only if there is not empty String in ${it.ment} then it should be appended superscripted string "Explanation" and when we click this string some Javascript function is called.

My solution above obviously didn't work, but is there any way to add some static html code to dynamic value generated by Thymeleaf at runtime.

What I am trying to do is :

Share Improve this question edited May 14, 2016 at 7:12 David Marko 2,5293 gold badges27 silver badges58 bronze badges asked May 13, 2016 at 17:05 TonyTony 3133 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

In your example you require to concat text to link, to do it simply change your html from:

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

to

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

This should display ments link when there are some.

Post a comment

comment list (0)

  1. No comments so far