I have the array variable (coming from back end) called "products". It has the following structure:
--products
-----special
----------0
---------------id
---------------price
---------------etc.....
----------1
---------------id
---------------price
---------------etc
----------2..etc
I am using thymeleaf. I am trying to loop the "special products" in javascript and trying to use their indexes to get the subdata (id, price, etc..). I am trying to use a new integer variable called "counter" to access the index. So my code is:
<script th:inline="javascript" th:with="setProducts=(${products.special})">
function LetsSayLoop() {
var counter = 0;
//looping through some checkboxes
$('#checkboxContainer .checkboxesClassName').each(function() {
if($(this).is(":checked")) {
//this is working:
var current_product_price = [[${setProducts[0].price}]];
//this is also working:
//var current_product_price = [[${setProducts[1].price}]];
//etc...
//this is not working:
var current_product_price = [[${setProducts[counter].price}]];
counter++;
}
});
}
</script>
My IDE (netbeans) is saying that the variable "counter " is not being used. Also the generated Javascritp and HTML ends with the following: Any ideas why?
var current_product_price =
EDIT: Small addition about the loop in javascript:
//[[${#lists.size(setProducts)}]] outputs the desired integer;
for (i = 0; i < [[${#lists.size(setProducts)}]]; i++) {
//this is working:
console.log([[${setProducts[0].price}]]);
//this is not:
console.log([[${setProducts[i].price}]]);
}
EDIT: (possible answer?)
I understood that thymeleaf does not recognize local javascript variables in [[]] scope. For example:
var current_product_price = [[${setProducts[counter].price}]];
the code expects that the variable "counter" is coming from thymeleaf. I need to do it using indexes, but with local variables it does not work. Can I increment some local js variables to accomplish this? Or is it some other way?