How to handle two Lists in thymeleaf with th:switch and th:each

I am getting one list of objects from the database and iterate through that list inside html page with thymeleaf engine using th:switch and th:each. Now I want to change one property from each object from this list to another format, The problem is how do I use those formatted value inside another list in switch statement in thymeleaf with it’s corresponding object from the first list.

Below shows how I loop inside every object to get the value of getKinachoitajika() which is a double number that appears as 5.0E7 in output, now I format it so as to get a display of normal number like 50,000,000 and put those value into an ArrayList of arrItajika and use them inside html file while looping through the objects of the first list.

@GetMapping("/joinMichangoQuery")
   public String kandaAndParokia(Model model){
     List<Kmichango> listKandaAndParokia = kMichangoRepository.findAll();
     ArrayList arrItajika = new ArrayList<>();
        for(int i=0; i< listKandaAndParokia.size(); i++){
           NumberFormat nf = NumberFormat.getInstance();
           nf.setMinimumFractionDigits(0);
           arrItajika.add(nf.format(listKandaAndParokia.get(i).getKinachoitajika()));
         }
     model.addAttribute("listKandaAndParokia", listKandaAndParokia);
     model.addAttribute("itajikaFromated", arrItajika);
     return "michango_joined";
 }

Now inside michango_joined.html I iterate through all the values of each object and display them on the table as below.

        <div th:switch="${listKandaAndParokia}" class="container my-5">
        <h2 th:case="null">No Michango</h2>
        <div th:case="*">
            <table class="table table-bordered table-striped table-responsive-md">
                <thead class="thead-dark">
                    <tr>
                <th>ID</th>
                <th>KandaID</th>
                <th>Kinachohitajika</th>
                <th>Kilichopatikana</th>
                <th>Hali</th>
                <th>Jina La Mchango toka Parokia Michango</th>
                <th></th>
                    </tr>
                </thead>
                <tbody>
                <tr th:each="kMchango : ${listKandaAndParokia}">
                <td th:text= "${kMchango.id}"></td>
                <td th:text= "${kMchango.kandaId.kandaName}"></td>
                <td th:text= "${kMchango.kinachoitajika}"></td>
                <td th:text= "${kMchango.kilichopatikana}"></td>
                <td>[[${kMchango.mchangoParokia.status}]]</td>
                <td th:text= "${kMchango.mchangoParokia.mchangoJina}"></td>
                </tr>
            </tbody>
            </table>
        </div>
    </div>

Now I want to use the value of the ArrayList arrItajika to be display with each corresponding objects within the switch, meaning I want to replace this “${kMchango.kinachoitajika}” with my arrItajika inside thymeleaf, How do I perform this interaction or is there another suggestion on how I can handle this.

Update: I have added the Repository file

public interface KmichangoRepository extends JpaRepository <Kmichango, String> {
@Query(value = "SELECT * FROM michango_kanda INNER JOIN michango ON michango_kanda.mchango_code = michango.mchango_code",nativeQuery = true)
public List<Kmichango>  getKandaAndParokiaByMichangoCode();


}

Answer

If the indexes of both lists are equal, you can use th:each with an index. See Thymeleaf – How to loop a list by index

<tr th:each="kMchango,iter: ${listKandaAndParokia}">
...
  <td th:text= "${itajikaFromated[iter.index]}"></td>
...
</tr>

Alternatively, create a new class that combines Kmichango with an extra field that is the formatted value and have just 1 list in your model with elements of that new type of class.

Finally, you can also do the formatting in the template itself:

<span th:text="${#numbers.formatDecimal(kMchango.kinachoitajika, 0, 'COMMA', 2, 'POINT')}">

Leave a Reply

Your email address will not be published. Required fields are marked *