Html 百里香中的 for 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20633118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:31:28  来源:igfitidea点击:

for loop in thymeleaf

htmlloopseachthymeleaf

提问by user1275645

How can I do the following (java):

我该如何执行以下操作(java):

for(int i = 0; i < 81 ; i+=20){
   //Should loop through 5 times!
}

in Thymeleaf?

在百里香?

I've tried this:

我试过这个:

<option th:each="i : ${#numbers.sequence( 1, 81/20)}">
   <p th:text="${ i }"></p> <!-- THIS loops 4 times, instead of 5 -->
</option>

The problem is that it is not as accurate as the java piece of code. How to do this?

问题是它不如java代码那么准确。这该怎么做?

采纳答案by JaanRaadik

I am assuming this is due to the numbers you are using. For your java code, int i = 0; i < 81 ; i+=20 will return i=0, i=20, i=40, i=60 and i=80

我假设这是由于您使用的数字。对于您的 java 代码, int i = 0; 我 < 81 ; i+=20 将返回 i=0、i=20、i=40、i=60 和 i=80

however your following code numbers.sequence( 1, 81/20)} should returns the integers from 1 to 4.05, being 1, 2, 3, and 4.

但是您的以下代码 numbers.sequence(1, 81/20)} 应该返回从 1 到 4.05 的整数,即 1、2、3 和 4。

The first loop returns 5 results for i, therefore runs 5 times. the second returns only 4 results, so runs 4 times. I would suggest running your sequence starting at 0 to return 5 results as desired.

第一个循环为 i 返回 5 个结果,因此运行了 5 次。第二个只返回 4 个结果,所以运行 4 次。我建议从 0 开始运行您的序列以根据需要返回 5 个结果。

If you wanted your java code to mirror the second code, you should change it to: int i = 1; i < 4.05 ; i+=1

如果你想让你的 java 代码反映第二个代码,你应该把它改成: int i = 1; 我 < 4.05 ; 我+=1

To put it simply, you are running through a loop with different numbers, I suggest changing the second statement to start from 0.

简单地说,您正在运行具有不同数字的循环,我建议将第二个语句更改为从 0 开始。

回答by windX

Add step to your code is quite easy.

向您的代码添加步骤非常简单。

#{numbers.sequence(0, 81, 20)}

回答by sitakant

use iterStatkey word to iterate. Example If you have an Array of String and you are iterating the same using thymeleaf.

使用iterStat关键字进行迭代。示例 如果您有一个字符串数组,并且您正在使用 thymeleaf 对其进行迭代。

<div th:each="str,iterStat : strings">
   <span th:text="${str}"/><!--This will print the index value-->
   <span th:text="${iterStat.index}"/><!--This will print the Index-->
</div> 

回答by user2274218

The 1st value is the beginning of count, the 2nd is the maximum value and the 3rd is the incremental value

第一个值是计数的开始,第二个是最大值,第三个是增量值

${#numbers.sequence( 1, 4, 1)}