해당 게시글은 개인 프로젝트인 "데이트 장소 다이어리 제작" 중
#22 "덧글 기능 적용" 이슈를 다루고 있습니다.
<!-- 조건이 걸려 있어 조건에 부합할 경우 해당 버튼 표시가 안 됨 -->
<a th:if="${user != null && user.username == dto.uid}">
<button type="button" th:if="${user != null && user.username == dto.uid}"
class="btn btn-danger deleteBtn">게시글 삭제</button>
</a>
// 게시글 삭제
// thymeleaf 조건문으로 인해 deleteBtn이 출력되지 않을 수 있어 null check를 해야 함
// (안 그러면 addEventListener null 처리되어 전체 JS 코드가 실행되지 않는다)
if (document.querySelector(".deleteBtn") != null) {
document.querySelector(".deleteBtn").addEventListener("click", function (e) {
e.preventDefault()
e.stopPropagation()
$.post('/diary/delete', {aid: aid})
alert("게시글이 삭제되었습니다.")
$.get('/diary/list')
location.href = "/diary/list"
}, false)
}