纯JS前端分页,可配合BootStrap、AngularJS使用
自己写的,保存起来便于以后使用。
class Pagination{
constructor(arrayData, pageSize) {
this.arrayData = arrayData;
this.pageSize = pageSize;
this.page = 1;
this._init();
}
_init() {
this.arraySize = Pagination.getArraySize(this.arrayData);
this.totalPage = Pagination.getTotalPage(this.arraySize, this.pageSize);
this.pageList = Pagination.getPageList(this.totalPage);
this.currentDate = this.getCurrentData()
}
getCurrentData() {
let page = this.page;
let all = this.arrayData;
return all.slice((page - 1) * this.pageSize, page * this.pageSize);
}
getCurrentPage() {
return this.page;
}
isFirstPage() {
return this.page === 1;
}
isLastPage() {
if (!this.totalPage || this.totalPage === 0){
return true;
}
return this.page === this.totalPage;
}
previousPage() {
if (this.isFirstPage()) {
return;
}
this.page --;
this.currentDate = this.getCurrentData();
}
nextPage() {
if (this.isLastPage()) {
return;
}
this.page ++;
this.currentDate = this.getCurrentData();
}
isActivePage(page) {
return page === this.page;
};
selectPage(page) {
if (page > this.totalPage) {
return;
}
this.page = page;
this.currentDate = this.getCurrentData();
};
static getArraySize(iterator) {
let size = 0;
if (!iterator) {
return 0;
}
iterator.forEach(iter => {
size ++;
});
return size;
}
static getTotalPage(arraySize, pageSize) {
let size = arraySize / pageSize;
return Math.ceil(size);
}
static getPageList(totalPage) {
let pageList = [];
for (let i = 1; i <= totalPage; i++){
pageList.push(i);
}
return pageList;
}
}