[Catalyst] OT: Better TT pager?

Fernan Aguero fernan.aguero at gmail.com
Tue Dec 23 14:19:48 GMT 2008


On Tue, Dec 23, 2008 at 9:34 AM, Jesse Sheidlower <jester at panix.com> wrote:
>
> Some time ago, I developed or stole a TT pager that I now use
> in all of my Cat apps. It looks like this, though occasionally
> with minor CSS tweaks:

[snipped code]

> However, a frustration is that when I have excessively large
> result sets (such as might generate dozens or hundreds of
> pages), it takes a long to generate and looks like hell. So
> I'd like to change this to one that only shows, say, 10 pages,
> and then has a "previous ten/next ten" and/or "first"/"last"
> or something like that, whatever the standard is now.
>
> Does someone have a model I can steal from?
>
> Thanks.
>
> Jesse

This is what I use in my template:

<script type="text/javascript" language="JavaScript">
function resize(uri) { eval( location = uri ); }
</script>

[% IF pager.total_entries() > 0 -%]
<p><b>[% pager.total_entries() -%]</b> records found <b>|</b>
Showing page <b>[% pager.current_page() -%]</b> of [% pager.last_page() -%]
(records [% pager.first() -%]-[%  pager.last() -%]) <b>|</b>
Number of records to display
<select name="pagesize" onChange="resize(this.value)" style="width: 60px">
  [% FOREACH val = [ "25", "50", "100", "150", "200" ] -%]
  <option [% IF val == pagesize -%]selected[% END -%]
    value="[% Catalyst.request.uri_with( rows => val ) -%]">[% val -%]</option>
  [% END -%]
</select>
</p>

<p><small><b>Go to page:</b> &nbsp;
[% IF page != pager.first_page() -%]
<a href="[% Catalyst.request.uri_with( page => pager.first_page() )
-%]">first</a>
[% END -%]

[% IF pager.previous_page -%]
<a href="[% Catalyst.request.uri_with( page => pager.previous_page() )
-%]">previous</a>
[% END -%]

[% IF pager.next_page -%]
<a href="[% Catalyst.request.uri_with( page => pager.next_page() ) -%]">next</a>
[% END -%]

[% IF page != pager.last_page() -%]
<a href="[% Catalyst.request.uri_with( page => pager.last_page() )-%]">
 last</a>
[% END -%]
</small></p>


In the controller, as Zbigniew suggested, you just have to:

my $page  = $c->req->param('page') || 1;
my $rows  = $c->req->param('rows') || 25;
$c->stash->{page} = $page;
$c->stash->{pagesize} = $rows;
my $where = { # your SQL/DBIC conditions here };
my $attr =  { page => $page, rows => $rows };
my $rs = $model->search( $where, $attr );
my $pager = $rs->pager();
$c->stash->{pager} = $pager;
$c->stash->{template} = 'your_template.tt';

HTH,

-- 
fernan



More information about the Catalyst mailing list