π€ Pagination?
νμ΄μ§λ€μ΄μ μ΄λ μ½ν μΈ λ₯Ό μ¬λ¬ νμ΄μ§κ³ λλκ³ , μ΄μ νΉμ λ€μ νμ΄μ§λ‘ λμ΄κ°κ±°λ νΉμ νμ΄μ§λ‘ λμ΄κ° μ μλ μΌλ ¨μ λ§ν¬λ₯Ό νμ΄μ§ μλ¨μ΄λ νλ¨μ λ°°μΉνλ λ°©λ²μ λλ€.
β νμν Pagination 쑰건
κ²μνμ λ μ½κ² λ³Ό μ μκ³ μ¬μ©μμ μ κ·Όμ±μ λμΌ μ μκ² Pagnation
μ μ μ©νκΈ°λ‘ νμ΅λλ€.πͺ
Pagination
μ ꡬμνλ©΄μ νμν μꡬ 쑰건λ€μ μ΄ λ€ κ°μ§μ
λλ€.
1 | ν νμ΄μ§μ νμ΄μ§ λ§ν¬λ 10κ°λ‘ 보μ¬μ€λ€. |
2 | ν νμ΄μ§μ 20κ°μ© κ²μλ¬Όμ 보μ¬μ€λ€. |
3 | μ΄μ , λ€μ λ²νΌμ΄ μ‘΄μ¬νλ€. |
4 | μ²μμΌλ‘, λ§μ§λ§μΌλ‘ λ²νΌμ΄ μ‘΄μ¬νλ€. |
π©π» JSλ‘ Pagination κ°λ°
[1. ν νμ΄μ§μ νμ΄μ§ λ§ν¬λ 10κ°λ‘ 보μ¬μ€λ€.]
total : 462
1 ... 10
11 ... 20
21 ... 23
μμ κ°μ΄ νμ΄μ§λ€μ΄μ
μ μμ±νκΈ° μν΄μλ λ€ κ°μ§μ κ°
μ΄ νμν©λλ€.
β νλ©΄μ 보μ¬μ§ νμ΄μ§ κ·Έλ£Ή
β νλ©΄μ 보μ¬μ§ 첫λ²μ§Έ νμ΄μ§
β νλ©΄μ 보μ¬μ§ λ§μ§λ§ νμ΄μ§
β μ΄ νμ΄μ§ μ
π μ΄ νμ΄μ§ μ
μ νλ©΄μ 보μ¬μ§ νμ΄μ§ κ·Έλ£Ή
μ κ²μλ¬Ό μ 체 κ°μμ νμ¬ νμ΄μ§λ₯Ό μ΄μ©ν΄ ꡬν©λλ€.
μ΄ νμ΄μ§ μ = Math.ceil(μ 체 κ°μ/ ν νμ΄μ§μ λνλΌ λ°μ΄ν° μ);
νλ©΄μ 보μ¬μ§ νμ΄μ§ κ·Έλ£Ή = Math.ceil(νμ¬ νμ΄μ§/ ν νλ©΄μ λνλΌ νμ΄μ§ μ);
π νλ©΄μ κ·Έλ €μ§ μ²« λ²μ§Έ νμ΄μ§
μ λ§μ§λ§ νμ΄μ§
λ μμμ ꡬν νλ©΄μ 보μ¬μ§ νμ΄μ§ κ·Έλ£Ή
μ μ΄μ©ν΄ λ₯Ό ꡬν©λλ€.
νλ©΄μ κ·Έλ €μ§ μ²« λ²μ§Έ νμ΄μ§ : νλ©΄μ κ·Έλ €μ§ λ§μ§λ§ νμ΄μ§ - (ν νλ©΄μ λνλΌ νμ΄μ§ - 1)
(λ¨, κ³μ°λ κ°μ΄ 0 μ΄νμ΄λ©΄ 첫λ²μ§Έ νμ΄μ§λ 1μ΄λ€.)
νλ©΄μ κ·Έλ €μ§ λ§μ§λ§ νμ΄μ§ : νλ©΄μ 보μ¬μ§ νμ΄μ§ κ·Έλ£Ή * ν νλ©΄μ λνλΌ νμ΄μ§
(λ¨, κ³μ°λ κ°μ΄ μ΄ νμ΄μ§μλ³΄λ€ λ§μΌλ©΄ λ§μ§λ§ νμ΄μ§λ μ μ΄ νμ΄μ§ μμ΄λ€.)
μ΄λ κ² λ€ κ°μ§ κ°
μ κ³μ°μ΄ λλ¬μ΅λλ€.
μ΄μ , μ΄ λ€ κ°μ§ κ°
μ μ΄μ©ν΄ Pagination
μ DOM
μ κ·Έλ €μ€λλ€.
renderPagination: function (currentPage) {
// νμ¬ κ²μλ¬Όμ μ 체 κ°μκ° 20κ° μ΄νλ©΄ paginationμ μ¨κΉλλ€.
if (_totalCount <= 20) return;
var totalPage = Math.ceil(_totalCount / 20);
var pageGroup = Math.ceil(currentPage / 10);
var last = pageGroup * 10;
if (last > totalPage) last = totalPage;
var first = last - (10 - 1) <= 0 ? 1 : last - (10 - 1);
const fragmentPage = document.createDocumentFragment();
if (prev > 0) {
var allpreli = document.createElement('li');
allpreli.insertAdjacentHTML("beforeend", `<a href='#js-bottom' id='allprev'><<</a>`);
var preli = document.createElement('li');
preli.insertAdjacentHTML("beforeend", `<a href='#js-ottom' id='prev'><</a>`);
fragmentPage.appendChild(allpreli);
fragmentPage.appendChild(preli);
}
for (var i = first; i <= last; i++) {
const li = document.createElement("li");
li.insertAdjacentHTML("beforeend", `<a href='#js-bottom' id='page-${i}' data-num='${i}'>${i}</a>`);
fragmentPage.appendChild(li);
}
if (last < totalPage) {
var allendli = document.createElement('li');
allendli.insertAdjacentHTML("beforeend", `<a href='#js-bottom' id='allnext'>>></a>`);
var endli = document.createElement('li');
endli.insertAdjacentHTML("beforeend", `<a href='#js-bottom' id='next'>></a>`);
fragmentPage.appendChild(endli);
fragmentPage.appendChild(allendli);
}
document.getElementById('js-pagination').appendChild(fragmentPage);
// νμ΄μ§ λͺ©λ‘ μμ±
}
[2. ν νμ΄μ§μ 20κ°μ© κ²μλ¬Όμ 보μ¬μ€λ€.]
1νμ΄μ§ : 20κ°
2νμ΄μ§ : 40κ°
3νμ΄μ§ : 60κ°
.
.
.
Nνμ΄μ§ : (N * 20)κ°
κ° νμ΄μ§λ§λ€ 20κ°μ© κ²μλ¬Όμ 보μ¬μ£ΌκΈ° μν΄ νμ΄μ§ λ²νΈλ₯Ό μλ²λ‘ μ μ‘ν΄ page
μ λ°λΌ 20κ°μ© κ²°κ³Όλ₯Ό λ°μ΅λλ€.
π νμ΄μ§λ€μ΄μ νμ΄μ§λ₯Ό ν΄λ¦νμ λ μ΄λ²€νΈλ₯Ό λ±λ‘ν΄ νμ΄μ§ μ«μλ₯Ό μλ²λ‘ λ겨μ€λλ€.
$("#js-pagination a").click(function (e) {
e.preventDefault();
var $item = $(this);
var $id = $item.attr("id");
var selectedPage = $item.text();
list.renderPagination(selectedPage);//νμ΄μ§λ€μ΄μ
그리λ ν¨μ
list.search(selectedPage);//νμ΄μ§ 그리λ ν¨μ
});
β μλ²λ‘ μ μ‘λ νμ΄μ§ μ«μλ₯Ό μ΄μ©ν΄ startNum
μ νμ΄μ§ μ«μλ‘ λ°κ³ endNum
μ νμ΄μ§ μ«μ * 20
μ ν΄ μΏΌλ¦¬λ₯Ό μμ±νμ΅λλ€.
SELECT *
FROM (
SELECT ROW_NUMBER() OVER (/**orderby**/) NUM, LIST.*
FROM DAMS_PGM_EPISODE_VIEW LIST
/**where**/
)
WHERE NUM BETWEEN :startRow AND :endRow
ORDER BY NUM
μ΄λ κ² λλ©΄ 20κ°μ© κ²μλ¬Όμ΄ νμ΄μ§μ λ§κ² κ°μ Έμ μ§λλ€.
[3. μ΄μ , λ€μ λ²νΌμ΄ μ‘΄μ¬νλ€.]
μ΄μ λ²νΌ : νλ©΄μ κ·Έλ €μ§ μ²«λ²μ§Έ νμ΄μ§ - 1
λ€μ λ²νΌ : νλ©΄μ κ·Έλ €μ§ λ§μ§λ§ νμ΄μ§ + 1
π λ²νΌμ ν΄λ¦νμ λ νμ΄μ§ μ«μκ° μλ next
prev
μ΄λ©΄ μλ§κ² κ³μ°λ κ°μ νμ΄μ§ μ«μλ‘ λ겨μ€λλ€.
var next = last + 1;
var prev = first - 1;
$("#js-pagination a").click(function (e) {
e.preventDefault();
var $item = $(this);
var $id = $item.attr("id");
var selectedPage = $item.text();
if ($id == "next") selectedPage = next;
if ($id == "prev") selectedPage = prev;
list.renderPagination(selectedPage);//νμ΄μ§λ€μ΄μ
그리λ ν¨μ
list.search(selectedPage);//νμ΄μ§ 그리λ ν¨μ
});
[4. μ²μμΌλ‘, λ§μ§λ§μΌλ‘ λ²νΌμ΄ μ‘΄μ¬νλ€.]
μ²μμΌλ‘ λ²νΌ : 1
λ§μ§λ§μΌλ‘ λ²νΌ : μ΄ νμ΄μ§ μ
π λ²νΌμ ν΄λ¦νμ λ νμ΄μ§ μ«μκ° μλ 첫λ²μ§Έ νμ΄μ§μ λ§μ§λ§ νμ΄μ§λ₯Ό λ겨μ€λλ€.
$("#js-pagination a").click(function (e) {
e.preventDefault();
var $item = $(this);
var $id = $item.attr("id");
var selectedPage = $item.text();
if ($id == "next") selectedPage = next;
if ($id == "prev") selectedPage = prev;
if ($id == "allprev") selectedPage = 1;
if ($id == "allnext") selectedPage = totalPage;
list.renderPagination(selectedPage);//νμ΄μ§λ€μ΄μ
그리λ ν¨μ
list.search(selectedPage);//νμ΄μ§ 그리λ ν¨μ
});
μ΅μ’ κ²°κ³Ό β
[μ΅μ’ μ½λ π]
function renderPagination(currentPage) {
if (_totalCount <= 20) return;
var totalPage = Math.ceil(_totalCount / 20);
var pageGroup = Math.ceil(currentPage / 10);
var last = pageGroup * 10;
if (last > totalPage) last = totalPage;
var first = last - (10 - 1) <= 0 ? 1 : last - (10 - 1);
var next = last + 1;
var prev = first - 1;
const fragmentPage = document.createDocumentFragment();
if (prev > 0) {
var allpreli = document.createElement('li');
allpreli.insertAdjacentHTML("beforeend", `<a href='#js-bottom' id='allprev'><<</a>`);
var preli = document.createElement('li');
preli.insertAdjacentHTML("beforeend", `<a href='#js-bottom' id='prev'><</a>`);
fragmentPage.appendChild(allpreli);
fragmentPage.appendChild(preli);
}
for (var i = first; i <= last; i++) {
const li = document.createElement("li");
li.insertAdjacentHTML("beforeend", `<a href='#js-bottom' id='page-${i}' data-num='${i}'>${i}</a>`);
fragmentPage.appendChild(li);
}
if (last < totalPage) {
var allendli = document.createElement('li');
allendli.insertAdjacentHTML("beforeend", `<a href='#js-bottom' id='allnext'>>></a>`);
var endli = document.createElement('li');
endli.insertAdjacentHTML("beforeend", `<a href='#js-program-detail-bottom' id='next'>></a>`);
fragmentPage.appendChild(endli);
fragmentPage.appendChild(allendli);
}
document.getElementById('js-pagination').appendChild(fragmentPage);
// νμ΄μ§ λͺ©λ‘ μμ±
$(`#js-pagination a`).removeClass("active");
$(`#js-pagination a#page-${currentPage}`).addClass("active");
$("#js-pagination a").click(function (e) {
e.preventDefault();
var $item = $(this);
var $id = $item.attr("id");
var selectedPage = $item.text();
if ($id == "next") selectedPage = next;
if ($id == "prev") selectedPage = prev;
if ($id == "allprev") selectedPage = 1;
if ($id == "allnext") selectedPage = totalPage;
list.renderPagination(selectedPage);//νμ΄μ§λ€μ΄μ
그리λ ν¨μ
list.search(selectedPage);//νμ΄μ§ 그리λ ν¨μ
});
};
'FRONT > JavaScript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[JS] Cropper.js μ¬μ©κΈ° (0) | 2022.02.05 |
---|---|
[KENDO JS] DataSourceλ₯Ό gird, listviewμ μ¬μ¬μ©νκΈ° (0) | 2021.10.08 |
[JS] JavaScript μ€μ½ν (0) | 2021.07.01 |
[JS] vanilla jsλ‘ λ¬΄ν μ€ν¬λ‘€(Infinite scroll) ꡬννκΈ° (0) | 2021.07.01 |