sebastian romero
sebastian romero

Reputation: 186

Thymeleaf: How to use #numbers.sequence() with a variable limit?

I'm calling an int value from a database to determine the number of stars that should be displayed in my html using thymeleaf and Spring Boot, but using ${#numbers.sequence(1,obj.stars)} doesn't seem to work.

this is my html-thymeleaf code:

    <tr th:each="obj : ${allObjs}" class="pointer" th:onclick="'javascript:openobj(\'' + ${obj.id} + '\');'">
    <td class="text-center" th:text="${obj.id}"></td>
    <td class="text-center" th:text="${obj.code}"></td>
    <td class="text-center" th:text="${obj.name}"></td>
    <td class="text-center" th:text="${obj.contract}"></td>
    <td class="text-center" th:text="${obj.difficulty}"></td>
    <td class="text-center" th:text="${obj.priority}"></td>
    <td class="text-center">
        <!--this is the line I can't get to work :(-->
        <span class="fa fa-star-o" th:each="star:${#numbers.sequence(1,obj.stars)}"></span> 
    </td>
    <td class="text-center" th:text="${obj.state}"></td>
    <td class="text-center" th:text="${obj.percent}"></td>
    <td class="text-center" th:text="${obj.term}"></td>
    <td class="text-center" th:text="${obj.version}"></td>
    <td class="text-center" th:text="${obj.price}"></td>
</tr>

and my controller

 @GetMapping("/Obj")
 public ModelAndView index() {
      ModelAndView view = new ModelAndView("/Obj/index");
      view.addObject("title", "Obj");
      List<Obj> allObjs = ObjService.findAll();
      view.addObject("allObjs", allObjs);
      return view;
 }

Upvotes: 2

Views: 17409

Answers (1)

sebastian romero
sebastian romero

Reputation: 186

Well, I know it's weird to answer your own question but, thanks to Michael Petch who tested it, I found that the problem was in the sequence. It was starting from 1 when I had values of 0 in obj.stars so the sequence couldn't be created with a step of 1.

Changing it to

<span class="fa fa-star-o" th:each="star:${#numbers.sequence(0,obj.stars)}"></span> 

Solved the problem.

Upvotes: 10

Related Questions