使用Thymeleaf中的列表的最佳方法,通过嵌套值将其添加到Bootstrap手风琴中。

huangapple go评论89阅读模式
英文:

Best approach using a list in Thymeleaf adding to a Bootstrap accordion by a nested value

问题

以下是翻译好的部分:

Data getAllRoundsByUserId (sorry if it looks complicated, a round has a date, total, score (List of Score) and a course(name, record, par, List of Hole)

  1. 我有一个用户的飞盘高尔夫回合列表,我想要为每个球场显示一个手风琴,然后当您选择球场时,显示并可折叠该球场的所有回合。我可以使手风琴工作,但问题是每个得分都是一个手风琴。问题是,我是否应该有一个回合列表的列表,或者只显示的回合列表会起作用?
  2. 以下是我的数据(getAllRoundsByUserId),我的控制器以及带有rounds/{id}(id=userId)方法和我的HTML
  3. 数据getAllRoundsByUserId(如果看起来很复杂,一个回合包括日期,总分和分数(Score列表),以及一个球场(名称、记录、差点和Hole列表)。

Controller method

  1. @GetMapping("/rounds/{id}")
  2. public String roundsHome(@PathVariable(value = "id") Long id,
  3. Model model) {
  4. List<Course> courses = courseService.getAllCourses();
  5. List<Round> rounds = userService.getUserById(id).getRounds();
  6. model.addAttribute("courses", courses);
  7. model.addAttribute("rounds", rounds);
  8. return "/discgolf/round/rounds";
  9. }

html

  1. <div class="container">
  2. <div>
  3. <a>Rounds Played</a>
  4. </div>
  5. <div id="accordion">
  6. <div th:each="round : ${rounds}" class="card">
  7. <div class="card-header" id="headingOne">
  8. <h5 class="mb-0">
  9. <button class="btn btn-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
  10. <div>
  11. <label>Course: </label>
  12. <label th:text="${round.course.name}"></label>
  13. </div>
  14. </button>
  15. </h5>
  16. </div>
  17. <div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion">
  18. <div class="card-body">
  19. <div class="d-flex flex-row">
  20. <div class="p-3">
  21. <label>Date: </label>
  22. <label th:text="${#dates.format(round.roundDate, 'dd-MMM-yyyy')}"></label>
  23. </div>
  24. <div class="p-3">
  25. <label>Score: </label>
  26. <label th:text="${round.total - round.course.par}"></label>
  27. </div>
  28. </div>
  29. <br>
  30. <div>
  31. <table id="courseInfo" class="table table-bordered w-auto">
  32. <th:block th:each="course : ${round.course}">
  33. <tr>
  34. <th th:text="${'Hole'}"></th>
  35. <th th:each="hole : ${course.holes}" th:text="${hole.number}"></th>
  36. <th th:text="${'Total'}"></th>
  37. </tr>
  38. <tr>
  39. <td th:text="${'Par'}"></td>
  40. <td th:each="par : ${course.holes}" th:text="${par.par}"></td>
  41. <td th:text="${course.par}"></td>
  42. </tr>
  43. <tr>
  44. <td th:text="${'Score'}"></td>
  45. <th:block th:each="score : ${round.scores}">
  46. <td th:style="${score.score > score.holePar ? 'background-color: red' : (score.score < score.holePar ? 'background-color: blue' : 'background-color: #eee' )}" th:text="${score.score}"></td>
  47. </th:block>
  48. <td th:text="${round.total}"></td>
  49. </tr>
  50. </th:block>
  51. </table>
  52. <br>
  53. <a th:href="@{/discgolf/deleteRound/{id}(id=${round.roundId})}" title="Remove Course" data-target="#deleteRoundModal" class="table-link danger" id="deleteRoundButton">
  54. <span id="deleteRound" class="fa-stack">
  55. <i class="fa fa-square fa-stack-2x"></i>
  56. <i class="fa fa-trash-o fa-stack-1x fa-inverse" title="Delete this round"></i>
  57. </span>
  58. </a>
  59. </div>
  60. </div>
  61. </div>
  62. </div>
  63. </div>
  64. </div>
英文:

I have a list of discgolf rounds for a user, to display them I want an accordion for each course, then as you select the course all rounds for that course are displayed and can be collapsed. I can get the accordion working but the problem is each score is its own accordion. Question should I have a list of a list of rounds, or would just the shown list of rounds work?

Below is my data (getAllRoundsByUserId), my controller with rounds/{id} (id=userId) method and my html.

Data getAllRoundsByUserId (sorry if it looks complicated, a round has a date, total, score (List of Score) and a course(name, record, par, List of Hole)

  1. [Round{roundId=19, course=Course{id=1, name=&#39;Legende&#39;, holes=[Hole{holeId=37, number=1, par=3}, Hole{holeId=38, number=2, par=3}, Hole{holeId=39, number=3, par=3}, Hole{holeId=40, number=4, par=3}, Hole{holeId=41, number=5, par=3}, Hole{holeId=42, number=6, par=3}, Hole{holeId=43, number=7, par=3}, Hole{holeId=44, number=8, par=3}, Hole{holeId=45, number=9, par=3}], par=27, record=-2}, scores=[Score{scoreId=181, score=2, holePar=3}, Score{scoreId=182, score=4, holePar=3}, Score{scoreId=183, score=4, holePar=3}, Score{scoreId=184, score=2, holePar=3}, Score{scoreId=185, score=3, holePar=3}, Score{scoreId=186, score=2, holePar=3}, Score{scoreId=187, score=3, holePar=3}, Score{scoreId=188, score=3, holePar=3}, Score{scoreId=189, score=3, holePar=3}], roundDate=2023-03-04 00:00:00.0, total=26}, Round{roundId=20, course=Course{id=1, name=&#39;Legende&#39;, holes=[Hole{holeId=37, number=1, par=3}, Hole{holeId=38, number=2, par=3}, Hole{holeId=39, number=3, par=3}, Hole{holeId=40, number=4, par=3}, Hole{holeId=41, number=5, par=3}, Hole{holeId=42, number=6, par=3}, Hole{holeId=43, number=7, par=3}, Hole{holeId=44, number=8, par=3}, Hole{holeId=45, number=9, par=3}], par=27, record=-2}, scores=[Score{scoreId=190, score=4, holePar=3}, Score{scoreId=191, score=4, holePar=3}, Score{scoreId=192, score=3, holePar=3}, Score{scoreId=193, score=3, holePar=3}, Score{scoreId=194, score=3, holePar=3}, Score{scoreId=195, score=3, holePar=3}, Score{scoreId=196, score=4, holePar=3}, Score{scoreId=197, score=3, holePar=3}, Score{scoreId=198, score=3, holePar=3}], roundDate=2023-03-04 00:00:00.0, total=30}, Round{roundId=21, course=Course{id=2, name=&#39;Ilsede&#39;, holes=[Hole{holeId=46, number=1, par=3}, Hole{holeId=47, number=2, par=3}, Hole{holeId=48, number=3, par=3}, Hole{holeId=49, number=4, par=3}, Hole{holeId=50, number=5, par=3}, Hole{holeId=51, number=6, par=3}, Hole{holeId=52, number=7, par=3}, Hole{holeId=53, number=8, par=3}, Hole{holeId=54, number=9, par=3}, Hole{holeId=55, number=10, par=3}, Hole{holeId=56, number=11, par=3}, Hole{holeId=57, number=12, par=3}, Hole{holeId=58, number=13, par=4}, Hole{holeId=59, number=14, par=3}, Hole{holeId=60, number=15, par=3}, Hole{holeId=61, number=16, par=3}, Hole{holeId=62, number=17, par=3}, Hole{holeId=63, number=18, par=3}], par=55, record=7}, scores=[Score{scoreId=199, score=3, holePar=3}, Score{scoreId=200, score=3, holePar=3}, Score{scoreId=201, score=3, holePar=3}, Score{scoreId=202, score=4, holePar=3}, Score{scoreId=203, score=3, holePar=3}, Score{scoreId=204, score=3, holePar=3}, Score{scoreId=205, score=2, holePar=3}, Score{scoreId=206, score=3, holePar=3}, Score{scoreId=207, score=3, holePar=3}, Score{scoreId=208, score=4, holePar=3}, Score{scoreId=209, score=3, holePar=3}, Score{scoreId=210, score=3, holePar=3}, Score{scoreId=211, score=2, holePar=3}, Score{scoreId=212, score=3, holePar=3}, Score{scoreId=213, score=3, holePar=3}, Score{scoreId=214, score=4, holePar=3}, Score{scoreId=215, score=3, holePar=3}, Score{scoreId=216, score=2, holePar=3}], roundDate=2023-03-01 00:00:00.0, total=54}]

Controller method

  1. @GetMapping(&quot;/rounds/{id}&quot;)
  2. public String roundsHome(@PathVariable(value = &quot;id&quot;) Long id,
  3. Model model) {
  4. List&lt;Course&gt; courses = courseService.getAllCourses();
  5. List&lt;Round&gt; rounds = userService.getUserById(id).getRounds();
  6. model.addAttribute(&quot;courses&quot;, courses);
  7. model.addAttribute(&quot;rounds&quot;, rounds);
  8. return &quot;/discgolf/round/rounds&quot;;
  9. }

html

  1. &lt;div class=&quot;container&quot;&gt;
  2. &lt;div&gt;
  3. &lt;a&gt;Rounds Played&lt;/a&gt;
  4. &lt;/div&gt;
  5. &lt;div id=&quot;accordion&quot;&gt;
  6. &lt;div th:each=&quot;round : ${rounds}&quot; class=&quot;card&quot;&gt;
  7. &lt;div class=&quot;card-header&quot; id=&quot;headingOne&quot;&gt;
  8. &lt;h5 class=&quot;mb-0&quot;&gt;
  9. &lt;button class=&quot;btn btn-link&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapseOne&quot; aria-expanded=&quot;true&quot; aria-controls=&quot;collapseOne&quot;&gt;
  10. &lt;div&gt;
  11. &lt;label&gt;Course: &lt;/label&gt;
  12. &lt;label th:text=&quot;${round.course.name}&quot;&gt;&lt;/label&gt;
  13. &lt;/div&gt;
  14. &lt;/button&gt;
  15. &lt;/h5&gt;
  16. &lt;/div&gt;
  17. &lt;div id=&quot;collapseOne&quot; class=&quot;collapse show&quot; aria-labelledby=&quot;headingOne&quot; data-parent=&quot;#accordion&quot;&gt;
  18. &lt;div class=&quot;card-body&quot;&gt;
  19. &lt;div class=&quot;d-flex flex-row&quot;&gt;
  20. &lt;div class=&quot;p-3&quot;&gt;
  21. &lt;label&gt;Date: &lt;/label&gt;
  22. &lt;label th:text=&quot;${#dates.format(round.roundDate, &#39;dd-MMM-yyyy&#39;)}&quot;&gt;&lt;/label&gt;
  23. &lt;/div&gt;
  24. &lt;div class=&quot;p-3&quot;&gt;
  25. &lt;label&gt;Score: &lt;/label&gt;
  26. &lt;label th:text=&quot;${round.total - round.course.par}&quot;&gt;&lt;/label&gt;
  27. &lt;/div&gt;
  28. &lt;/div&gt;
  29. &lt;br&gt;
  30. &lt;div &gt;
  31. &lt;table id=&quot;courseInfo&quot; class=&quot;table table-bordered w-auto&quot;&gt;
  32. &lt;th:block th:each=&quot;course : ${round.course}&quot;&gt;
  33. &lt;tr&gt;
  34. &lt;th th:text=&quot;${&#39;Hole&#39;}&quot;&gt;&lt;/th&gt;
  35. &lt;th th:each=&quot;hole : ${course.holes}&quot; th:text=&quot;${hole.number}&quot;&gt;&lt;/th&gt;
  36. &lt;th th:text=&quot;${&#39;Total&#39;}&quot;&gt;&lt;/th&gt;
  37. &lt;/tr&gt;
  38. &lt;tr&gt;
  39. &lt;td th:text=&quot;${&#39;Par&#39;}&quot;&gt;&lt;/td&gt;
  40. &lt;td th:each=&quot;par : ${course.holes}&quot; th:text=&quot;${par.par}&quot;&gt;&lt;/td&gt;
  41. &lt;td th:text=&quot;${course.par}&quot;&gt;&lt;/td&gt;
  42. &lt;/tr&gt;
  43. &lt;tr&gt;
  44. &lt;td th:text=&quot;${&#39;Score&#39;}&quot;&gt;&lt;/td&gt;
  45. &lt;th:block th:each=&quot;score : ${round.scores}&quot;&gt;
  46. &lt;td th:style=&quot;${score.score &gt; score.holePar}
  47. ? &#39;background-color: red&#39;
  48. : (${score.score &lt; score.holePar } ? &#39;background-color: blue&#39;
  49. : &#39;background-color: #eee&#39; ) &quot; th:text=&quot;${score.score}&quot;&gt;&lt;/td&gt;
  50. &lt;/th:block&gt;
  51. &lt;td th:text=&quot;${round.total}&quot;&gt;&lt;/td&gt;
  52. &lt;/tr&gt;
  53. &lt;/th:block&gt;
  54. &lt;/table&gt;
  55. &lt;br&gt;
  56. &lt;a th:href=&quot;@{/discgolf/deleteRound/{id}(id=${round.roundId})}&quot; title=&quot;Remove Course&quot;
  57. data-target=&quot;#deleteRoundModal&quot; class=&quot;table-link danger&quot; id=&quot;deleteRoundButton&quot; &gt;
  58. &lt;span id=&quot;deleteRound&quot; class=&quot;fa-stack&quot;&gt;
  59. &lt;i class=&quot;fa fa-square fa-stack-2x&quot;&gt;&lt;/i&gt;
  60. &lt;i class=&quot;fa fa-trash-o fa-stack-1x fa-inverse&quot; title=&quot;Delete this round&quot;&gt;&lt;/i&gt;
  61. &lt;/span&gt;
  62. &lt;/a&gt;
  63. &lt;/div&gt;
  64. &lt;/div&gt;
  65. &lt;/div&gt;
  66. &lt;/div&gt;
  67. &lt;/div&gt;
  68. &lt;/div&gt;

答案1

得分: 1

So I figured it out with using a Map. For other beginners this is great to learn.

  1. @GetMapping("/rounds/{id}")
  2. public String roundsHome(@PathVariable(value = "id") Long id,
  3. Model model) {
  4. List<Course> courses = courseService.getAllCourses();
  5. List<Round> rounds = userService.getUserById(id).getRounds();
  6. Map<Course, List<Round>> mapRoundsByCourse = rounds.stream().collect(Collectors.groupingBy(Round::getCourse));
  7. model.addAttribute("courses", courses);
  8. model.addAttribute("rounds", mapRoundsByCourse);
  9. return "/discgolf/round/rounds";
  10. }

Then I used the bottom answer here to render in my accordion.

英文:

So I figured it out with using a Map. For other beginners this is great to learn.

  1. @GetMapping(&quot;/rounds/{id}&quot;)
  2. public String roundsHome(@PathVariable(value = &quot;id&quot;) Long id,
  3. Model model) {
  4. List&lt;Course&gt; courses = courseService.getAllCourses();
  5. List&lt;Round&gt; rounds = userService.getUserById(id).getRounds();
  6. Map&lt;Course, List&lt;Round&gt;&gt; mapRoundsByCourse = rounds.stream().collect(Collectors.groupingBy(Round::getCourse));
  7. model.addAttribute(&quot;courses&quot;, courses);
  8. model.addAttribute(&quot;rounds&quot;, mapRoundsByCourse);
  9. return &quot;/discgolf/round/rounds&quot;;
  10. }

Then I used the bottom answer here to render in my accordion.

huangapple
  • 本文由 发表于 2023年3月7日 19:44:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定