Java_User
Java_User

Reputation: 475

How to display list of map values in HTML5 using thymeleaf

I need to display values in map in HTML using thymeleaf. Here is my java code

List<Object> searchResultsList = searchService.searchPerson(id); 
List<String> list = new ArrayList<String>();
Map<Integer, List<String>> mapResults = new HashMap<Integer, List<String>>();
for (int i = 0,; i < searchResultsList.size(); i++) { 
        list.add(0, row[0].toString());
        list.add(1, row[1].toString());
        list.add(2, row[2].toString()); 
    mapResults.put(i, list);
}
model.addAttribute("mapResults", mapResults); 

HTML Code:

<div  th:if="!${#maps.isEmpty(mapResults)}">
    <div>
    Found List of  records  
    </div>
    <table class="tg">
        <thead>
            <tr> 
                <th class="tg"># of Lines</th> 
                <th class="tg">Person id</th>
                <th class="tg">Sub Id </th>             
            </tr>
        </thead>    
        <tbody>
            <tr th:each="row : ${mapResults}"> 
                <td class="tg bg" th:text="${row.value}"></td> 
                <td class="tg bg" th:text="${row.value}"></td> 
                <td class="tg bg" th:text="${row.value}"></td> 
            </tr> 
        </tbody>
    </table>
</div>

Actual Output :

# of Lines          Person id           Sub Id   
[2, 1235, 1]        [2, 1235, 1]        [2, 1235, 1]

Expected Output :

# of Lines          Person id           Sub Id   
2                   1235                1

Can anyone help me how to retrieve each value from list.

Thanks in advance.

Upvotes: 2

Views: 5156

Answers (1)

user178982
user178982

Reputation:

row.value is a list. To get a specific item from it, just use this item's index:

<tr th:each="row : ${mapResults}"> 
    <td class="tg bg" th:text="${row.value[0]}"></td> 
    <td class="tg bg" th:text="${row.value[1]}"></td> 
    <td class="tg bg" th:text="${row.value[2]}"></td> 
</tr> 

Upvotes: 3

Related Questions