I'm trying to make a pagination for my .pdf file gallery.
I'm using a free pagination template I found online. It works fine but the problem is it creates infinite page numbers.
For example, if I have 100 documents and display 4 per page; the pagination goes from 1-20 and shows them all.
I want it to show 3 at a time, not all of them.
I want something like this: "< 1 ... 5 6 7 ... 20 >" (will change of course each time the page changes).
Right now it's like this: "< 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 >"
Can you guys help me out? Thanks.
HTML:
<ul class="tilesWrap" id="paginated-list" data-current-page="1" aria-live="polite">
<li>
<h2>1</h2>
<h3>Ekim 1. Hafta</h3>
<button>?�ndir</button>
</li>
<li>
<h2>2</h2>
<h3>Ekim 2. Hafta</h3>
<button>?�ndir</button>
</li>
<li>
<h2>3</h2>
<h3>Ekim 3. Hafta</h3>
<button>?�ndir</button>
</li>
<li>
<h2>4</h2>
<h3>Ekim 4. Hafta</h3>
<button>?�ndir</button>
</li>
<li>
<h2>1</h2>
<h3>Eyl??l 1. Hafta</h3>
<button>?�ndir</button>
</li>
<li>
<h2>2</h2>
<h3>Eyl??l 2. Hafta</h3>
<button>?�ndir</button>
</li>
<li>
<h2>3</h2>
<h3>Eyl??l 3. Hafta</h3>
<button>?�ndir</button>
</li>
<li>
<h2>4</h2>
<h3>Eyl??l 4. Hafta</h3>
<button>?�ndir</button>
</li>
</ul>
<nav class="pagination-container">
<button class="pagination-button" id="prev-button" aria-label="Previous page" title="Previous page">
<
</button>
<div class="pagination-numbers" id="pagination-numbers">
</div>
<button class="pagination-button" id="next-button" aria-label="Next page" title="Next page">
>
</button>
</nav>
Java:
<script type="text/javascript">
const paginationNumbers = document.getElementById("pagination-numbers");
const paginatedList = document.getElementById("paginated-list");
const listItems = paginatedList.querySelectorAll("li");
const nextButton = document.getElementById("next-button");
const prevButton = document.getElementById("prev-button");
const paginationLimit = 4;
const pageCount = Math.ceil(listItems.length / paginationLimit);
let currentPage = 1;
const disableButton = (button) => {
button.classList.add("disabled");
button.setAttribute("disabled", true);
};
const enableButton = (button) => {
button.classList.remove("disabled");
button.removeAttribute("disabled");
};
const handlePageButtonsStatus = () => {
if (currentPage === 1) {
disableButton(prevButton);
} else {
enableButton(prevButton);
}
if (pageCount === currentPage) {
disableButton(nextButton);
} else {
enableButton(nextButton);
}
};
const handleActivePageNumber = () => {
document.querySelectorAll(".pagination-number").forEach((button) => {
button.classList.remove("active");
const pageIndex = Number(button.getAttribute("page-index"));
if (pageIndex == currentPage) {
button.classList.add("active");
}
});
};
const appendPageNumber = (index) => {
const pageNumber = document.createElement("button");
pageNumber.className = "pagination-number";
pageNumber.innerHTML = index;
pageNumber.setAttribute("page-index", index);
pageNumber.setAttribute("aria-label", "Page " + index);
paginationNumbers.appendChild(pageNumber);
};
const getPaginationNumbers = () => {
for (let i = 1; i <= pageCount; i++) {
appendPageNumber(i);
}
};
const setCurrentPage = (pageNum) => {
currentPage = pageNum;
handleActivePageNumber();
handlePageButtonsStatus();
const prevRange = (pageNum - 1) * paginationLimit;
const currRange = pageNum * paginationLimit;
listItems.forEach((item, index) => {
item.classList.add("hidden");
if (index >= prevRange && index < currRange) {
item.classList.remove("hidden");
}
});
};
window.addEventListener("load", () => {
getPaginationNumbers();
setCurrentPage(1);
prevButton.addEventListener("click", () => {
setCurrentPage(currentPage - 1);
});
nextButton.addEventListener("click", () => {
setCurrentPage(currentPage + 1);
});
document.querySelectorAll(".pagination-number").forEach((button) => {
const pageIndex = Number(button.getAttribute("page-index"));
if (pageIndex) {
button.addEventListener("click", () => {
setCurrentPage(pageIndex);
});
}
});
});</script>
I am trying to build a pagination that works properly as I want it to.
Please use the following pagination :
<style>
.pagination {
text-decoration:none;
color:black;
font-family:arial;
font-size:12px;
}
</style>
<script>
var totalpages=15;
var currentpage=<?php echo $_GET["page"]; ?>;
var offset=1;
var offsetnum=(offset*2) +1;
var index=0;
var index0=0;
var starttag="<a class=pagination href='testpage.php?page=";
var endtag="</a>";
if (totalpages >0) {
index0=currentpage-1;
if (index0 <1) { index0=1; }
document.write(starttag+index0+"'>"+"<" + endtag + " ");
if (totalpages <=(offsetnum+2)) {
for (index =1; index <=totalpages; index++) {
if (index !=currentpage){
document.write(starttag+index+"'>"+index + endtag + " ");
}else{
document.write("<B>"+starttag+index+"'>"+index + endtag + "</B> ");
}
}
} else {
if (currentpage<=(1+offset)) {
var startpage=2;
var endpage=2+(offsetnum)-1;
} else {
if (currentpage>=(totalpages-offset)) {
var startpage=(totalpages-1)-(offset*2);
var endpage=(totalpages-1);
} else {
var startpage=currentpage-offset;
var endpage=currentpage+offset;
}
}
if (currentpage!=1){
document.write(starttag+1+"'>1" + endtag + " ");
}else{
document.write("<B>"+starttag+1+"'>1" + endtag+"</B>" + " ");
}
if (startpage>2) document.write(".. ");
for (index =startpage; index <=endpage; index++) {
if (currentpage!=index){
document.write(starttag+index+"'>" + index + endtag + " ");
}else{
document.write("<B>" +starttag+index+"'>" + index + endtag + "</B> ");
}
}
if (endpage < (totalpages-1)) document.write(".. ");
if (currentpage!=totalpages){
document.write(starttag+totalpages+"'>" + totalpages + endtag + " ");
}else{
document.write("<B>"+starttag+totalpages+"'>" + totalpages + endtag + "</B> ");
}
}
index0=currentpage+1;
if (index0 >totalpages) { index0=totalpages; }
document.write(starttag+index0+"'>"+">" + endtag + " ");
}
</script>
Fully working sandbox link:
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.