I'm working on a jQuery pagination tool, and while I have the pagination working, I see a flaw:
Having a row of 14+ pagination links is fine on a desktop, but it is not OK for a mobile device. So I want to limit the number of visible pages to 5 at a time (not including the next/prev buttons) and have it update when it reaches the third visible page and update the visible pages in the pagination essentially looking like
| Prev | 1 | ... | 3 | 4 | 5 | 6 | Next |
I've written this CodePen of what I have so far. I'm aware there are plug-ins that do this for me but I want to avoid using plug-ins for this project.
HTML (example content being paginated)
<div class="container" id="jar">
<div class="row content">
<div class="col">
<img src="http://via.placeholder.com/350x150">
</div>
<div class="col">
<img src="http://via.placeholder.com/350x150">
</div>
</div>
</div>
HTML (pagination element)
<nav>
<ul class="pagination justify-content-center pagination-sm">
<li id="previous-page" class="page-item"><a class="page-link" href="javascript:void(0)">Prev</a>
</li>
</ul>
</nav>
JavaScript:
"use strict";
//NOTE: the class"page-item page-link" are classes used in bootstrap 4 and will not be seen declared or used outside of the <li> as the bootstrap framework uses those classes to apply CSS
//sets number of items and limits the number of items per page
var numberOfItems = $("#jar .content").length;
var limitPerPage = 2;
//as the items start at 0 to keep the items per page at the limitPerPage set in variable, need to subtract 1 as is shown below
$("#jar .content:gt(" + (limitPerPage - 1) + ")").hide();
//sets total pages rounded to the next whole number
var totalPages = Math.round(numberOfItems / limitPerPage);
//append the 1 page to the pagination
$(".pagination").append(
"<li class='page-item current-page active'><a class='page-link' href='javascript:void(0)'>" +
1 +
"</a></li>"
);
//append the pages in sequential order after prev button (as seen in the html in codepen)
for (var i = 2; i <= totalPages; i++) {
$(".pagination").append(
"<li class='page-item current-page'><a class='page-link' href='javascript:void(0)'>" +
i +
"</a></li>"
);
}
//appends the next button link as the final child element in the pagination
$(".pagination").append(
"<li class='page-item' id='next-page'><a class='page-link' href='javascript:void(0)'>Next</a></li>"
);
//When a page is selected, if it has "active" class return false, if no "active" class, go to page and add "active" class attribute and remove from any other element that has "active" on it.
$(".pagination li.current-page").on("click", function () {
if ($(this).hasClass("active")) {
return false;
} else {
var currentPage = $(this).index();
$(".pagination li").removeClass("active");
$(this).addClass("active");
$("#jar .content").hide();
//.hide will hide content that does not fit into that page (ie 0 and 1 on page one, 2 and 3 on page two and so on will show on appropriate page) If it does not fall within the range for that page .hide, if it falls within the range .show content
var grandTotal = limitPerPage * currentPage;
for (var i = grandTotal - limitPerPage; i < grandTotal; i++) {
$("#jar .content:eq(" + i + ")").show();
}
}
});
//when next is clicked if it is on the last page, return false otherwise move on to next page in pagination and remove "active" class from previous page and add "active" class to new page
$("#next-page").on("click", function () {
var currentPage = $(".pagination li.active").index();
if (currentPage === totalPages) {
return false;
} else {
currentPage++;
$(".pagination li").removeClass("active");
$("#jar .content").hide();
var grandTotal = limitPerPage * currentPage;
for (var i = grandTotal - limitPerPage; i < grandTotal; i++) {
$("#jar .content:eq(" + i + ")").show();
}
$(".pagination li.current-page:eq(" + (currentPage - 1) + ")").addClass(
"active"
);
}
});
//when prev is clicked if it is on the 1 page, return false otherwise move on to previous page and remove "active" class from last page visited and add "active" class to new page
$("#previous-page").on("click", function () {
var currentPage = $(".pagination li.active").index();
if (currentPage === 1) {
return false;
} else {
currentPage--;
$(".pagination li").removeClass("active");
$("#jar .content").hide();
var grandTotal = limitPerPage * currentPage;
for (var i = grandTotal - limitPerPage; i < grandTotal; i++) {
$("#jar .content:eq(" + i + ")").show();
}
$(".pagination li.current-page:eq(" + (currentPage - 1) + ")").addClass(
"active"
);
}
});