Now allows for generic pagination of any data. Also made massive performance and memory improvements.
This commit is contained in:
parent
047e648bb3
commit
040feef5c0
1 changed files with 91 additions and 63 deletions
|
|
@ -16,6 +16,7 @@ package WebGUI::Paginator;
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use WebGUI::International;
|
use WebGUI::International;
|
||||||
|
use WebGUI::Session;
|
||||||
use WebGUI::URL;
|
use WebGUI::URL;
|
||||||
|
|
||||||
=head1 NAME
|
=head1 NAME
|
||||||
|
|
@ -26,21 +27,23 @@ use WebGUI::URL;
|
||||||
|
|
||||||
use WebGUI::Paginator;
|
use WebGUI::Paginator;
|
||||||
$p = WebGUI::Paginator->new("/index.pl/page_name?this=that",\@row);
|
$p = WebGUI::Paginator->new("/index.pl/page_name?this=that",\@row);
|
||||||
$p->getBar(2);
|
$html = $p->getBar;
|
||||||
$p->getBarAdvanced(2);
|
$html = $p->getBarAdvanced;
|
||||||
$p->getBarSimple(2);
|
$html = $p->getBarSimple;
|
||||||
$p->getBarTraditional(2);
|
$html = $p->getBarTraditional;
|
||||||
$p->getFirstPageLink(2);
|
$html = $p->getFirstPageLink;
|
||||||
$p->getLastPageLink(2);
|
$html = $p->getLastPageLink;
|
||||||
$p->getNextPageLink(2);
|
$html = $p->getNextPageLink;
|
||||||
$p->getNumberOfPages;
|
$integer = $p->getNumberOfPages;
|
||||||
$p->getPage(2);
|
$html = $p->getPage;
|
||||||
$p->getPageLinks(2);
|
$arrayRef = $p->getPageData;
|
||||||
$p->getPreviousPageLink(2);
|
$integer = $p->getPageNumber;
|
||||||
|
$html = $p->getPageLinks;
|
||||||
|
$html = $p->getPreviousPageLink;
|
||||||
|
|
||||||
=head1 DESCRIPTION
|
=head1 DESCRIPTION
|
||||||
|
|
||||||
Package that paginates rows of data for display on the web.
|
Package that paginates rows of arbitrary data for display on the web.
|
||||||
|
|
||||||
=head1 METHODS
|
=head1 METHODS
|
||||||
|
|
||||||
|
|
@ -49,24 +52,6 @@ use WebGUI::URL;
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------
|
|
||||||
sub _generatePages {
|
|
||||||
my (@page, $row, @rows, $rowRef, $pn, $i, $itemsPerPage);
|
|
||||||
$rowRef = $_[0];
|
|
||||||
@rows = @{$rowRef};
|
|
||||||
$itemsPerPage = $_[1];
|
|
||||||
foreach $row (@rows) {
|
|
||||||
$page[$pn] .= $row;
|
|
||||||
$i++;
|
|
||||||
if ($i >= $itemsPerPage) {
|
|
||||||
$i = 0;
|
|
||||||
$pn++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return \@page;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------
|
#-------------------------------------------------------------------
|
||||||
|
|
||||||
=head2 getBar ( [ pageNumber ] )
|
=head2 getBar ( [ pageNumber ] )
|
||||||
|
|
@ -75,9 +60,9 @@ sub _generatePages {
|
||||||
last links. If there's only one page, nothing is returned.
|
last links. If there's only one page, nothing is returned.
|
||||||
|
|
||||||
=item pageNumber
|
=item pageNumber
|
||||||
|
|
||||||
The page number you're currently looking at. If omited, page one
|
Defaults to the page you're currently viewing. This is mostly here
|
||||||
is assumed.
|
as an override and probably has no real use.
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
|
|
@ -110,8 +95,8 @@ sub getBar {
|
||||||
|
|
||||||
=item pageNumber
|
=item pageNumber
|
||||||
|
|
||||||
The page number you're currently looking at. If omited, page one
|
Defaults to the page you're currently viewing. This is mostly here
|
||||||
is assumed.
|
as an override and probably has no real use.
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
|
|
@ -145,8 +130,8 @@ sub getBarAdvanced {
|
||||||
|
|
||||||
=item pageNumber
|
=item pageNumber
|
||||||
|
|
||||||
The page number you're currently looking at. If omited, page one
|
Defaults to the page you're currently viewing. This is mostly here
|
||||||
is assumed.
|
as an override and probably has no real use.
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
|
|
@ -175,8 +160,8 @@ sub getBarSimple {
|
||||||
|
|
||||||
=item pageNumber
|
=item pageNumber
|
||||||
|
|
||||||
The page number you're currently looking at. If omited, page one
|
Defaults to the page you're currently viewing. This is mostly here
|
||||||
is assumed.
|
as an override and probably has no real use.
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
|
|
@ -205,14 +190,14 @@ sub getBarTraditional {
|
||||||
|
|
||||||
=item pageNumber
|
=item pageNumber
|
||||||
|
|
||||||
The page number you're currently looking at. If omited, page one
|
Defaults to the page you're currently viewing. This is mostly here
|
||||||
is assumed.
|
as an override and probably has no real use.
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
sub getFirstPageLink {
|
sub getFirstPageLink {
|
||||||
my ($text, $pn);
|
my ($text, $pn);
|
||||||
$pn = $_[1] || 1;
|
$pn = $_[1] || $_[0]->getPageNumber;
|
||||||
$text = '|<'.WebGUI::International::get(404);
|
$text = '|<'.WebGUI::International::get(404);
|
||||||
if ($pn > 1) {
|
if ($pn > 1) {
|
||||||
return '<a href="'.
|
return '<a href="'.
|
||||||
|
|
@ -232,14 +217,14 @@ sub getFirstPageLink {
|
||||||
|
|
||||||
=item pageNumber
|
=item pageNumber
|
||||||
|
|
||||||
The page number you're currently looking at. If omited, page one
|
Defaults to the page you're currently viewing. This is mostly here
|
||||||
is assumed.
|
as an override and probably has no real use.
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
sub getLastPageLink {
|
sub getLastPageLink {
|
||||||
my ($text, $pn);
|
my ($text, $pn);
|
||||||
$pn = $_[1] || 1;
|
$pn = $_[1] || $_[0]->getPageNumber;
|
||||||
$text = WebGUI::International::get(405).'>|';
|
$text = WebGUI::International::get(405).'>|';
|
||||||
if ($pn != $_[0]->getNumberOfPages) {
|
if ($pn != $_[0]->getNumberOfPages) {
|
||||||
return '<a href="'.
|
return '<a href="'.
|
||||||
|
|
@ -259,14 +244,14 @@ sub getLastPageLink {
|
||||||
|
|
||||||
=item pageNumber
|
=item pageNumber
|
||||||
|
|
||||||
The page number you're currently looking at. If omited, page one
|
Defaults to the page you're currently viewing. This is mostly here
|
||||||
is assumed.
|
as an override and probably has no real use.
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
sub getNextPageLink {
|
sub getNextPageLink {
|
||||||
my ($text, $pn);
|
my ($text, $pn);
|
||||||
$pn = $_[1] || 1;
|
$pn = $_[1] || $_[0]->getPageNumber;
|
||||||
$text = WebGUI::International::get(92).'»';
|
$text = WebGUI::International::get(92).'»';
|
||||||
if ($pn < $_[0]->getNumberOfPages) {
|
if ($pn < $_[0]->getNumberOfPages) {
|
||||||
return '<a href="'.WebGUI::URL::append($_[0]->{_url},($_[0]->{_pn}.'='.($pn+1))).'">'.$text.'</a>';
|
return '<a href="'.WebGUI::URL::append($_[0]->{_url},($_[0]->{_pn}.'='.($pn+1))).'">'.$text.'</a>';
|
||||||
|
|
@ -285,7 +270,9 @@ sub getNextPageLink {
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
sub getNumberOfPages {
|
sub getNumberOfPages {
|
||||||
return $#{$_[0]->{_pageRef}}+1;
|
my $pageCount = int(($#{$_[0]->{_rowRef}}+1)/$_[0]->{_rpp});
|
||||||
|
$pageCount++ unless (($#{$_[0]->{_rowRef}}+1)%$_[0]->{_rpp} == 0);
|
||||||
|
return $pageCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -293,21 +280,64 @@ sub getNumberOfPages {
|
||||||
|
|
||||||
=head2 getPage ( [ pageNumber ] )
|
=head2 getPage ( [ pageNumber ] )
|
||||||
|
|
||||||
Returns the data from the page specified.
|
Returns the data from the page specified as a string.
|
||||||
|
|
||||||
|
NOTE: This is really only useful if you passed in an array reference
|
||||||
|
of strings when you created this object.
|
||||||
|
|
||||||
=item pageNumber
|
=item pageNumber
|
||||||
|
|
||||||
The page number you wish to view. If omitted, page one is assumed.
|
Defaults to the page you're currently viewing. This is mostly here
|
||||||
|
as an override and probably has no real use.
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
sub getPage {
|
sub getPage {
|
||||||
my ($pn);
|
return join("",@{$_[0]->getPageData($_[1])});
|
||||||
$pn = $_[1] || 1;
|
|
||||||
return $_[0]->{_pageRef}[$pn-1];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------
|
||||||
|
|
||||||
|
=head2 getPageData ( [ pageNumber ] )
|
||||||
|
|
||||||
|
Returns the data from the page specified as an array reference.
|
||||||
|
|
||||||
|
=item pageNumber
|
||||||
|
|
||||||
|
Defaults to the page you're currently viewing. This is mostly here
|
||||||
|
as an override and probably has no real use.
|
||||||
|
|
||||||
|
=cut
|
||||||
|
|
||||||
|
sub getPageData {
|
||||||
|
my ($i, @pageRows, $allRows, $pageCount, $pageNumber, $rowsPerPage, $pageStartRow, $pageEndRow);
|
||||||
|
$pageNumber = $_[1] || $_[0]->getPageNumber;
|
||||||
|
$pageCount = $_[0]->getNumberOfPages;
|
||||||
|
return [] if ($pageNumber > $pageCount);
|
||||||
|
$rowsPerPage = $_[0]->{_rpp};
|
||||||
|
$pageStartRow = ($pageNumber*$rowsPerPage)-$rowsPerPage;
|
||||||
|
$pageEndRow = $pageNumber*$rowsPerPage;
|
||||||
|
$allRows = $_[0]->{_rowRef};
|
||||||
|
for ($i=$pageStartRow; $i<$pageEndRow; $i++) {
|
||||||
|
$pageRows[$i-$pageStartRow] = $allRows->[$i] if ($i <= $#{$_[0]->{_rowRef}});
|
||||||
|
}
|
||||||
|
return \@pageRows;
|
||||||
|
}
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------
|
||||||
|
|
||||||
|
=head2 getPageNumber ( )
|
||||||
|
|
||||||
|
Returns the current page number. If no page number can be found
|
||||||
|
then it returns 1.
|
||||||
|
|
||||||
|
=cut
|
||||||
|
|
||||||
|
sub getPageNumber {
|
||||||
|
return $session{form}{$_[0]->{_pn}} || 1;
|
||||||
|
}
|
||||||
|
|
||||||
#-------------------------------------------------------------------
|
#-------------------------------------------------------------------
|
||||||
|
|
||||||
=head2 getPageLinks ( [ pageNumber ] )
|
=head2 getPageLinks ( [ pageNumber ] )
|
||||||
|
|
@ -316,14 +346,14 @@ sub getPage {
|
||||||
|
|
||||||
=item pageNumber
|
=item pageNumber
|
||||||
|
|
||||||
The page number you're currently looking at. If omited, page one
|
Defaults to the page you're currently viewing. This is mostly here
|
||||||
is assumed.
|
as an override and probably has no real use.
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
sub getPageLinks {
|
sub getPageLinks {
|
||||||
my ($i, $output, $pn);
|
my ($i, $output, $pn);
|
||||||
$pn = $_[1] || 1;
|
$pn = $_[1] || $_[0]->getPageNumber;
|
||||||
for ($i=0; $i<$_[0]->getNumberOfPages; $i++) {
|
for ($i=0; $i<$_[0]->getNumberOfPages; $i++) {
|
||||||
if ($i+1 == $pn) {
|
if ($i+1 == $pn) {
|
||||||
$output .= ' '.($i+1).' ';
|
$output .= ' '.($i+1).' ';
|
||||||
|
|
@ -345,14 +375,14 @@ sub getPageLinks {
|
||||||
|
|
||||||
=item pageNumber
|
=item pageNumber
|
||||||
|
|
||||||
The page number you're currently looking at. If omitted, page one
|
Defaults to the page you're currently viewing. This is mostly here
|
||||||
is assumed.
|
as an override and probably has no real use.
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
sub getPreviousPageLink {
|
sub getPreviousPageLink {
|
||||||
my ($text, $pn);
|
my ($text, $pn);
|
||||||
$pn = $_[1] || 1;
|
$pn = $_[1] || $_[0]->getPageNumber;
|
||||||
$text = '«'.WebGUI::International::get(91);
|
$text = '«'.WebGUI::International::get(91);
|
||||||
if ($pn > 1) {
|
if ($pn > 1) {
|
||||||
return '<a href="'.WebGUI::URL::append($_[0]->{_url},($_[0]->{_pn}.'='.($pn-1))).'">'.$text.'</a>';
|
return '<a href="'.WebGUI::URL::append($_[0]->{_url},($_[0]->{_pn}.'='.($pn-1))).'">'.$text.'</a>';
|
||||||
|
|
@ -397,9 +427,7 @@ sub new {
|
||||||
$rowRef = shift;
|
$rowRef = shift;
|
||||||
$rowsPerPage = shift || 25;
|
$rowsPerPage = shift || 25;
|
||||||
$formVar = shift || "pn";
|
$formVar = shift || "pn";
|
||||||
$pageRef = _generatePages($rowRef,$rowsPerPage);
|
bless {_url => $currentURL, _rpp => $rowsPerPage, _rowRef => $rowRef, _pn => $formVar}, $class;
|
||||||
bless {_url => $currentURL, _rpp => $rowsPerPage, _rowRef => $rowRef,
|
|
||||||
_pn => $formVar, _pageRef => $pageRef}, $class;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue