3

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

1 Answer 1

9

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.