ํ์ฌ ์ํฉ
๊ฐ ์นดํ ๊ณ ๋ฆฌ์ ๋ง๊ฒ ๋ฒํผ์ด ๋์ด์๊ณ ์นดํ ๊ณ ๋ฆฌ๋ฅผ ํด๋ฆญํ๊ณ ๊ฒ์ ๋ฒํผ์ ๋๋ฅด๋ฉด ๊ทธ์ ๋ง๋ ๊ฒ์๋ฌผ์ด ์๋์ ๋ ๋๋ง๋๊ฒ ํ๊ณ ์ถ๋ค
์ฌ๊ธฐ์ ํน์ด์ฌํญ์ ์นดํ ๊ณ ๋ฆฌ ๋ฒํผ์ ์ฌ๋ฌ๊ฐ๊ฐ ๋๋ฆด ์ ์์ด์ผ ํ๋ค๋ ๊ฒ!
1. ์ฒซ ๋ฒ์งธ ๋ฐฉ๋ฒ
๋ด๊ฐ ์๊ฐํ ์ฒซ๋ฒ์งธ ๋ฐฉ๋ฒ์ ajax๋ฅผ ์ด์ฉํด์ ํด๋ฆญ๋ ์นดํ ๊ณ ๋ฆฌ ๋ฒํผ์ ๋ฐฐ์ด์ ์ ์ฅํ๋ ๊ฒ์ด๋ค.
list.html์ ์ผ๋ถ์ด๋ค. id๋ ์นดํ ๊ณ ๋ฆฌ์ id๋ก ์ง์ ํ๊ณ , ํด๋ฆญ์ id๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๋๊ฒจ์ฃผ๋ onClick ํจ์๋ฅผ ํธ์ถํ๋ค.
<!-- list.html์ script ์ผ๋ถ -->
<div class="gather-category">
<div th:each="category,index:${categoryList}">
<label><input type="button" name="category" class="btn btn-outline-info" th:value="${category.tagName}" th:id="${category.id}" onclick="onClick(this.id)"></label>
</div>
</div>
onClick ํจ์๋ถ๋ถ์ ์ฒ๋ฆฌ๋ ๋ค์๊ณผ ๊ฐ๋ค
<!-- list.html์ script ์ผ๋ถ -->
<script>
let arr=[];
function onClick(id) {
var button = document.getElementById(id);
button.classList.toggle('active');
var idx = arr.indexOf(id);
if(idx > -1) {
arr.splice(idx, 1);
} else {
arr.push(id);
}
}
$(document).on("click", "#searchBtn", function () {
$.ajax({
url: "/gathering/search",
type: "GET",
contentType: "application/json; charset=UTF-8",
dataType: "json",
data: {
"category": arr,
"classification": $("#classification option:selected").val(),
"searchKeyword" : $("#searchKeyword").val(),
},
success: function (data) {
//console.log(data.resultData);
arr = [];
if (data.result == true) {
alert("์ฑ๊ณต");
}
}
});
});
</script>
์ด๋ ๊ฒ arr์ด๋ผ๋ ๋ฐฐ์ด์ ์ ์ธํ๊ณ ํด๋ฆญ์ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์ id๋ฅผ arr์ ๋ฃ์ด์ค๋ค.
๋ฒํผ์ด bootstrap์ผ๋ก ๊พธ๋ฉฐ์ ธ์์ด์ ํด๋ฆญํจ๊ณผ๋ฅผ ์ฃผ๊ธฐ ์ํด์ active๋ฅผ ํ ๊ธํ๋ ๋ฐฉ์์ผ๋ก ๋ฐฐ๊ฒฝ์์ ์ ํ๊ณ , arr ๋ฐฐ์ด์ ํด๋น id๊ฐ์ด ๋ค์ด๊ฐ์์ผ๋ฉด ๋์ด๊ฐ๊ณ , ์๋ ๊ฒฝ์ฐ์ ๋ฐฐ์ด์ ๋ฃ์ด์ค๋ค.
๋ฐ๋ผ์ ๊ฒ์ ๋ฒํผ์ด ๋๋ ธ์๋, ajax๋ก arr ๋ฐฐ์ด์ “category”๋ผ๋ ๊ฐ์ผ๋ก ๋ณด๋ธ๋ค!
์์ฒญ์ ๋ฐ๋ Controller ๋ถ๋ถ์ด๋ค.
//GatheringListController.java
@GetMapping("/search")
public ModelAndView searchBoard(ModelAndView mav, @ModelAttribute GatheringListCriteria criteria){
mav.setViewName("jsonView");
System.out.println(criteria.toString());
List<GatheringListDetailDto> gatheringBoardList = gatheringService.listByCriteria(criteria);
for(GatheringListDetailDto dto : gatheringBoardList) {
System.out.println(dto.toString());
}
mav.addObject("gatheringList", gatheringBoardList);
return mav;
}
๋ก๊ทธ๋ฅผ ๋ณด๋ ๋ด๊ฐ ์๋ํ๋ ๋๋ก category ๊ฐ์ด List<Integer>์ ํํ๋ก ์ ๋ค์ด๊ฐ์์์ ํ์ธํ ์ ์๋ค.
๊ทธ๋ฐ๋ฐ ์ด ๋ฐฉ์์ ๋ฌธ์ ์ ์ด ์๋ค.
- ๊ฐ๋
์ฑ์ด ๋๋ฌด ๊ตฌ๋ฆฌ๋คโญ๏ธ(์ด๊ฒ ์ ์ผ ํต์ฌ์ด๋ค)
→ ์ ์ง๋ณด์๊ฐ ํ๋ค ๊ฒ ๊ฐ๋ค.
์ง๊ธ ์ด ํฌ์คํ ์ ํ๋ฉด์๋ ๋ด๊ฐ ๋ฌด์จ ์ฝ๋๋ฅผ ์งฐ๋์ง ๊ธฐ์ต์ด ์๋์ ๊ตฌ๊ธ๋งํ๋ค.. - ์ง๊ธ ์ด ๋ฐฉ์์ ์๋ฒ๊ฐ ์ฒ๋ฆฌํ ๊ฒฐ๊ณผ ๋ฐ์ดํฐ๋ฅผ ๋ค์ ํด๋ผ์ด์ธํธ ์ชฝ์์ ๊ฐ์ ์ฌ๋ฐฐ์นํด์ค์ผ ํ๋ค.
form ํํ๋ก ๋ณด๋ด๋ฉด ์์์ ์ฒ๋ฆฌํด์ฃผ๋๊น ์ง๊ธ ๋ฐฉ์์ด ํจ์จ์ ์ธ ๋ฐฉ์์ด ์๋๋ผ๋ ์๊ฐ์ด ๋ค์๋ค.
๊ฒ์๋ง์ ์ํ /search ํจ์๋ฅผ ๋ง๋๋๊ฒ๋ณด๋ค๋ ๊ธฐ์กด์ ์๋ list์์ ํํฐ๋ง๊ณผ ๊ด๋ จ๋ ๋ชจ๋ ๋ก์ง์ ์ฒ๋ฆฌํ๋๊ฒ์ด ์ข์ ๊ฒ ๊ฐ๋ค.
2. ๋ ๋ฒ์งธ ๋ฐฉ๋ฒ
์ด ๋ฐฉ๋ฒ์ผ๋ก ๊ฐ๊ฒ ๊ฐ๋ค! ๊ธฐ์กด์ ๋ฐฉ์์ ๊ฒฐ๊ณผ๋ฐ์ดํฐ๋ฅผ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ์ง์ ๋ํด์ ๊ณ ๋ฏผํ๋ค๊ฐ ๊ฐ๋ ์ฑ ์ด์๋ก ๊ณผ๊ฐํ๊ฒ ํฌ๊ธฐํ๊ณ , form์ ํํ๋ก ์ฒ๋ฆฌํ๊ธฐ๋ก ํ๋ค.
๊ธฐ์กด์ ๋ด๊ฐ form ๋ฐฉ์์ ๊ณ ๋ คํ๋ค๊ฐ ํฌ๊ธฐํ ์ด์ ๋ ๋ค์๊ณผ ๊ฐ๋ค.
๋ฒํผ์ ์๋ ๊ฐ์ Listํํ๋ก ๋ณด๋ด์ผํ๋๋ฐ ๊ฐ์ id๋ฅผ ๊ฐ์ง๊ณ ์๋ ๋ฐ์ดํฐ๋ฅผ list์ ํํ๋ก ๋ณด๋ด๋๊ฒ์ด checkbox ๋ง๊ณ ๋ ์๋๋ ๊ฑธ๋ก ์๊ณ ์๊ธฐ ๋๋ฌธ์ด๋ค.
์๋ฌด๋ฆฌ ๊ตฌ๊ธ๋ง์ ํด๋ด๋ ์ ๋ชจ๋ฅด๊ฒ ์ด์.. ๊ทธ๋ฅ ํด๋ฆญ ์ก์ ์ฒ๋ฆฌํ ๊ฒธ ajax๋ฅผ ์ด๊ฑด๋ฐ checkbox๋ฅผ ํ๋ฒ ์ ์จ๊ฒจ์ ์ฒ๋ฆฌํด๋ณด์๋ ํผ๋๋ฐฑ์ ๋ฐ์๋ค!
See the Pen [ํฌ์คํ ] checkbox๋ฅผ ์จ๊ฒจ์ ํด๋ฆญ์ด๋ฒคํธ ์ฒ๋ฆฌํ๊ธฐ by ๋ฐ๋ค์ (@lion0913) on CodePen.
๋ด๊ฐ ์ฐธ๊ณ ํ ๋ฐฉ์์ด๋ค. ์ด๋ ๊ฒ ๋ณด๋ฉด ์ดํด๊ฐ ํ๋ค ์๋ ์์ง๋ง opacity๋ฅผ ์ฃผ์์ฒ๋ฆฌํ๋ฉด


์ด๋ ๊ฒ!
์ด๋ ๊ฒ ์ฒดํฌ๋ฐ์ค๋ฅผ ์ฒ๋ฆฌํด์ฃผ๋ฉด form์ list ํํ๋ก ๋ณด๋ผ ์ ์๊ณ , ๋ฒํผ์ ๋ชจ์ ๊ทธ๋๋ก๋ฅผ ์ฑ๊ธธ ์ ์์ด์ ๋ด๊ฐ ์๋ํ ๊ทธ๋ฆผ๊ณผ ์ผ์นํ๋น
๊ทธ๋ ๋ค๋ฉด ์๋ฅผ ์ด์ ํ๋ก์ ํธ์ ์ ์ฉํด๋ณด์!
๊ณผ๊ฐํ๊ฒ ajax ์ฒ๋ฆฌ๋ถ๋ถ์ ์ญ์ ํด๋ฒ๋ฆฌ๊ณ list.html ๋ถ๋ถ์ ์๋ดค๋ค.
<div th:each="category,index:${categoryList}">
<input type="checkbox" name="category" th:value="${category.id}">
<div th:text="${category.tagName}" type="button" class="btn btn-outline-info"></div>
</div>
codepen์ ์ฌ๋ฆฐ๋๋ก input๊ณผ div๋ฅผ ํ์์ผ๋ก ๋๊ณ , input์ ํ์ ์ checkbox๋ก ์ค์ ํด list ํํ๋ฅผ ์ฒ๋ฆฌํ ์ ์๊ฒ ํ๋ค. ๊ทธ๋ฆฌ๊ณ ์ด input์ด ์๋ฒ์๊ฒ ๋๊ฒจ์ค ๊ฐ์ธ id๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
๊ฐ์ ๋ผ์ธ์ ๋ฒํผ์ ๊พธ๋ฉฐ์ฃผ๊ธฐ์ํด์ ๋ฒํผ์ ๋ฃ๊ณ ๋ณธ๊ฒฉ์ ์ผ๋ก css ์ ํ๊ธฐ๋ฅผ ์์ํ๋ค..
codepen์์์ css ์ฝ๋์ ํฌ๊ฒ ๋ฌ๋ผ์ง ์ ์ ์๋ค. ์๊น๊ณผ ํ ๋๋ฆฌ ๋ฅ๊ธ๊ธฐ์ ๋??
.gather-category input[type="checkbox"]:checked + div {
color:white;
background-color: #01C9C6;
}
.gather-category > div {
display: inline-block;
position: relative;
}
.gather-category > div > input[type="checkbox"] {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
}
.gather-category > div > div {
color:#01C9C6;
font-size: 14px;
outline-color: #01C9C6;
margin-right: 15px;
margin-bottom: 10px;
padding: 10px 20px;
border-radius: 25px;
white-space: nowrap;
}
position์ relative, absolute ๊ด๊ณ๋ก ๊ฐ๊ฐ ๋๋ฉด์, ๋ด๋ถ ๋ฐ์ค๊ฐ button ์์ ๋ค์ด๊ฐ๊ฒ ํ๊ณ , checkbox์ ํฌ๋ช ๋๋ฅผ ์ต๋๋ก ํ๋ค๊ณ ์์ฝํ ์ ์๋ค..ใ ใ (css ์ฝ๋๊ฐ ๋๋ฌด ์ง์ ๋ถํ๋ฐ ๊ฑด๋๋ฆด ์๋๊ฐ ๋์ง ์๋๋ค)
์ด๋ ๊ฒ ํด์ ์ฝ๋๋ฅผ ์คํ์ํค๋ฉด!

ํด๋ฆญ ์ ์๊น์ด ์ด๋ ๊ฒ ์์๊ฒ ๋ค์ด๊ฐ๋ค…ใ ใ ๋๋ฌด ๊ท์ฝ๋ค!

๊ฒฐ๊ณผํ๋ฉด์ด๋ค!! ๋ชป์๊ธด ๊ฒ์ํ๋ฉด ๋ฐฐ์น๋ ๋ฌด์ํด์ฃผ๊ธธ ๋ฐ๋๋ค.. ๊ณง ์์๊ฒ ๊พธ๋ฉฐ์ค๊ฑฐ๋ค.
'๐ป Project > project' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[DB] Mysql ํ๊ธ๊นจ์ง, Mariadb ํ๊ธ๊นจ์ง ํด๊ฒฐ๋ฐฉ๋ฒ (0) | 2022.09.06 |
---|