The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Data::Scroller

SYNOPSIS

In Perl module

  my $s = Data::Scroller->new(
    max_value => $max_value,
    selected  => $selected,
    increment => $increment,
  );
  my $page_params = $s->display;

OR

  my $page_params = Data::Scroller->new(
    max_value => $max_value,
    selected  => $selected,
    increment => $increment,
  )->display;

You would then make the $page_params hashref returned via the display method available for use within your templating system.

For example if you were in a Catalyst based application:

        $c->stash->{page_params} = $page_params;

You also want the current 'increment' value available in your templates, so for example you could display/change the number of pages displayed per page via your template.

        $c->stash->{page_increment} = $s->increment;

Note: when using this module within the Catalyst framework, say for the 'page' attribute of a search, setting that attribute to 'selected' ( the currently selected page ) will not work as you expect. This is because this module expects 'selected' to be relevant to the database row you want whereas the catalyst 'page' attribute expects it to be the display page number. A work around for this problem is as such:

my $catalyst_attributes = { page => $selected == 0 ? $selected : int(($selected / 10) + 1), };

In Template

Here is an example of how to set up paging in your templates assuming Template::Toolkit and Catalyst:

        [% IF page_params %]
                <br />
                <div>
                  [% IF page_params.page_first || page_params.page_first == 0 %]
                    <a class="paging" href="[% base _ c.namespace _ '/list?page=' _ page_params.page_first _ '&order=' _ order _ '&o2=' _ o2%]">&laquo; first</a>
                  [% END %]
                  [% IF page_params.page_prev || page_params.page_prev == 0 %]
                    <a class="paging" href="[% base _ c.namespace _ '/list?page=' _ page_params.page_prev _ '&order=' _ order _ '&o2=' _ o2 %]">&laquo; prev</a>
                  [% END %]
                  [% FOR p IN page_params.page_list %]
                    [% IF p.page_current %]
                      <span class="paging">[% p.page_display %]</span>
                    [% ELSE %]
                      <a class="paging" href="[% base _ c.namespace _ '/list?page=' _ p.page_value _ '&order=' _ order _ '&o2=' _ o2 %]">[% p.page_display %]</a>
                    [% END %]
                  [% END %]
                  [% IF page_params.page_next %]
                    <a class="paging" href="[% base _ c.namespace _ '/list?page=' _ page_params.page_next _ '&order=' _ order _ '&o2=' _ o2 %]">next &raquo;</a>
                  [% END %]
                  [% IF page_params.page_last %]
                    <a class="paging" href="[% base _ c.namespace _ '/list?page=' _ page_params.page_last _ '&order=' _ order _ '&o2=' _ o2 %]">last &raquo;</a>
                  [% END %]
                </div>
        [% END %]

As you can see, you can simply add any additional parameters you wish to the paging links via your template.

DESCRIPTION

Handle navigation of data over multiple pages in a 'rolling' pageset fashion, similar to that of google.

An alternative to Data::Page by Leon Brocard ( written long before I'd heard of that :)

REQUIRED INPUT PARAMETERS

max_value

The total number of elements, eg; table rows, to be displayed. Required.

OPTIONAL INPUT PARAMETERS

selected

The input parameter passed in from templates that indicates the new page currently being requested. Default 0 ( page 1 ).

increment

Indicates the number of elements to display per page. Default 10.

page_increment

The maximum number of pages to increment by in either direction for rolling page number display. Default $increment.

name

The name of the input parameter passed in from templates that indicates the current selected page. Default 'row_num'.

METHODS

new( %args )

  %args = (
    max_value      => $max_value,
    selected       => $selected,
    increment      => $increment,
    page_increment => $page_increment,
    name           => $name
  );

Constructor. Takes hash of both required and optional input parameters as arguments. For full descriptions of the available input options see the 'REQUIRED INPUT PARAMETERS' and 'OPTIONAL INPUT PARAMETERS' sections.

display()

Determines the appropriate "set" of page numbers / links to display for the given arguments and returns a hashref of parameters for use in your template with the following structure:

  $page_params = {
    'page_increment' => $page_increment,
    'page_name' => $page_name,
    'page_list' => [
      {
        'page_value' => $page_value,
        'page_display' => $page_number,
        'page_current' => $boolean
      },
    ],
    'page_first' => $page_first,
    'page_last' => $page_last,
    'page_prev' => $page_prev,
    'page_next' => $page_next,
    'page_total' => $page_total,
  };

max_value()

return value of max_value config param.

increment()

return value of increment config param.

max_display()

return value of max_display config param.

name()

return value of name config param.

selected()

return value of selected param.

page_increment()

return value of page_increment config param.

AUTHOR

Ben Hare for Fotango Ltd, London, <benhare@gmail.com>, www.fotango.com, (c) 2004/5.

Based on 'Paging.pm', originally conceived and written by <john.ormandy@gmail.com>.

COPYRIGHT

Copyright (c) 2004/5 Fotango, London

This module is free software. You can redistribute it or modify it under the same terms as Perl itself.