Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/articles/perl-tutorials/)
-   -   Easy Pagination In Perl (http://www.go4expert.com/articles/easy-pagination-perl-t18335/)

pradeep 3Jul2009 16:59

Easy Pagination In Perl
 

Introduction



Pagination is a very basic requirement of a web application, in this article we'll see how to implement pagination easily using the Perl module Data::Pageset.

Basically there are two types of pagination, Jumping and Sliding. In the jumping type the current page jumps from the starting position in a frame till the end and goes till the get before moving on to the next frame. Whereas, in the sliding type the current page remains in the center of the frame (of course except the current page is not in the starting or ennding half of the pageset), here is an illustration of the jumping and sliding pagination types which will help you understand the logic better.

Jumping
Code:

  [1] 2  3  4  5  =>  # first frame: [1-5]
<=  1 [2] 3  4  5  =>
<=  1  2 [3] 4  5  =>
<=  1  2  3 [4] 5  =>
<=  1  2  3  4 [5] =>  # current page jumps to the next frame
<= [6] 7  8  9 10  =>  # second frame: [6-10]
<=  6 [7] 8  9 10  =>
<=  6  7 [8] 9 10  =>

Sliding
Code:

  [1] 2  3  4  5  =>
<= 1 [2] 3  4  5  =>
<= 1  2 [3] 4  5  =>  # from here onwards current page remaing in the center
<= 2  3 [4] 5  6  =>  # current page
<= 3  4 [5] 6  7  =>  # and it stays there...
<= 4  5 [6] 7  8  =>
<= 5  6 [7] 8  9  =>
<= 6  7 [8] 9 10  =>

The Code



Code: Perl

## the default is Jumping
use Data::Pageset;

my $total_entries = 500;
my $entries_per_page = 10;
my $current_page = 30;
my $pages_per_set = 11;

my $paging_jump = Data::Pageset->new({
    'total_entries'       => $total_entries,
    'entries_per_page'    => $entries_per_page,
    # Optional, will use defaults otherwise.
    'current_page'        => $current_page,
    'pages_per_set'       => $pages_per_set
});

my $paging_slide = Data::Pageset->new({
    'total_entries'       => $total_entries,
    'entries_per_page'    => $entries_per_page,
    # Optional, will use defaults otherwise.
    'current_page'        => $current_page,
    'pages_per_set'       => $pages_per_set,
    'mode'                => 'slide'
});

# Print the page numbers of the current set : Jumping
foreach my $page (@{$paging->pages_in_set()})
{
    if($page == $paging_jump->current_page())
    {
        print "[$page] ";
    }
    else
    {
        print "$page ";
    }
}

print "\n\n";

# Print the page numbers of the current set : Jumping
foreach my $page (@{$paging_slide->pages_in_set()})
{
    if($page == $paging->current_page())
    {
        print "[$page] ";
    }
    else
    {
        print "$page ";
    }
}


Hope you liked the article and find it useful, you can check more about the module at http://search.cpan.org/~llap/Data-Pa...ata/Pageset.pm

shabbir 3Aug2009 14:34

Re: Easy Pagination In Perl
 
Nominate this article for Article of the month - Jul 2009

shabbir 16Aug2009 13:20

Re: Easy Pagination In Perl
 
Vote for this article for Article of the month - July 2009


All times are GMT +5.5. The time now is 19:48.