Created
April 27, 2024 10:50
-
-
Save moxet/c5e270b9de422d193f5a4aff463bb1a5 to your computer and use it in GitHub Desktop.
Jquery Code for REST API Pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="pagin"></div> | |
<script> | |
jQuery(document).ready(function($) { | |
//Pagination | |
pageSize = 10; | |
incremSlide = 10; | |
startPage = 0; | |
numberPage = 0; | |
var pageCount = $(".jet-listing-grid__item").length / pageSize; | |
var totalSlidepPage = Math.floor(pageCount / incremSlide); | |
for(var i = 0 ; i<pageCount;i++){ | |
$("#pagin").append('<li><a href="#">'+(i+1)+'</a></li> '); | |
if(i>pageSize){ | |
$("#pagin li").eq(i).hide(); | |
} | |
} | |
var prev = $("<li/>").addClass("prev").html("Prev").click(function(){ | |
startPage-=5; | |
incremSlide-=5; | |
numberPage--; | |
slide(); | |
}); | |
prev.hide(); | |
var next = $("<li/>").addClass("next").html("Next").click(function(){ | |
startPage+=5; | |
incremSlide+=5; | |
numberPage++; | |
slide(); | |
}); | |
$("#pagin").prepend(prev).append(next); | |
$("#pagin li").first().find("a").addClass("current"); | |
slide = function(sens){ | |
$("#pagin li").hide(); | |
for(t=startPage;t<incremSlide;t++){ | |
$("#pagin li").eq(t+1).show(); | |
} | |
if(startPage == 0){ | |
next.show(); | |
prev.hide(); | |
}else if(numberPage == totalSlidepPage ){ | |
next.hide(); | |
prev.show(); | |
}else{ | |
next.show(); | |
prev.show(); | |
} | |
} | |
showPage = function(page) { | |
$(".jet-listing-grid__item").hide(); | |
$(".jet-listing-grid__item").each(function(n) { | |
if (n >= pageSize * (page - 1) && n < pageSize * page) | |
$(this).show(); | |
}); | |
} | |
showPage(1); | |
$("#pagin li a").eq(0).addClass("current"); | |
$("#pagin li a").click(function() { | |
$("#pagin li a").removeClass("current"); | |
$(this).addClass("current"); | |
showPage(parseInt($(this).text())); | |
}); | |
}); | |
</script> | |
<style> | |
li a | |
{ | |
color: white; | |
} | |
.current { | |
color: white; | |
} | |
#pagin li { | |
display: inline-block; | |
cursor: pointer; | |
background: #8a3948; | |
color: white!important; | |
padding: 5px 25px 5px 25px; | |
} | |
.prev { | |
cursor: pointer; | |
cursor: pointer; | |
background: #8a3948; | |
color: white; | |
padding: 5px 25px 5px 25px; | |
} | |
.next { | |
cursor: pointer; | |
cursor: pointer; | |
background: #8a3948; | |
color: white; | |
padding: 5px 25px 5px 25px; | |
} | |
.last{ | |
cursor:pointer; | |
margin-left:5px; | |
} | |
.first{ | |
cursor:pointer; | |
margin-right:5px; | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More Optimized code