2

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();


}
Gabriel Rogath
  • 710
  • 2
  • 8
  • 23

1 Answers1

2

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')}">
Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211
  • Thank you so much. The second one worked , but the first didn't work , show me this error _Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.thymeleaf.engine.IterationStatusVar] to type [java.lang.Integer]_ . Iam trying to find out why, Because the first option mighty be usefully in future also. – Gabriel Rogath Mar 12 '21 at 13:31
  • I have added the repository file to highlight why the first option does not work. @Wim Deblauwe – Gabriel Rogath Mar 12 '21 at 13:35
  • I updated the answer for the first to use `iter.index`, I made a mistake. – Wim Deblauwe Mar 12 '21 at 13:36
  • Now both option works fine. Thank you so much @Wim Deblauwe , This is a five star answer. – Gabriel Rogath Mar 12 '21 at 13:41
  • @GabrielRogath - just for reference, you can see a list of the iteration status variables [here](https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf#keeping-iteration-status). They are `index`, `count`, `size`, `even`, `odd`, `first`, `last`. – andrewJames Mar 12 '21 at 13:41
  • Okay. Thanks once again @Wim Deblauwe – Gabriel Rogath Mar 12 '21 at 13:44