Toshimasa Ishibashi - Data.Page-0.02

Documentation | Source

NAME

Data.Page - Auto generate useful information for pagination

SYNOPSIS

  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' ]

DESCRIPTION

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.

Constructor

  var page = new Data.Page(
      99,  // total entries
      15,  // entries displayed per page
      2    // current page number
  ); 

Or, you can set those parameters later:

  var page = new Data.Page();
  page.total_entries( 99 );
  page.entries_per_page( 15 );
  page.current_page( 2 );

Methods

total_entries()

  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.

entries_per_page()

  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.

current_page()

  page.current_page( 10 );  // setter
  page.current_page();      // getter

Sets/gets the total number of entries displayed per page. Ignored if passed a negative value.

entries_on_this_page()

  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.

last_page()

  page.last_page();  // 5

Gets the last page number. It's also the "total page count".

first_page()

  page.first_page(); // 1

Gets the first page number, which will always return 1. Counter-part for page.last_page() method.

first()

  page.first();  // 21

Gets the item number of the first item in current page.

last()

  page.last();  // 30

Gets the item number of the last item in current page.

previous_page()

  page.previous_page();  // 1

Gets the page number of the previous page. Returns "undefined" if called at first page.

next_page()

  page.next_page();  // 2

Gets the page number of the next page. Returns "undefined" if called at last page.

splice()

  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.

skipped()

  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.

SEE ALSO

Perl CPAN - Data::Page module http://search.cpan.org/dist/Data-Page/

AUTHOR

Toshimasa Ishibashi <iandeth99@ybb.ne.jp>

http://iandeth.dyndns.org/

This module was ported from Perl Module - Data::Page

Original perl code by Leon Brocard - http://search.cpan.org/dist/Data-Page/

Thank you Leon for a nice work.

COPYRIGHT

  Copyright (c) 2007 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.
/*

=head1 NAME

Data.Page - Auto generate useful information for pagination

=head1 SYNOPSIS

  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' ]

=head1 DESCRIPTION

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.

=head2 Constructor

  var page = new Data.Page(
      99,  // total entries
      15,  // entries displayed per page
      2    // current page number
  ); 

Or, you can set those parameters later:

  var page = new Data.Page();
  page.total_entries( 99 );
  page.entries_per_page( 15 );
  page.current_page( 2 );

=head2 Methods

=head3 total_entries()

  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.

=head3 entries_per_page() 

  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.

=head3 current_page()

  page.current_page( 10 );  // setter
  page.current_page();      // getter

Sets/gets the total number of entries displayed per page. Ignored if passed a negative value.

=head3 entries_on_this_page()

  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.

=head3 last_page()

  page.last_page();  // 5

Gets the last page number. It's also the  "total page count".

=head3 first_page()

  page.first_page(); // 1

Gets the first page number, which will always return 1. Counter-part for
page.last_page() method.

=head3 first()

  page.first();  // 21

Gets the item number of the first item in current page.

=head3 last()

  page.last();  // 30

Gets the item number of the last item in current page.

=head3 previous_page()

  page.previous_page();  // 1

Gets the page number of the previous page. Returns "undefined" if called at first page.

=head3 next_page()

  page.next_page();  // 2

Gets the page number of the next page. Returns "undefined" if called at last page.

=head3 splice()

  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.

=head3 skipped()

  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.

=head1 SEE ALSO

Perl CPAN - Data::Page module
L<http://search.cpan.org/dist/Data-Page/>

=head1 AUTHOR

Toshimasa Ishibashi <F<iandeth99@ybb.ne.jp>>

L<http://iandeth.dyndns.org/>

This module was ported from Perl Module - Data::Page

Original perl code by Leon Brocard - L<http://search.cpan.org/dist/Data-Page/>

Thank you Leon for a nice work.

=head1 COPYRIGHT

  Copyright (c) 2007 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.

=cut

*/

if( typeof( Data ) != 'function' ) {
    Data = function (){};
}

Data.Page = function() {
    this.initialize.apply(this, arguments);
};

Data.Page.VERSION = "0.02";

Data.Page.prototype = {
    initialize: function ( total_entries, entries_per_page, current_page ){
        this._total_entries    = 0;
        this._entries_per_page = 10;
        this._current_page     = 1;
        this.total_entries( total_entries );
        this.entries_per_page( entries_per_page );
        this.current_page( current_page );
    },
    total_entries: function ( int ){
        int = parseInt( int );
        if ( int > 0 ){
            this._total_entries = int;
        }
        return this._total_entries;
    },
    entries_per_page: function ( int ){
        int = parseInt( int );
        if ( int > 0 ){
            this._entries_per_page = int;
        }
        return this._entries_per_page;
    },
    current_page: function ( int ){
        int = parseInt( int );
        if ( int > 0 ){
            this._current_page = int;
        }
        return this._current_page;
    },
    entries_on_this_page: function (){
        if( this.total_entries() == 0 ){
            return 0;
        }
        return this.last() - this.first() + 1;
    },
    first_page: function (){
        return 1;
    },
    last_page: function (){
        var pages = this.total_entries() / this.entries_per_page();
        var last_page = 1;
        if( pages == parseInt( pages ) ){
            last_page = pages;
        }else{
            last_page = 1 + parseInt( pages );
        }
        if( last_page < 1 ){
            last_page = 1;
        }
        return last_page;
    },
    first: function (){
        if( this.total_entries() == 0 ){
            return 0;
        }
        return ( ( this.current_page() - 1 ) * this.entries_per_page() ) + 1;
    },
    last: function (){
        if( this.current_page() == this.last_page() ){
            return this.total_entries();
        }
        return this.current_page() * this.entries_per_page();
    },
    previous_page: function (){
        if( this.current_page() > 1 ){
            return this.current_page() - 1;
        }
        return undefined;
    },
    next_page: function (){
        if( this.current_page() < this.last_page() ){
            return this.current_page() + 1;
        }
        return undefined;
    },
    splice: function ( arr ){
        var top = arr.length;
        if( arr.length > this.last() ){
            top = this.last();
        }
        if( top == 0 ){
            return [];
        }
        var ret = [];
        var from = this.first() - 1;
        var to   = top - 1;
        for( var i=0; i<arr.length; i++ ){
            if( i < from ){ continue; }
            if( i > to ){ break; }
            ret.push( arr[i] );
        }
        return ret;
    },
    skipped: function (){
        var skipped = this.first() - 1;
        if( skipped < 0 ){
            return 0;
        }
        return skipped;
    }
};