[Catalyst] OT: Better TT pager?

Zbigniew Lukasiak zzbbyy at gmail.com
Tue Dec 23 13:13:50 GMT 2008


On Tue, Dec 23, 2008 at 12:34 PM, 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:
>
> ---
>
> <hr>
> [% IF pager.last_page > 1 %]
> <p><span class="lead">
>  [% IF pager.previous_page %]
>    [<a href="[% c.req.uri_with({'page' => pager.previous_page}) %]">previous</a
>>]
>  [% ELSE %]
>    [previous]
>  [% END %]
>  [% FOR page IN [1..pager.last_page] %]
>    [% IF page == pager.current_page %]
>      [[% page %]]
>    [% ELSE %]
>      [<a href="[% c.req.uri_with({'page' => page}) %]">[% page %]</a>]
>    [% END %]
>  [% END # FOR...%]
>
>  [% IF pager.next_page %]
>    [<a href="[% c.req.uri_with({'page' => pager.next_page}) %]">next</a>]
>  [% ELSE %]
>    [next]
>  [% END %]
> </span></p>
> [% END # IF... %]
>
> ---
>
> 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?
>

In my very old code I have:

sub pages_links {
    my ( $c, $pages_count, $valid ) = @_;
    my $curr_page = $valid->{page} || 1;
    my $result = '';
    if ( $curr_page > 12 ){
        $result .= create_param_link ( $c, 'page', 1, $valid ) . ' ... ';
    }
    for my $page ( max ( 1, $curr_page - 10 ) .. min ( $curr_page +
10, $pages_count ) ){
        $result .= create_param_link ( $c, 'page', $page, $valid );
    }
    if ( $curr_page < $pages_count - 11 ){
        $result .= ' ... ' . create_param_link ( $c, 'page',
$pages_count, $valid );
    }
    return $result;
}

This was before I discovered uri_with (or perhaps even before that was
introduced) and create_param_link is what I wrote instead. $valid is a
hash ref of the rest of the parameters.

-- 
Zbigniew Lukasiak
http://brudnopis.blogspot.com/
http://perlalchemy.blogspot.com/



More information about the Catalyst mailing list