Javascript Pagination Class
Provides calculations for paging elements.
Class properties
- total - total items in list
- items = [ firstIndex, lastIndex ] - Indexes of first and last items to be listed on current page
- page - current page
- perPage = 10 - how many items to be displayed on page
- pagesPerPage = 10 - maximum page numbers to be displayed on page at once
- totalPages - total pages count
- pages = [ minPage, maxPage ] - start and end numbers of pages to be displayed on current page
- prev - previous page number or false if first page
- next - next page number or false if last page
- first - detects whether first page is selected
- last - detects whether last page is selected
Example of usage
Initialization
var pagination, pagingInfo; pagination = new Pagination({ total: 100, perPage: 5, page: 15 }); pagingInfo = pagination.toJSON();
Outputs:
pagingInfo -> { page: 15, perPage: 5, total: 100, items: [70, 75], prev: 14, next: 16, first: false, last: false, pages: [11, 20], totalPages: 20, pagesPerPage: 10 };
Updating existing instance
// set all changed properties pagination.set({ page: 3, perPage: 16 }); // then pagingInfo = pagination.toJSON();
Outputs:
pagingInfo -> { page: 3, perPage: 16, total: 100, items: [32, 48], prev: 2, next: 4, first: false, last: false, pages: [1, 7], totalPages: 7, pagesPerPage: 10 };