Skip to content
Advertisement

Populating inner table inside another table with Thymeleaf

I have DTO class:

private LocalDate date;
private List<String> firstShift;
private List<String> secondShift;
private List<String> thirdShift;

With getters, setters and toString for every field.

I’m creating this table for shift schedule calendar:

enter image description here

My Thymeleaf:

    <table class = "table table-striped table-hover" id = "schedule_table">
        <thead>
            <tr>
                <th class = "th_schedule_table">Date</th>
                <th class = "th_schedule_table">First Shift</th>
                <th class = "th_schedule_table">Second Shift</th>
                <th class = "th_schedule_table">Third Shift</th>
            </tr>
        </thead>
        <tbody>
            <tr th:each = "calendar_node : ${calendarNodeList}">
                <td th:text = "${calendar_node.date}"></td>
                <td>
                    <table>
                        <tbody>
                            <tr th:each = "employee, i : ${firstShift}">
                                <td th:text = "${firstShift.[i]}"></td>
                            </tr>
                        </tbody>
                    </table>
                </td>
                <td>
                    <table>
                        <tbody>
                            <tr th:each = "employee: ${secondShift}">
                                <td th:text = "${employee}"></td>
                            </tr>
                        </tbody>
                    </table>
                </td>
                <td>C</td>
            </tr>
        </tbody>
    </table>

The idea behind this: CalendarNode.date generates row for parent table, for each date. That works fine.

Inside every row, in a cell I have second table that should show list of employees who works on that date in that shift. If I have calendar_node object in one row, I’m using his “firstShift” field to generated rows for second inner table.

The problem is that I’m getting empty table. I checked my back-end and I have two employees for first date (18th July), first shift, one employee for second shift, but none is shown. I tried a lot of different syntax for Thymeleaf, none of it works. I guess I did Thymeleaf wrong?

UPDATE: Example of data that has been passed to web page via model object:

enter image description here

Advertisement

Answer

If firstShift is a field of calendar_node, then you need to actually address it as such in the template:

${calendar_node.firstShift} instead of ${firstShift}

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement