I have a CGI page with a table which is populated by fetching the data from database, in a word its li开发者_如何学Pythonke a DATAGRID.
And just at the bottom right end of tha Grid I need a link like "First << 1 2 >> Last" or like" |< < > >| "on clicking which I can navigate to and fro the records. And I am intend to have "10" records per page.
While surfing I got a piece of code which I am going to paste in the code field. But problem is that it displays the paging link something like this "1 2 3 4 5 ..and so on". But I am not willing to have this paging format because the number of records increases the length of the link also goes on increasing. So can this code be modified to the format which I am intending to have?
#!C:\perl\bin\perl.exe -wT
use CGI;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
use warnings;
use DBI;
my $query = new CGI;
my $bornum;
my $itemnum;
my $i;
my @overduedata;
my $pageNum =$query->param('pageNum');
print "Content-Type: text/html\n\n";
unless($pageNum) {
$pageNum=0;
}
my $offset=$query->param('offset');
unless($offset) {
$offset=10;
}
$i=0;
my $numOfRec = 100;
while ($i < $numOfRec){
$bornum = "bornum" . $i;
$itemnum = "itmnum" . $i;
push (@overduedata, { bornum => $bornum, itemnum => $itemnum });
$i = $i + 1;
}
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">";
print "\n<form>";
print "\nNo: of records per page : <input type=text name=offset>";
print "\n<input type=submit value=submit>";
print "\n</form>";
print "\n<br> No: of rec per page = " . $offset . " -- pageNum = " . $pageNum ;
print "<table border=1>";
my $startDisplay = ($pageNum) * $offset;
my $endDisplay = ($pageNum + 1) * $offset;
$i = $startDisplay;
while ($i < $endDisplay){
print "<tr><td>" . $i . "</td><td>" . $overduedata[$i]->{'bornum'} . "</td><td>" .
$overduedata[$i]->{'itemnum'} . "</td></tr>";
$i = $i + 1;
}
print "</table>";
my $numofPages = $numOfRec / $offset;
$i = 0;
print "<table border=1><tr>";
while ($i < $numofPages){
print "<td> <a href = ?pageNum=" . $i . "&offset=" . $offset . ">" .
$i . "</a></td>";
$i = $i + 1;
}
print "<tr></table>";
---------------------------------------------------------------------------------------
I think you probably want Data::Pageset.
If all you don't like are the links, then you can modify the loop at the end to print the links you want. It's just a matter manipulating page numbers.
( I don't know where you got this code, but "page 0" is not user-friendly to even programmers who have been working with 0 offset for decades! And if you know your page size, the offset is needless data, and as it indicates more page size, it's also wrong. )
So the simplest change is:
my $pageN;
if ( $pageNum > 0 ) {
print q[<td><a href="?pageNum=1">|<</a></td>];
$pageN = $pageNum - 1;
print qq[<td><a href="?pageNum=$pageN"><</a></td>];
}
else { # don't link to the current page
print q[<td><span class="currentpage">|<</span></td>];
print q[<td><span class="currentpage"><</span></td>];
}
if ( $pageNum < ( $numofPages - 1 )) {
$pageN = $pageNum + 1;
print qq[<td><a href="?pageNum=$pageN">></a></td>];
print qq[<td><a href="?pageNum=$numofPages">>|</a></td>];
}
else { # don't link to the current page
print q[<td><span class="currentpage">></span></td>];
print q[<td><span class="currentpage">>|</span></td>];
}
Just a note: not linking to the current page helps a user understand quicker where they are in the list. Also it means that they can't hit the server just to reload the page. You want to be able to clearly communicate that they are already on the first or last page.
However, part of your question is how would you do that in Perl CGI. Using actual Perl CGI, you don't print '<td>'
. You use methods to create tags:
my @cells
= map {
my ( $page, $text ) = @$_;
( $query->td(
$page > 0 and $page <= $numofPages and $pageNum != $page
? $query->a( { href => "?pageNum=$page" }, $text )
: $query->span( { -class => 'currentpage' }, $text )
), "\n"
)
} ( [ 1 => '|<' ]
, [ ( $pageNum - 1 ) => '<' ]
, [ ( $pageNum + 1 ) => '>' ]
, [ $numofPages => '>|' ]
)
;
print $query->table(
{ -class => 'pagenav', -border => 1 }
, $query->Tr( { -class => 'pagenavrow' } @cells )
);
You should note that I also modified it so that your first page is #1 as well.
Since you indicated that you might want some page numbers as well, the only adjustment I had to make was to compute the min-max range, like so:
use List::Util qw<max min>;
my $min_page = max( 1, $pageNum - 5 );
my $max_page = min( $numofPages, $min_page + 10 );
$min_page = max( 1, min( $min_page, $max_page - 10 ));
And use those values to add numbered page links to the list passed to the map
creating @cells
:
( [ 1 => '|<' ]
, [ ( $pageNum - 1 ) => '<' ]
, ( map { [ $_ => $_ ] } $min_page..$max_page )
, [ ( $pageNum + 1 ) => '>' ]
, [ $numofPages => '>|' ]
)
精彩评论