hyun_sato
hyun_sato

Reputation: 39

How to set variable in thymeleaf?

I'm still looking for a way to set values ​​directly into JavaScript function param in thymeleaf. How to set ${variable} to javascript function param directly? Help me.

function **loadDetailView**(no){
 location.href = "/home/docs/holiday/detail-view/"+no; 
}

<tbody id="docsTr">
<tr th:if="${size} == '0'">
  <td colspan=3>No data.</td>
</tr>
<tr th:unless="${size} == '0'" th:each = "docs : ${list}"
style="cursor:pointer;" onclick="**loadDetailView**(1)">
  <td th:text="${docs.board_no}">1</td>
  <td th:text="${docs.name}">Brian</td>
  <td th:text="${docs.holiday_type}">Annual</td>
</tr>
</tbody>

Upvotes: 0

Views: 292

Answers (1)

Jorge.V
Jorge.V

Reputation: 1347

I think you might be mixing concepts here. A javascript function can't recieve a thymeleaf parameter, because what thymeleaf does is "compiling" the page before sending it to the client. Once it's on the client (and javascript may execute) there is no thymeleaf anymore.

What you can do is make each onclick call to loadDetailView have a different parameter in each <tr>, in the following way:

<tr th:unless="${size} == '0'" th:each = "docs, iter : ${list}"
style="cursor:pointer;" onclick="loadDetailView([[${iter.index}]])">

Which would call loadDetailView with 0, 1, 2, etc depending on the row.

Upvotes: 1

Related Questions