tharindu_DG
tharindu_DG

Reputation: 9261

Thymeleaf : Getting value from a map while iterating through a list

I'm trying to get the value from a map while iterating through a list. searchResult contains a list of Book objects and each has a list of categories. categoryFacetCounts is a map that contains counts for each category value. The map has values for all the possible categories (I debugged and checked).

Below is my solution which prints null as the output of the map:

<table class="table table-striped" id="watchlist-table">
    <thead>
    <tr>
        <th>Name</th>
        <th>Description</th>
        <th>Categories</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="book : ${searchResult}">
        <td th:text="${book.name}"></td>
        <td th:text="${book.description}"></td>
        <td>
            <span th:each="c : ${book.categories}">
                <span th:text="${c} + '(' + ${categoryFacetCounts[__${c}__]} + ') '"></span>
            </span>
        </td>
    </tr>
    </tbody>
</table>

Upvotes: 2

Views: 4429

Answers (1)

tharindu_DG
tharindu_DG

Reputation: 9261

Use the get method of the map, categoryFacetCounts

<span th:each="c : ${book.categories}">
       <span th:text="${c} + '(' + ${categoryFacetCounts.get(c)} + ') '"></span>
</span>

Hope this helps.

Upvotes: 3

Related Questions