@laralex Add your pagination links to an array, then use slice() to grab the first 3 (or whatever you want) items, then do a slice() using a negative number to grab the last 3 (whatev) items from your pagination. Then just compile as array 1, dot dot dot, array 2.
Feb 16, 2022
8
Level 12
AJAX results pagination problem
Hey, @everyone!
I'm paginating my results which are displayed through AJAX request but I'm facing a problem with the pagination. Here's my pagination logic:
function showReviews(page = 1) {
return $.get(searchUrl(page)).done(function (reviews) {
// ....
});
});
if(reviews.pagination.next_page_url != null || reviews.pagination.current_page != 1) {
var paginat = "";
paginat = paginat + '<nav role="navigation" aria-label="Pagination Navigation" class="pagination__wrapper add_bottom_15 flex items-center justify-between"><div class="hidden sm:flex-1 sm:flex sm:items-center sm:justify-between"><div class="pagination"><span class="d-flex">';
for (let p = 1; p <= reviews.pagination.last_page; p++) {
if (p === reviews.pagination.current_page) {
paginat = paginat + '<span class="d-inline" aria-current="page"><a class="active">' + p + '</a></span>';
} else {
paginat = paginat + '<a class="d-inline" onclick="showReviews('+p+')">' + p + '</a>';
}
}
paginat = paginat + '</span></div></div></nav>';
$(paginat).appendTo("#reviews-pages");
}
Here is an image what happens when the results are too many. How to minify this with .. (dots) between the page numbers?
This is how I return pagination info:
return response()->json([
'pagination' => collect($reviews->toArray())->except('data'),
// ....
]);
Level 67
@Laralex This involves a fair bit of logic to make work. I suggest looking at what others have done, for example: https://codepen.io/robertcooper_rc/pen/XeabLa
Please or to participate in this conversation.