๋ชฉ์ฐจ
ajax๋ก ํ์ด์ง์ ๊ตฌํ ํ๋๋ฐ ํ์ด์ง ์ด๋์ด ๋์๋ค๊ฐ ๋ค์ ๊ฐ์ ํ์ด์ง๊ฐ ํธ์ถ์ด ๋์๋ค.
์ฝ์ ๋ณด๋ฉด 2ํ์ด์ง ๋ฐ์ดํฐ๋ฅผ ์ ์์ ์ผ๋ก ๊ฐ์ ธ์๋ค๊ฐ ๋ค์ 1ํ์ด์ง๊ฐ ํธ์ถ์ด ๋๊ณ ์๋ค.
๊ฐ๋จํ ์์ธ ์ด์๋๋ฐ ๊ฝค ๋ง์ ์๊ฐ ์ฝ์ง ๋์ ๋์ค์ ๋ค์ ๋ด์ผ์ง ํ๊ณ ๋์ด ๊ฐ์๋ค๊ฐ ๋ค์ ๋ด์ ํด๊ฒฐ ํ๋ค.
before source ๐
/**
* ํ์ด์ง๋ค์ด์
์ ๊ทธ๋ฆฐ๋ค.
* @param paging
*/
function drawPagination(paging) {
let pageHtml = "";
pageHtml += "<ul class='pagination'>";
//first
const first = parseInt(paging.firstPage);
pageHtml += "<li class='paginate_button page-item first'>";
if (paging.currentPage === paging.firstPage) {
pageHtml += "<button class='page-link' disabled>";
} else {
pageHtml += "<button class='page-link' style='cursor: pointer;' onclick='movePage("+ first +")'>";
}
pageHtml += "<span class='hidden'>first page</span>";
pageHtml += "</button>";
pageHtml += "</li>"
//prev
const prev = parseInt(paging.currentPage) -1;
pageHtml += "<li class='paginate_button page-item prev'>";
if (paging.firstPage === paging.currentPage) {
pageHtml += "<button class='page-link' disabled>";
} else {
pageHtml += "<button class='page-link' style='cursor: pointer;' onclick='movePage("+ prev +")'>";
}
pageHtml += "<span class='hidden'>prev page</span>";
pageHtml += "</button>";
pageHtml += "</li>";
//โ ~ โฉ
for (let i = paging.firstPageNo; i <= paging.lastPageNo; i++) {
pageHtml += "<li class='paginate_button page-item";
//ํ์ฌ ํ์ด์ง๊ฐ ์ธ๋ฑ์ค์ ๊ฐ์ผ๋ฉด active class ์ถ๊ฐ
if (paging.currentPage === i) {
pageHtml += " active'>";
pageHtml += "<button class='page-link' style='cursor: pointer;' onclick='movePage("+ i +")'>";
pageHtml += i;
pageHtml += "</button>";
pageHtml += "</li>"
} else {
pageHtml += "'>";
pageHtml += "<button class='page-link' style='cursor: pointer;' onclick='movePage("+ i +")'>";
pageHtml += i;
pageHtml += "</button>";
pageHtml += "</li>"
}
}
//next
const next = parseInt(paging.currentPage) + 1;
pageHtml += "<li class='paginate_button page-item next'>";
if (paging.currentPage === paging.lastPage) {
pageHtml += "<button class='page-link' disabled>";
} else {
pageHtml += "<button class='page-link' style='cursor: pointer;' onclick='movePage("+ next +")'>";
}
pageHtml += "<span class='hidden'>next page</span>";
pageHtml += "</button>";
pageHtml += "</li>"
//last
const last = parseInt(paging.lastPage);
pageHtml += "<li class='paginate_button page-item last'>";
if (paging.currentPage === paging.lastPage) {
pageHtml += "<button class='page-link' disabled>";
} else {
pageHtml += "<button class='page-link' style='cursor: pointer;' onclick='movePage("+ last +")'>";
}
pageHtml += "<span class='hidden'>last page</span>";
pageHtml += "</button>";
pageHtml += "</li>"
pageHtml += "</ul>";
//ํ์ด์ง๋ค์ด์
์์ญ์ ๋ฐ์
$(".tbl-paging").html(pageHtml);
}
after source ๐
/**
* ํ์ด์ง๋ค์ด์
์ ๊ทธ๋ฆฐ๋ค.
* @param paging
*/
function drawPagination(paging) {
let pageHtml = "";
pageHtml += "<ul class='pagination'>";
//first
const first = parseInt(paging.firstPage);
pageHtml += "<li class='paginate_button page-item first'>";
if (paging.currentPage === paging.firstPage) {
pageHtml += "<button type='button' class='page-link' disabled>";
} else {
pageHtml += "<button type='button' class='page-link' style='cursor: pointer;' onclick='movePage("+ first +")'>";
}
pageHtml += "<span class='hidden'>first page</span>";
pageHtml += "</button>";
pageHtml += "</li>"
//prev
const prev = parseInt(paging.currentPage) -1;
pageHtml += "<li class='paginate_button page-item prev'>";
if (paging.firstPage === paging.currentPage) {
pageHtml += "<button type='button' class='page-link' disabled>";
} else {
pageHtml += "<button type='button' class='page-link' style='cursor: pointer;' onclick='movePage("+ prev +")'>";
}
pageHtml += "<span class='hidden'>prev page</span>";
pageHtml += "</button>";
pageHtml += "</li>";
//โ ~ โฉ
for (let i = paging.firstPageNo; i <= paging.lastPageNo; i++) {
pageHtml += "<li class='paginate_button page-item";
//ํ์ฌ ํ์ด์ง๊ฐ ์ธ๋ฑ์ค์ ๊ฐ์ผ๋ฉด active class ์ถ๊ฐ
if (paging.currentPage === i) {
pageHtml += " active'>";
pageHtml += "<button type='button' class='page-link' style='cursor: pointer;' onclick='movePage("+ i +")'>";
pageHtml += i;
pageHtml += "</button>";
pageHtml += "</li>"
} else {
pageHtml += "'>";
pageHtml += "<button type='button' class='page-link' style='cursor: pointer;' onclick='movePage("+ i +")'>";
pageHtml += i;
pageHtml += "</button>";
pageHtml += "</li>"
}
}
//next
const next = parseInt(paging.currentPage) + 1;
pageHtml += "<li class='paginate_button page-item next'>";
if (paging.currentPage === paging.lastPage) {
pageHtml += "<button type='button' class='page-link' disabled>";
} else {
pageHtml += "<button type='button' class='page-link' style='cursor: pointer;' onclick='movePage("+ next +")'>";
}
pageHtml += "<span class='hidden'>next page</span>";
pageHtml += "</button>";
pageHtml += "</li>"
//last
const last = parseInt(paging.lastPage);
pageHtml += "<li class='paginate_button page-item last'>";
if (paging.currentPage === paging.lastPage) {
pageHtml += "<button type='button' class='page-link' disabled>";
} else {
pageHtml += "<button type='button' class='page-link' style='cursor: pointer;' onclick='movePage("+ last +")'>";
}
pageHtml += "<span class='hidden'>last page</span>";
pageHtml += "</button>";
pageHtml += "</li>"
pageHtml += "</ul>";
//ํ์ด์ง๋ค์ด์
์์ญ์ ๋ฐ์
$(".tbl-paging").html(pageHtml);
}
๋ฌ๋ผ์ง ๋ถ๋ถ์ ๋์ ์ผ๋ก ๋ง๋ button์ type์ button์ผ๋ก ์ค์ ํ๋ค.
์ด๋ฒ์ ์๊ฒ๋ ์ฌ์ค์ธ๋ฐ button์ type์ ๋ช ์ํ์ง ์์ ๊ฒฝ์ฐ default๊ฐ์ submit์ด๋ผ๊ณ ํ๋ค.
๊ทธ๋ฌ๋ ๋ฒํผ์ ๋๋ฅผ ๋๋ง๋ค ajax๋ก 2ํ์ด์ง๋ฅผ ๊ฐ์ ธ์จ ๋ค์ form submit์ ํ๋ฒ ๋ ํ๊ฒ ๋๊ฑฐ๋ค.
form action์ ์๋ฌด๊ฐ๋ ์ฃผ์ง ์์๊ธฐ์ ํ์ฌ ํ์ด์ง๋ก submit์ ํ์์ ๊ณ์ ๊ฐ์ ํ์ด์ง๊ฐ ๋ก๋ ๋ ๊ฑฐ๋ค.
์ฌ์ค์ ์๊ฒ ๋ ํ form action์ ์กด์ฌํ์ง ์๋ ์ฃผ์๋ฅผ ์คฌ๋๋ ์์๋๋ก 404 ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
์ด ์ฝ์ง์ ํตํด ๋๋ ๊ตํ: button์ ๋ง๋ค ๋ ๋ช ์์ ์ผ๋ก ํญ์ type์ ์ ์ธํ๋๋ก ํ์.
์ ์ฒด ์์ค(์ํ์ฉ)
๋ฆฌํฉํ ๋ง X
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<style>
li {
list-style: none;
float: left;
}
</style>
</head>
<body>
<form id="searchForm">
<select name="type" id="type">
<option value="">์ ์ฒด</option>
<option value="T">์ ๋ชฉ</option>
<option value="C">๋ด์ฉ</option>
<option value="W">์์ฑ์</option>
</select>
<input type="text" name="keyword" id="keyword">
<button id="searchBtn">๊ฒ์</button>
<a th:href="@{/board/reg}">๊ธ์ฐ๊ธฐ</a>
<div id="count"></div>
<table>
<thead>
<th>๋ฒํธ</th>
<th>์ ๋ชฉ</th>
<th>๋ด์ฉ</th>
<th>์์ฑ์</th>
<th>๋ฑ๋ก์ผ</th>
</thead>
<tbody id="boardBody"></tbody>
</table>
<div class="tbl-paging"></div>
</form>
<script th:inline="javascript" type="text/javascript">
//๊ตฌ๋ถ
const menu = "board";
//๊ฒ์์กฐ๊ฑด
let searchData = "";
$(function () {
getList("", 1, menu);
});
//๊ฒ์
$("#searchBtn").on("click", function () {
searchData = $('#searchForm').serialize();
getList(searchData, 1, menu);
});
/**
* ํ์ด์ง ์ด๋
* @param currentPage
*/
function movePage(currentPage){
//๊ฒ์์กฐ๊ฑด์ด ์์ ๊ฒฝ์ฐ์๋ง ๊ฒ์์กฐ๊ฑด ์ถ๊ฐ(๊ฒ์์กฐ๊ฑด ์ ์งํ ์ฑ ํ์ด์ง ์ด๋)
(searchData != null) ? getList(searchData, currentPage, menu) : getList("", currentPage, menu);
}
/**
* ajax ํต์ ์ผ๋ก ๋ฆฌ์คํธ๋ฅผ ์กฐํํ๋ค.
* @param searchParam ๊ฒ์์กฐ๊ฑด
* @param currentPage ํ์ฌ ํ์ด์ง
* @param type ๋ฉ๋ดํ์
*/
function getList(searchParam, currentPage, type){
console.log("getList!!!!");
$.ajax({
url: "/api/" + type +"?currentPage=" + currentPage,
type:"get",
//IE ๋ธ๋ผ์ฐ์ ์ฌ์ฉ ์ํ๋ค๋ ๊ฐ์ ํ์ ์ฃผ์์ฒ๋ฆฌ
// cache: false,
data: searchParam,
success: function (data){
//draw tbody
drawTbody(data, type);
//draw pagination
drawPagination(data.paging);
},
error:function(e){
}
});
}
/**
* tbody์ html์ ๋ฎ์ด์ด๋ค.
* @param data
* @param gubun
*/
function drawTbody(data, gubun) {
//drawHtml, draw ๋์ tbody
let htmlData, targetTbody = "";
targetTbody = "boardBody";
// foreach start
for (let i = 0; i < data.list.length; i++) {
htmlData += "<tr ";
htmlData += "data-sno=" + data.list[i].boardSno + ">";
htmlData += "<td>";
htmlData += data.list[i].no;
htmlData += "</td>";
htmlData += "<td>";
htmlData += "<a href='#' onclick='detail(\"" + data.list[i].boardSno + "\")'>";
htmlData += data.list[i].title;
htmlData += "</a>";
htmlData += "</td>";
htmlData += "<td>";
htmlData += data.list[i].content;
htmlData += "</td>";
htmlData += "<td>";
htmlData += data.list[i].userId;
htmlData += "</td>";
htmlData += "<td>";
htmlData += data.list[i].regDate;
htmlData += "</td>";
htmlData += "<td>";
htmlData += "<a href='#' onclick='delBoard(\"" + data.list[i].boardSno + "\")'>์ญ์ </a>";
htmlData += "</td>";
htmlData += "</tr>";
}
// foreach end
//tbody์ ๋ฐ์
$("#" + targetTbody + "").html(htmlData);
countHtml = "";
countHtml += data.count + "๊ฑด";
$("#count").text(countHtml);
}
// end function getList()
//์์ธ๋ณด๊ธฐ
function detail(sno) {
location.href = "/board/mod/" + sno;
}
function delBoard(sno) {
$.ajax({
url: "/api/board/" + sno,
type:"delete",
//IE ๋ธ๋ผ์ฐ์ ์ฌ์ฉ ์ํ๋ค๋ ๊ฐ์ ํ์ ์ฃผ์์ฒ๋ฆฌ
// cache: false,
success: function (data) {
if(data.code === "1") alert("์ญ์ ๋์์ต๋๋ค.");
location.href = "/board"
},
error:function(e){
console.log(e);
}
});
}
/**
* ํ์ด์ง๋ค์ด์
์ ๊ทธ๋ฆฐ๋ค.
* @param paging
*/
function drawPagination(paging) {
let pageHtml = "";
pageHtml += "<ul class='pagination'>";
//first
const first = parseInt(paging.firstPage);
pageHtml += "<li class='paginate_button page-item first'>";
if (paging.currentPage === paging.firstPage) {
pageHtml += "<button type='button' class='page-link' disabled>";
} else {
pageHtml += "<button type='button' class='page-link' style='cursor: pointer;' onclick='movePage("+ first +")'>";
}
pageHtml += "<span class='hidden'>first page</span>";
pageHtml += "</button>";
pageHtml += "</li>"
//prev
const prev = parseInt(paging.currentPage) -1;
pageHtml += "<li class='paginate_button page-item prev'>";
if (paging.firstPage === paging.currentPage) {
pageHtml += "<button type='button' class='page-link' disabled>";
} else {
pageHtml += "<button type='button' class='page-link' style='cursor: pointer;' onclick='movePage("+ prev +")'>";
}
pageHtml += "<span class='hidden'>prev page</span>";
pageHtml += "</button>";
pageHtml += "</li>";
//โ ~ โฉ
for (let i = paging.firstPageNo; i <= paging.lastPageNo; i++) {
pageHtml += "<li class='paginate_button page-item";
//ํ์ฌ ํ์ด์ง๊ฐ ์ธ๋ฑ์ค์ ๊ฐ์ผ๋ฉด active class ์ถ๊ฐ
if (paging.currentPage === i) {
pageHtml += " active'>";
pageHtml += "<button type='button' class='page-link' style='cursor: pointer;' onclick='movePage("+ i +")'>";
pageHtml += i;
pageHtml += "</button>";
pageHtml += "</li>"
} else {
pageHtml += "'>";
pageHtml += "<button type='button' class='page-link' style='cursor: pointer;' onclick='movePage("+ i +")'>";
pageHtml += i;
pageHtml += "</button>";
pageHtml += "</li>"
}
}
//next
const next = parseInt(paging.currentPage) + 1;
pageHtml += "<li class='paginate_button page-item next'>";
if (paging.currentPage === paging.lastPage) {
pageHtml += "<button type='button' class='page-link' disabled>";
} else {
pageHtml += "<button type='button' class='page-link' style='cursor: pointer;' onclick='movePage("+ next +")'>";
}
pageHtml += "<span class='hidden'>next page</span>";
pageHtml += "</button>";
pageHtml += "</li>"
//last
const last = parseInt(paging.lastPage);
pageHtml += "<li class='paginate_button page-item last'>";
if (paging.currentPage === paging.lastPage) {
pageHtml += "<button type='button' class='page-link' disabled>";
} else {
pageHtml += "<button type='button' class='page-link' style='cursor: pointer;' onclick='movePage("+ last +")'>";
}
pageHtml += "<span class='hidden'>last page</span>";
pageHtml += "</button>";
pageHtml += "</li>"
pageHtml += "</ul>";
//ํ์ด์ง๋ค์ด์
์์ญ์ ๋ฐ์
$(".tbl-paging").html(pageHtml);
}
</script>
</body>
</html>
๊ฐ์ธ ์คํฐ๋ ๊ธฐ๋ก์ ๋ฉ๋ชจํ๋ ๊ณต๊ฐ์ด๋ผ ํ๋ฆฐ์ ์ด ์์ ์ ์์ต๋๋ค.
ํ๋ฆฐ ์ ์์ ๊ฒฝ์ฐ ๋๊ธ ๋ถํ๋๋ฆฝ๋๋ค.
'IT > development' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ChatGPT] ChatGPT ๊ณ ๋ง๋ค. (0) | 2023.07.22 |
---|---|
[React.js] state ์ฌ์ฉ๋ฒ (0) | 2023.07.13 |
[dbeaver] dbeaver DDL, DML ์์ฑ (0) | 2023.07.08 |
[JavaScript] ๋์ ์๋ฆฌ๋จผํธ onclick ๋ฌธ์์ด (0) | 2023.07.05 |
[JavaScript] validation (0) | 2023.06.25 |
๋๊ธ