Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Matts (1087)

Matts
  (email not shown publicly)

I work for MessageLabs [messagelabs.com] in Toronto, ON, Canada. I write spam filters, MTA software, high performance network software, string matching algorithms, and other cool stuff mostly in Perl and C.

Journal of Matts (1087)

Wednesday July 09, 2003
03:03 AM

Pagination again

[ #13345 ]

OK, so it looked at first as though Data::Pageset would be what I wanted (thanks davorg and Ranguard!), and then it looked like it didn't quite give me what I needed...

The problem seemed to stem from a design decision in Data::Pageset - it was designed for explicitly paging through sets of data (via buttons to "page up" and "page down"), rather than what I wanted which was to do the paging a-la google, where it moves to the next pageset when you click on the last page in the list. i.e. in the below, clicking on 5 should move you to a set showing pages 5..10:

   <<Prev  1    2   *3*   4   5  Next>>

Whereas clicking on Prev/Next should take you to the next page, rather than the next pageset. This makes page flipping really easy (just hover the mouse over the "Next" button) and sensible.

Unfortunately Data::Pageset's boundaries are at distinct dividing points in the set, so you can't tell it to flip to the next/prev pageset when you get to the last number on a page. This seemed like a showstopper to me so I went on to do other things while I thought about it.

And then finally this morning the solution dawned on me. I could simply reduce the number of pages I expected to show, and subtract one and add one at the end to produce the effect I needed.

I ended up with the following code, which does what I need:

    if (my $n = $page->previous_page) {
        <pagenav previous="1">
            <xsp:attribute
                name="page"><xsp:expr>$n</xsp:expr></xsp:attribute>
            &lt;&lt;
        </pagenav>
    }
    if (my $n = $page->previous_set) {
        <pagenav>
            <xsp:attribute
                name="page"><xsp:expr>$n + $page->pages_per_set - 1</xsp:expr></xsp:attribute>
            <xsp:expr>$n + $page->pages_per_set - 1</xsp:expr>
        </pagenav>
    }
    foreach my $n ( @{ $page->pages_in_set } ) {
        <pagenav>
            <xsp:attribute name="current">
                <xsp:expr>$n == $page->current_page</xsp:expr>
            </xsp:attribute>
            <xsp:attribute
                name="page"><xsp:expr>$n</xsp:expr></xsp:attribute>
            <xsp:expr>$n</xsp:expr>
        </pagenav>
    }
    if (my $n = $page->next_set) {
        <pagenav>
            <xsp:attribute
                name="page"><xsp:expr>$n</xsp:expr></xsp:attribute>
            <xsp:expr>$n</xsp:expr>
        </pagenav>
    }
    if (my $n = $page->next_page) {
        <pagenav next="1">
            <xsp:attribute
                name="page"><xsp:expr>$n</xsp:expr></xsp:attribute>
            >>
        </pagenav>
    }

Now I'm going away to patch AxKit so that I can get rid of those HORRIBLE <xsp:attribute> tags - supporting attribute value templates a-la XSLT will make this significantly cleaner.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.