How to display iteration index in Thymeleaf?

PublicDisplayName

I'm attempting to display each row from an array using Thymeleaf - following their documentation I am unable to use any of the following attributes from th:each:

  • The current iteration index, starting with 0. This is the index property.

  • The current iteration index, starting with 1. This is the count property.

userinput.html:

 <tr th:each="year : ${years}">
      <th scope="row" th:text="${years}"></th>
      <th scope="row" th:text="${bdcInterest}"></th>
      <td th:text="${bdAmount}"></td>
 </tr>

CalculatorController.java:

    @RequestMapping(value = "/submit", method = RequestMethod.GET)
public String userInput(Model model, BigDecimal lumpsum, BigDecimal interestrate, BigDecimal monthlywithdrawal) {
    
    BigDecimal initialinvestment = lumpsum;
    
    BigDecimal[] bdAmount = new BigDecimal[11];
    BigDecimal[] bdcInterest = new BigDecimal[11];
    BigDecimal[] initialInvestment = new BigDecimal[11];
    int[] years = new int[11];
    
    bdcInterest[0] = new BigDecimal(0);
    initialInvestment[0] = initialinvestment;
    
    int increment = 1;

    while(increment < 10) {

        BigDecimal amount = lumpsum
                .multiply(BigDecimal
                        .valueOf(1)
                        .add(interestrate
                                .divide(BigDecimal
                                        .valueOf(100)))
                        .subtract(monthlywithdrawal
                                .multiply(BigDecimal
                                        .valueOf(12)))); // Calculate the total yearly amount
        
        BigDecimal cInterest = amount.subtract(initialinvestment); // Calculate only the interest earned 

        bdAmount[increment] = amount;
        bdcInterest[increment] = cInterest;
        initialInvestment[increment] = initialinvestment;
        years[increment] = increment;
        
        lumpsum = amount;

        increment++;
    }
    
    model.addAttribute("years", years);
    model.addAttribute("initialInvestment", initialInvestment);
    
    model.addAttribute("bdAmount", bdAmount);
    model.addAttribute("bdcInterest", bdcInterest);

    return "userinput";

}

The necessary data is submitted correctly in each respective array, however I believe I've misunderstood the documentation: enter image description here

jccampanero

Thymeleaf maintains the iteration status of the th:each tag in a special variable. Please, see the relevant documentation.

Among the different information provided in that variable, you can find an index property, which corresponds to the actual iteration index.

In you example, you probably could iterate your results like this:

 <tr th:each="year, iterStat : ${years}">
      <th scope="row" th:text="${year}"></th>
      <th scope="row" th:text="${bdcInterest[iterStat.index]}"></th>
      <td th:text="${bdAmount[iterStat.index]}"></td>
 </tr>

To avoid this kind of problems, please, consider define in your code a simple java object that agglutinates the four properties you are iterating:

public class MuCustomObject {
  private BigDecimal bdAmount;
  private BigDecimal bdcInterest;
  private BigDecimal initialInvestment;
  private int year;

  // getters and setters omitted for brevity
}

Then, use the object in your controller:

    @RequestMapping(value = "/submit", method = RequestMethod.GET)
public String userInput(Model model, BigDecimal lumpsum, BigDecimal interestrate, BigDecimal monthlywithdrawal) {
    
    BigDecimal initialinvestment = lumpsum;
    
    List<MyCustomObject> myCustomObjectList = new ArrayList<MyCustomObject>();
    
    MyCustomObject myCustomObject = new MyCustomObject();
    myCustomObject.setBdcInterest(new BigDecimal(0));
    myCustomObject.setInitialInvestment(initialinvestment);
    myCustomObjectList.add(myCustomObject);
    
    int increment = 1;

    while(increment < 10) {

        BigDecimal amount = lumpsum
                .multiply(BigDecimal
                        .valueOf(1)
                        .add(interestrate
                                .divide(BigDecimal
                                        .valueOf(100)))
                        .subtract(monthlywithdrawal
                                .multiply(BigDecimal
                                        .valueOf(12)))); // Calculate the total yearly amount
        
        BigDecimal cInterest = amount.subtract(initialinvestment); // Calculate only the interest earned 

        myCustomObject = new MyCustomObject();
        myCustomObject.setBdAmount(amount);
        myCustomObject.setBdcInterest(cInterest);
        myCustomObject.setInitialInvestment(initialinvestment);
        myCustomObject.setYear(increment);
        myCustomObjectList.add(myCustomObject);
        
        lumpsum = amount;

        increment++;
    }
    
    model.addAttribute("myCustomObjects", myCustomObjectList);

    return "userinput";

}

With that information you could directly iterate the collection:

 <tr th:each="myCustomObject, iterStat : ${myCustomObjects}">
      <th scope="row" th:text="${myCustomObject.year}"></th>
      <th scope="row" th:text="${myCustomObject.bdcInterest}"></th>
      <td th:text="${myCustomObject.bdAmount}"></td>
 </tr>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

How to place iteration index into array index using thymeleaf?

How to display a BufferedImage in Thymeleaf

How to calculate and display a percentage in Thymeleaf?

Thymeleaf - How to loop a list by index

How to delete objects on a Thymeleaf th:each iteration passing an ID to the controller?

Spring - Thymeleaf iteration

Thymeleaf, Javascript Inlining and Iteration

Expert Javascript Iteration Thymeleaf

thymeleaf iteration on a list

Wrong Thymeleaf iteration

how to display comments for posts in rails (by iteration )

How display images on each iteration (ajax)

How to display messages in Thymeleaf and Spring Boot?

How to display the collection of objects with for each loop in Thymeleaf?

How to display one value of enum with Thymeleaf

How to display ArrayList Object in Thymeleaf page

How to display blank data in Thymeleaf in th:text

How to display something on Thymeleaf table if not null

Thymeleaf - How to display text not included in th:text

Thymeleaf iteration status variable is null

How to display image in index view

How to display answers in index django?

How to get the index of the current element being processed in the iteration without a for loop?

how to put current iteration number as array index in xsl

How to acces index [i-1] at the first iteration of a FOR loop [Python]

How to pass an iteration index of list into a function called by multiprocessing pool?

How to iterate `dict` with `enumerate` and unpack the index, key, and value along with iteration

How to remove first three index of array in every loop iteration - JS

TOP Ranking

HotTag

Archive