Saturday, June 1, 2024
 Popular · Latest · Hot · Upcoming
25
rated 0 times [  29] [ 4]  / answers: 1 / hits: 28363  / 9 Years ago, wed, april 22, 2015, 12:00:00

I'm trying to write some Javascript code where I need to use a model attribute. Here is how I define the script tag:



<script type=text/javascript th:inline=javascript>
/*<![CDATA[*/

//need some loops

/*]]>*/
</script>


What I need to do is, using each iterations over model attributes inside the script. So far I couldn't manage to do this with th:each. Any help is appreciated.


More From » spring

 Answers
2

I guess you need to wrap your model attrubite in double brackets, like this: [[${modelAttribute}]]. The script inlining section of the Thymeleaf docs can help a bit:
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#script-inlining-javascript-and-dart



<script type=text/javascript th:inline=javascript>
/*<![CDATA[*/

var theList = [[${modelAttribute}]]
for (i = 0; i < theList.length; i++) {
doSomething(theList[i]);
}

/*]]>*/
</script>

[#66955] Tuesday, April 21, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
haden

Total Points: 638
Total Questions: 95
Total Answers: 99

Location: Burundi
Member since Wed, Nov 25, 2020
4 Years ago
;