Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i loop over an ArrayList using thymeleaf & spring Boot?

I am new to thymeleaf and i try to loop over a ArrayList but it doesn't work for me .. some help please: this is my Html page:

<body>
    <div class="row">
        <table>
            <tr th:each="data: mois">  
                <td class="text-center" th:text="${data}">data</td>
            </tr>			
        </table>
    </div>
</body>

this is My controller

@RequestMapping(value="/editePlanning", method= RequestMethod.GET)
    public String editePlanning(Model model){
        Psi psi = psiRepository.findOne((long) 1);
        List<String> data = new ArrayList<String>();

        for(int i=0;i<psi.getNombreMois();i++){
            int val = psi.getMoisDebut()+i%12;
            data.add(""+ val);
        }

        model.addAttribute("mois",data);
		
        return "editePlanning";
    }
like image 271
Beginner Avatar asked Jun 30 '26 17:06

Beginner


1 Answers

You have a typo in your iteration (see the docs, they are very good):

  <tr th:each="data: ${mois}">

Don't forget you can get the iteration index, useful to generate the id of elements

  <tr th:each="data, iterstat: ${mois}">
     <td th:text="${data}" th:id="|td${iterstat.index}|"></td>
  </tr>
like image 50
Andrei Epure is hiring Avatar answered Jul 03 '26 08:07

Andrei Epure is hiring



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!