Beginner
Beginner

Reputation: 41

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";
    }

Upvotes: 3

Views: 14114

Answers (1)

Andrei Epure
Andrei Epure

Reputation: 1828

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>

Upvotes: 9

Related Questions