Search code examples
javascriptthymeleaf

How to use the index of a JavaScript loop in a thymeleaf iteration?


I would like to use the index of this JavaScript loop to iterate my thymeleaf integration. For example:

for(let i=1; i<8; i++){
    /*[# th:each="temp : ${arrayListTemperature[ i ] }"]*/
        {x: new Date[(${temp})]},
    /*[/]*/
}

To get

/*[# th:each="temp : ${arrayListTemperature1}"]*/
    { x: new Date[(${temp})]},
/*[/]*/

Solution

  • Your thymeleaf is executed server-side, before the page is send to the browser.

    Your JavaScript is executed client-side, after the page is send to the browser.

    So what you're asking looks impossible: thymeleaf can't access a value during its execution, while this value will exist after thymeleaf was executed.