NAME

Nile::Plugin::Paginate - Efficient Data Pagination

SYNOPSIS

  # example data
  my $total_entries = 100;
  my $entries_per_page = 10;
  my $pages_per_set = 7;
  my $current_page = 4;

  my $paginate = $app->plugin->paginate(

      total_entries       => $total_entries, 
      entries_per_page    => $entries_per_page, 
      current_page        => $current_page,
      pages_per_set       => $pages_per_set,
      mode => "slide", #modes are 'slide', 'fixed', default is 'slide'

      css_class        => "pagination",
      layout => 0, # next&prev position, both right, 2: both left, 0: left and right
      page_link   => "action=browse&page=%page%&id=10",
      prev_page_text => "Prev",
      next_page_text => "Next",
      last_page_text => "Last",
      first_page_text => "First",
      showing_text => "Page %page% of  %pages% (listing %first% to %last% of %entries%)",
      showing_list_text => "Page %page% of  %pages%",
      more_text => "...",
  );

  # general page information
  print "         First page: ", $paginate->first_page, "\n";
  print "          Last page: ", $paginate->last_page, "\n";
  print "          Next page: ", $paginate->next_page, "\n";
  print "      Previous page: ", $paginate->prev_page, "\n";
  print "       Current page: ", $paginate->current_page, "\n";

  # entries on current page
  print "First entry on current page: ", $paginate->first, "\n";
  print " Last entry on current page: ", $paginate->last, "\n";

  # returns the number of entries on the current page
  print "Entries on the current page: ", $paginate->entries_on_current_page, " \n";

  # page set information
  print "First page of previous page set: ",  $paginate->previous_set, "\n";
  print "    First page of next page set: ",  $paginate->next_set, "\n";

  # print the page numbers of the current set (visible pages)
  foreach my $page (@{$paginate->pages_in_set()}) {
      ($page == $paginate->current_page())? print "[$page] " : print "$page ";
  }
  
  # this will print out these results:
  # First page: 1
  # Last page: 10
  # Next page: 5
  # Previous page: 3
  # Current page: 4
  # First entry on current page: 31
  # Last entry on current page: 40
  # Entries on the current page: 10 
  # First page of previous page set: 
  # First page of next page set: 11
  #     1 2 3 [4] 5 6 7 

  # rendering
  print $paginate->out, "\n";
  # prints: 
  <ul class="pagination">
     <li class="ui-state-default"><a href="page=3">Prev</a></li>
     <li class="ui-state-default"><a href="page=1">1</a></li>
     <li class="ui-state-default"><a href="page=2">2</a></li>
     <li class="ui-state-default"><a href="page=3">3</a></li>
     <li class="ui-state-active active"><a href="javascript:void(0);">4</a></li>
     <li class="ui-state-default"><a href="page=5">5</a></li>
     <li class="ui-state-default"><a href="page=6">6</a></li>
     <li class="ui-state-default"><a href="page=7">7</a></li>
     <li class="ellipsis">...</li><li class=" ui-state-default"><a href="page=10">10</a></li>
     <li class="ui-state-default"><a href="page=5">Next</a></li>
  </ul>

  print $paginate->showing, "\n";
  # prints: Page 4 of  10 (listing 31 to 40 of 100)

  print $paginate->showing_list, "\n";
  # prints: Page 4 of  10
  

DESCRIPTION

The module can be used to create page navigation for any type of applications specially good for web applications.

In addition it also provides methods for dealing with set of pages, so that if there are too many pages you can easily break them into chunks for the user to browse through.

This module is very friendly where you can change any single input parameter at anytime and it will automatically recalculate all internal methods data without the need to recreate the object again.

All the main object input options can be set direct and the module will redo the calculations including the mode method.

You can even choose to view page numbers in your set in a 'sliding' fassion.

METHODS

paginate()

my $paginate = $self->app->paginate(
    total_entries       => $total_entries, 
    entries_per_page    => $entries_per_page, 
    current_page        => $current_page,
    pages_per_set       => $pages_per_set,
    mode => "slide", #modes are 'slide', 'fixed', default is 'slide'
);

This is the constructor of the object.

SETTINGS are passed in a hash like fashion, using key and value pairs. Possible settings are:

total_entries - how many data units you have,

entries_per_page - the number of entries per page to display,

current_page - the current page number (defaults to page 1) and,

pages_per_set - how many pages to display, defaults to 10,

mode - the mode (which defaults to 'slide') determins how the paging will work.

total_entries()

$paginate->total_entries($total_entries);

This method sets or returns the total_entries. If called without any arguments it returns the current total entries.

entries_per_page()

$paginate->entries_per_page($entries_per_page);

This method sets or returns the entries per page (page size). If called without any arguments it returns the current data entries per page.

current_page()

$paginate->current_page($page_num);

This method sets or returns the current page. If called without any arguments it returns the current page number.

mode()

$paginate->mode('slide');

This method sets or returns the pages set mode which takes only two values 'fixed' or 'slide'. The default is 'slide'. The fixed mode will be good if you want to display all the navigation pages.

pages_per_set()

$paginate->pages_per_set($number_of_pages_per_set);

Sets or returns the number of pages per set (visible pages). If called without any arguments it will return the current number of pages per set.

entries_on_current_page()

$paginate->entries_on_current_page();

This method returns the number of entries on the current page.

first_page()

$paginate->first_page();

Returns first page. Always returns 1.

last_page()

$paginate->last_page();

Returns the last page number, the total number of pages.

total_pages()

$paginate->total_pages();

Returns the last page number, the total number of pages.

first()

$paginate->first();

Returns the number of the first entry on the current page.

last()

$paginate->last();

Returns the number of the last entry on the current page.

prev_page()

$paginate->prev_page();

Returns the previous page number, if one exists. Otherwise it returns undefined.

next_page()

$paginate->next_page();

Returns the next page number, if one exists. Otherwise it returns undefined.

first_page_in_set()

$paginate->first_page_in_set();

Returns 1 if the first page is in the current pages set. Otherwise it returns 0.

last_page_in_set()

$paginate->last_page_in_set();

Returns 1 if the last page is in the current pages set. Otherwise it returns 0.

previous_set()

print "Previous set starts at ", $paginate->previous_set(), "\n";

This method returns the page number at the start of the previous page set. undef is return if pages_per_set has not been set.

next_set()

print "Next set starts at ", $paginate->next_set(), "\n";

This method returns the page number at the start of the next page set. undef is return if pages_per_set has not been set.

pages_in_set()

foreach my $page_num (@{$paginate->pages_in_set()}) {
  print "Page: $page_num \n";
}

This method returns an array ref of the the page numbers within the current set. undef is return if pages_per_set has not been set.

out()

print "out: ", $paginate->out(), "\n";

showing()

print "showing: ", $paginate->showing(), "\n";

showing_list()

print "showing list: ", $paginate->showing_list(), "\n";

Bugs

This project is available on github at https://github.com/mewsoft/Nile.

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Nile.

SOURCE

Source repository is at https://github.com/mewsoft/Nile.

SEE ALSO

See Nile for details about the complete framework.

AUTHOR

Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> Website: http://www.mewsoft.com

COPYRIGHT AND LICENSE

Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, https://github.com/mewsoft/Nile, http://www.mewsoft.com

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 156:

=back doesn't take any parameters, but you said =back 4

=back without =over