const totalItems = 100;
const itemsPerPage = 10;
const totalPages = Math.ceil(totalItems / itemsPerPage);
let currentPage = 1;
const paginationContainer = document.querySelector('.pagination');
function generateDummyData() {
const dummyData = [];
for (let i = 0; i < dummyData.length; i++) {
dummyData.push(`Item ${i + 1}`);
}
return dummyData;
}
function updateList() {
paginationContainer.innerHTML = '';
const dummyData = generateDummyData();
const startIndex = (currentPage - 1) * itemsPerPage;
const endIndex = startIndex + itemsPerPage;
const currentPageItems = dummyData.slice(startIndex, endIndex);
const listContainer = document.querySelector('.list-container');
listContainer.innerHTML = '';
currentPageItems.forEach((item) => {
const listItem = document.createElement('li');
listItem.textContent = item;
listContainer.appendChild(listItem);
});
createPagination();
}
function createPagination() {
const prevButton = document.createElement('button');
prevButton.textContent = '이전';
prevButton.addEventListener('click', goToPreviousPage);
paginationContainer.appendChild(prevButton);
const startPage = Math.max(1, currentPage - 5);
const endPage = Math.min(totalPages, startPage + 9);
for (let i = startPage; i <= endPage; i++) {
const pageButton = document.createElement('button');
pageButton.textContent = i;
pageButton.addEventListener('click', () => goToPage(i));
paginationContainer.appendChild(pageButton);
}
const nextButton = document.createElement('button');
nextButton.textContent = '다음';
nextButton.addEventListener('click', goToNextPage);
paginationContainer.appendChild(nextButton);
}
function goToPreviousPage() {
if (currentPage > 1) {
currentPage--;
updateList();
}
}
function goToNextPage() {
if (currentPage < totalPages) {
currentPage++;
updateList();
}
}
function goToPage(page) {
currentPage = page;
updateList();
}
createPagination();
updateList();