java - Thymeleaf 迭代嵌套列表来为高尔夫应用程序创建表 <th> 标题和 <td> 文本

标签 java html thymeleaf

嗨,我对 Spring 和 Thymeleaf 比较陌生,但我正在创建一个圆盘高尔夫 CRUD 应用程序,并且我能够使用与 ParList 具有 oneToMany 关系的 Course 对象添加类(class)。一切正常,我可以添加球场,但球场并不总是有 18 洞。我怎样才能动态地显示这一点,以便 9 洞类(class)表有 9 列、14 - 14 列等? 我尝试过使用 th:each="par, iterStat : ${course.parLists}"并尝试使用 iterStat.index 但我不太明白。 那么,如何获取列表的索引并将其用于洞号并显示类(class)标准而不像下面那样重复? 所以我的数据如下所示:

Course{id=3, name='Legende Main', parLists=[ParList{parListId=10, parHole1='3', parHole2='3', parHole3='3', parHole4='3', parHole5='3', parHole6='3', parHole7='3', parHole8='3', parHole9='3', parHole10='', parHole11='', parHole12='', parHole13='', parHole14='', parHole15='', parHole16='', parHole17='', parHole18=''}]}

我的 HTML

<div class="container" th:each="course : ${courses}">
        <div class="border">
            <div id="courseDetails" class="row">
                <div class="col-sm-4">
                    <p><strong>Course Name:</strong>
                        <a th:text="${course.name}"></a>
                    </p>
                </div>
            </div>
        </div>
        <div >
            <table th:each="par, index : ${course.parLists}" id="courseInfo" class="table table-bordered w-auto">
                <tr >
                    <th th:text="${'Hole'}"></th>
                    <th th:text="${}"></th>
                    <th th:text="${'#2'}"></th>
                    <th th:text="${'#3'}"></th>
                    <th th:text="${'#4'}"></th>
                    <th th:text="${'#5'}"></th>
                    <th th:text="${'#6'}"></th>
                    <th th:text="${'#7'}"></th>
                    <th th:text="${'#8'}"></th>
                    <th th:text="${'#9'}"></th>
                    <th th:text="${'#10'}"></th>
                    <th th:text="${'#11'}"></th>
                    <th th:text="${'#12'}"></th>
                    <th th:text="${'#13'}"></th>
                    <th th:text="${'#14'}"></th>
                    <th th:text="${'#15'}"></th>
                    <th th:text="${'#16'}"></th>
                    <th th:text="${'#17'}"></th>
                    <th th:text="${'#18'}"></th>
                    <th>Actions</th>
                </tr>

                <tr>
                    <td th:text="${'Par'}"></td>
                    <td th:text="${par.parHole1}"></td>
                    <td th:text="${par.parHole2}"></td>
                    <td th:text="${par.parHole3}"></td>
                    <td th:text="${par.parHole4}"></td>
                    <td th:text="${par.parHole5}"></td>
                    <td th:text="${par.parHole6}"></td>
                    <td th:text="${par.parHole7}"></td>
                    <td th:text="${par.parHole8}"></td>
                    <td th:text="${par.parHole9}"></td>
                    <td th:text="${par.parHole10}"></td>
                    <td th:text="${par.parHole11}"></td>
                    <td th:text="${par.parHole12}"></td>
                    <td th:text="${par.parHole13}"></td>
                    <td th:text="${par.parHole14}"></td>
                    <td th:text="${par.parHole15}"></td>
                    <td th:text="${par.parHole16}"></td>
                    <td th:text="${par.parHole17}"></td>
                    <td th:text="${par.parHole18}"></td>
                    <td>

enter image description here

新类(class)

<form action="#" th:action="@{/discgolf/saveCourse}" th:object="${newCourse}"
                      method="POST">
                    <div class="form-group">
                    <input type="text" th:field="*{name}"
                           placeholder="Course name" class="form-control mb-4 col-4">
                    </div>
                    <th:block  th:object="${hole}">
                    <div class="container">
                        <div id="parRow" class="row g-0">
                            <div class="col">
                                <label>#1</label>
                                <input type="text" th:field="*{par}" placeholder="#1">
                            </div>
                            #2 - #17
                            <div class="col">
                                <label>#18</label>
                                <input type="text" th:field="*{par}" placeholder="#18">
                            </div>
                        </div>
                    </div>
                    </th:block>
                    <button type="submit" class="btn btn-info col-2">Save Course</button>
                </form>

最佳答案

如果您的parHole将是 List<Int>Map<Int,Int>那么你可以使用 th:each 迭代它或其条目就像任何列表一样。但是,由于每个 parHole 都有静态字段名称,因此您无法执行此操作,并且必须像示例中那样重复。

关于java - Thymeleaf 迭代嵌套列表来为高尔夫应用程序创建表 <th> 标题和 <td> 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75494349/

相关文章:

java - 取消 Java Webstart 自定义下载

javascript - 是否可以通过 javascript 将 @font-face html 实体输入到输入中?

javascript - 向矩形光栅图像添加圆 Angular

spring - 以编程方式解析 Thymeleaf 模板

jquery - 如何通过先前的选择填充下拉列表 Spring thymeleaf

java - 类似于 Java 的 EnumSet 的结构,可以容纳重复的元素

java - 如何将b树存储到光盘中?

java - Spring Boot配置-2配置LDAP-Yml文件

JavaScript - 如何更改innerHTML

javascript - Thymeleaf 在 Google Analytics 代码上抛出错误