주요 코드만
const positionOfEqual = location.search.indexOd('=');
// location.search를 자른다.
const param = location.search.substr(positionOfEqual+1);
let pageno = 1;
if(param!="") pageno = parseInt(param);
const PAGE_SIZE = 10;
const PAGE_PER_BLOCK = 5;
function getPagination(result) {
const totalcount = result.totalcount;
let countOfPage = Math.floor(totalcount/PAGE_SIZE)+1;
if(totalcount%PAGE_SIZE)==0) countOfPage--;
let blockNo = Math.floor(pageno/PAGE_PER_BLOCK);
if(pageno%PAGE_PER_BLOCK==0) blockNo--;
const startPage = (blockNo * PAGE_PER_BLOCK) +1;
const prev = startPage -1;
let endPage = (startPage + PAGE_PER_BLOCK) -1;
let next = endPage +1;
if(endPage>=countOfPage){
endPage = countOfPage;
next = 0
}
return {prev, startPage, endPage, enxt};
}
function printContacts(contacts) {
const $list = $('#list');
$.each(contacts, function(idx,contact){
const $tr = $('<tr>').appendTo($list);
$('<td>').text(contact.no).appendTo($tr);
const $td = $('<td>').appendTo($tr);
$('<a>').attr('href','#').text(contact.name).appendTo($td);
$('<td>').text(contact.address).appendTo($tr);
$('<td>').text(contact.tel).appendTo($tr);
});
}
function printPagination(pagination){
const $pagination = $('#pagination_div').find('.pagination');
if(pagination.prev>0){
const $prev = $('<li>').appendTo($pagination);
$('<a>').attr('href','004.html?pageno =
'+pagination.prev).text('이전').appendTo($prev);
}
for(let i=pagination.startPage; i<pagination.endPage; i++){
const $li = $('<li>').appendTo($pagination);
if(pageno==i)
$li.attr('class','active');
$('<a>').attr('href','oo4.html?pageno='+i).text(i).appendTo($li);
}
if(pagination.next>0) {
const $next = $('<li>').appendTo($pagination);
$(<'a>').attr('href','004.html?pageno =
'+pagination.next).text('다음').appendTo($next);
}
}
$(document).ready(function(){
const url = 'https://sample.bmaster.kro.kr/contacts?pageno='
+ pageno + '&pagesize' + PAGE_SIZE;
$.ajax(url).done((result)=>{
const pagination = getPagination(result);
printContacts(result.contacts);
printPagination(pagination);
});
});