【问题标题】:Looping through array Thymeleaf using javascript使用javascript循环遍历数组Thymeleaf
【发布时间】:2018-06-12 06:18:36
【问题描述】:

我有一个名为“products”的数组变量(来自后端)。它具有以下结构:
--产品
-----特别
----------0
---------------身份证
---------------价格
---------------等.....
----------1
---------------身份证
---------------价格
---------------等
----------2..等

我正在使用百里香叶。我正在尝试在 javascript 中循环“特殊产品”并尝试使用它们的索引来获取子数据(id、价格等)。 我正在尝试使用一个名为“counter”的新整数变量来访问索引。所以我的代码是:

<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>

我的 IDE (netbeans) 说变量“counter”没有被使用。生成的 Javascritp 和 HTML 也以以下结尾: 任何想法为什么?

var current_product_price = 

编辑:关于 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}]]);
}

编辑:(可能的答案?)
我知道 thymeleaf 无法识别 [[]] 范围内的本地 javascript 变量。例如:

var current_product_price = [[${setProducts[counter].price}]];

代码期望变量“counter”来自百里香。 我需要使用索引来做到这一点,但使用局部变量它不起作用。我可以增加一些本地 js 变量来完成这个吗?还是有其他方式?

【问题讨论】:

  • 所以唯一的问题是您在 IDE 上看到的警告?代码工作正常吗?可能是 IDE 上的错误,他们有那些 :P 代码对我来说似乎很好。
  • 不,代码不起作用。它中断并且输出以:“var current_product_price =”结束,就是这样。之后不再有代码

标签: javascript thymeleaf


【解决方案1】:

如果您将jquery.each 更改为forEach,它应该可以工作。这里举个例子

 function a() {
      var counter =0;
      [1, 2, 3, 4].forEach(function(e){
              console.log('Element:', e);
              console.log('Counter:', counter++);
      });
 }

 a();

【讨论】:

  • 主要思想是生成的javascript不会显示未使用的数据(如name、id等)。这个方法应该可以,但是[1,2,3,4]会被替换成实际的产品完整信息
【解决方案2】:

代码对我来说看起来不错, 所以它应该工作。

如果唯一的问题是您在 IDE 上看到的警告,我不会担心,这可能是一个错误。

但是你不需要使用计数器,你可以使用 .each 使用的内置变量:

.each(function( index, element ) {
  //Use index instead of counter.
}

编辑:

好的,我想现在我明白了这个问题:

您不能在这种表达式中使用 javascript 变量。将其分为两部分 - 首先从数组中创建一个 js 变量,然后像这样使用 [index]:

function LetsSayLoop() {
    var current_Products = [[${setProducts}]]; //btw not sure about double brackets
    //looping through some checkboxes
    $('#checkboxContainer .checkboxesClassName').each(function(index) {
        if($(this).is(":checked")) {

            //this should work:
            var current_product_price = current_Products[index];
            //etc...  
        }
    }); 
}

【讨论】:

  • 它确实有效,但生成的 javascript 包含我不想在代码中显示的所有 ${setProducts} 字段
  • @jondaniels 嗯好的,所以你不能在 Thymeleaf 上创建计数器? {
【解决方案3】:

我还不能评论,所以我会发布答案。

请将此行添加到each 调用中,并告诉我们计数器是否实际上在each 范围内可用并更新。接下来你会知道它实际被调用了多少次。

alert("Counter val: " + counter.toString());

【讨论】:

  • 那不是问题。读取“var current_product_price = [[${setProducts[counter].price}]];”行后循环中断.我无法使用 counter 变量。如果此变量被 Thymeleaf 变量替换,则可以,但我无法评估 thymeleaf 变量
  • 会不会是 setProducts[] 数组的问题?输出它的大小并告诉我们。我知道这似乎是多余的,但是您给我们留下的信息很少,也没有可以运行的示例。
  • 尺寸还可以。我设法发现的是 thymeleaf 理解这一行:var current_product_price = [[${setProducts[counter].price}]]; 正在使用来自 thymeleaf 的变量计数器,我不能使用一些本地 js 变量。问题是我可以使用本地 thymeleaf 变量并每次都增加它吗?
  • 这是一个不同的问题,更新上面的问题陈述,您会得到答案。我希望它不会,但我对 Thymeleaf 没有经验。也许他们的文档中有一些东西?
猜你喜欢
  • 2018-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-16
  • 2017-03-27
  • 2017-12-12
  • 2015-10-15
相关资源
最近更新 更多