사랑하애오
article thumbnail

이번에는 질문 목록에 "해당 질문에 달린 답변 개수"를 표시할 수 있는 기능을 추가해 보자.
코드의 분량은 많지 않지만, 게시판 서비스를 더욱 서비스답게 만들어 주는 기능이다.

답변 개수는 다음처럼 게시물 제목 바로 오른쪽에 표시하자.

[파일명:/sbb/src/main/resources/templates/question_list.html]

<code />
<html layout:decorate="~{layout}"> <div layout:fragment="content" class="container my-3"> <table class="table"> <thead class="table-dark"> <tr> <th>번호</th> <th>제목</th> <th>작성일시</th> </tr> </thead> <tbody> <tr th:each="question, loop : ${paging}"> <td th:text="${paging.getTotalElements - (paging.number * paging.size) - loop.index}"></td> <td> <a th:href="@{|/question/detail/${question.id}|}" th:text="${question.subject}"></a> <span class="text-danger small ms-2" th:if="${#lists.size(question.answerList) > 0}" th:text="${#lists.size(question.answerList)}"> </span> </td> <td th:text="${#temporals.format(question.createDate, 'yyyy-MM-dd HH:mm')}"></d> </tr> </tbody> </table> (... 생략 ...)

th:if="${#lists.size(question.answerList) > 0}"로 답변이 있는지 조사하고, 
th:text="${#lists.size(question.answerList)}"로 답변 개수를 표시했다.

#list.size(이터러블객체)는 이터러블 객체의 사이즈를 반환하는 타임리프의 유틸리티이다.

이제 답변이 있는 질문은 다음처럼
제목 오른쪽에 빨간색(text-danger) 숫자가 작게(small) 왼쪽 여백(ms-2)이 추가되어 표시된다.

profile

사랑하애오

@사랑하애

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!