EinsatzOnline/resources/js/pagination.js

63 lines
1.9 KiB
JavaScript

class Pagination{
constructor(pcore, template, destination, limit, callback){
this.limit = limit;
this.offset = 0;
this.page = 1;
this.total_count = 0;
this.pcore = pcore;
this.template = template;
this.dest = destination;
this.callback = callback;
}
set_limit(limit){
this.limit = limit;
}
render(total_count, offset){
this.total_count = total_count;
this.offset = offset;
let data = {};
data.pcore = this.pcore;
data.pages = [];
for(let i=1;i<=this.total_page_count();i++){
let page = {};
page.num = i;
page.disabled = this.current_page() === i;
data.pages.push(page);
}
data.next_disabled = !(this.current_page()<this.total_page_count());
data.back_disabled = !(this.current_page()>1);
$(this.dest).html(this.template(data));
let ob = this;
$(".pagination_"+this.pcore+"_nav_back").off("click").on("click", function(){
if(!$(this).hasClass("disabled")){
ob.offset = ob.offset-ob.limit;
ob.callback(ob.offset);
}
});
$(".pagination_"+this.pcore+"_nav_next").off("click").on("click", function(){
if(!$(this).hasClass("disabled")){
ob.offset = ob.offset+ob.limit;
ob.callback(ob.offset);
}
});
$(".pagination_"+this.pcore+"_nav_numbers").off("click").on("click", function(){
if(!$(this).hasClass("disabled")){
let num = $(this).data("pagenum");
ob.offset = (num-1)*ob.limit;
ob.callback(ob.offset);
}
})
}
total_page_count(){
return Math.ceil(this.total_count/this.limit)
}
current_page(){
return Math.floor(this.offset/this.limit)+1
}
}