Data.Page - Autogenerate useful information for pagination
var p = new Data.Page( 120, // total entries 10, // entries per page 1 // current page ); // get pagination info p.total_entries() // 120 p.entries_per_page() // 10 p.current_page() // 1 p.entries_on_this_page() // 10 p.first_page() // 1 p.last_page() // 12 p.first() // 1 p.last() // 10 p.previous_page() // undefined p.next_page() // 2 // get items that are included in this page var items = [ 'item1' .. 'item99' ]; items = p.splice( items ); // [ 'item1' .. 'item10' ]
By giving minimum parameters, this module auto-calculates all the neccessary information needed to display pagination, which we see (and program) alot in a search result page.
Especially useful for client-side AJAX applications.
var page = new Data.Page( 99, // total entries 15, // entries displayed per page 2 // current page number );
Or, you can set those parameter later:
var page = new Data.Page(); page.total_entries( 99 ); page.entries_per_page( 15 ); page.current_page( 2 );
page.total_entries( 130 ); // setter page.total_entries(); // getter
Sets/gets the total number of entries in your result set. Ignored if passed a negative value.
page.entries_per_page( 10 ); // setter page.entries_per_page(); // getter
Sets/gets the total number of entries displayed per page. Ignored if passed a negative value.
page.current_page( 10 ); // setter page.current_page(); // getter
page.entries_on_this_page(); // 10 // might be different on last page page.current_page( page.last_page() ); page.entries_on_this_page(); // 7
Gets the number of items displayed in current page. It's usually same as page.entries_per_page(), but might differ on the last page.
page.last_page(); // 5
Gets the last page number. It's also the "total page count".
page.first_page(); // 1
Gets the first page number, which will always return 1. Counter-part for page.last_page() method.
page.first(); // 21
Gets the item number of the first item in current page.
page.last(); // 30
Gets the item number of the last item in current page.
page.previous_page(); // 1
Gets the page number of the previous page. Returns "undefined" if called at first page.
page.next_page(); // 2
Gets the page number of the next page. Returns "undefined" if called at last page.
var items = [ 'item1' .. 'item99' ]; items = p.splice( items ); // [ 'item1' .. 'item10' ]
By passing an array with items/records for all pages, it will return an array containing only the items for the current page.
var page = new Data.Page( 50, 10, 3 ); // we're at page 3 page.skipped(); // 20
Returns how many items are skipped as for the current page.
Perl CPAN - Data::Page module http://search.cpan.org/dist/Data-Page/
Leon Brocard <acme@astray.com> Toshimasa Ishibashi <iandeth99@ybb.ne.jp>
Translated from Perl to JavaScript by Leon but with help from Toshimasa's previous translation.
Copyright (c) 2011 Leon Brocard and Toshimasa Ishibashi. All rights reserved. This module is free software; you can redistribute it and/or modify it under the terms of the Artistic license. Or whatever license I choose, which I will do instead of keeping this documentation like it is.
To install JS::Data::Page, copy and paste the appropriate command in to your terminal.
cpanm
cpanm JS::Data::Page
CPAN shell
perl -MCPAN -e shell install JS::Data::Page
For more information on module installation, please visit the detailed CPAN module installation guide.