lib/pagination.js
module.exports = class Pagination {
constructor(total){
this.view = 10; // 1ページに表示するレコード数
this.page_range = 2; // リンクを前後何ページ分表示するかの設定
this.count = total; // レコード数
this.total_page = Math.ceil(this.count / this.view); // ページ総数
this.current_page; // 現在のページ
this.sql_start = (this.current_page - 1) * this.view;
this.nums = []; // ページ番号格納用
};
notParam(){
this.current_page = 1;
this.sql_start = (this.current_page - 1) * this.view;
let prev = Math.max(this.current_page - 1, 1);
let next = Math.min(this.current_page + 1, this.total_page);
let start = Math.max(this.current_page - this.page_range, 2);
let end = Math.min(this.current_page + this.page_range, this.total_page - 1); // ページ番号終点
for (let i = start; i <= end; i++) { this.nums.push(i) }
let data = {
view: this.view,
sql_start: this.sql_start,
total_count: this.count,
prev: prev,
next: next,
start: start,
end: end,
nums: this.nums,
page: this.current_page,
pageRange: this.page_range,
totalPage: this.total_page
}
return data;
}
param(param){
this.current_page = param;
this.sql_start = (this.current_page - 1) * this.view;
let prev = Math.max(this.current_page - 1, 1);
let next = Math.min(Number(this.current_page) + 1, this.total_page);
let start = Math.max(this.current_page - this.page_range, 2);
let end = Math.min(Number(this.current_page) + Number(this.page_range), this.total_page - 1); // ページ番号終点
for (let i = start; i <= end; i++) { this.nums.push(i) }
let data = {
view: this.view,
sql_start: this.sql_start,
total_count: this.count,
prev: prev,
next: next,
start: start,
end: end,
nums: this.nums,
page: this.current_page,
pageRange: this.page_range,
totalPage: this.total_page
}
return data;
}
}