pagination patch from UNTD - limit the number of page links displayed and have a...
authorivan <ivan>
Wed, 25 Oct 2006 17:36:28 +0000 (17:36 +0000)
committerivan <ivan>
Wed, 25 Oct 2006 17:36:28 +0000 (17:36 +0000)
httemplate/elements/pager.html
httemplate/search/cust_main.cgi
httemplate/search/elements/search.html

index 2327594..a53300f 100644 (file)
@@ -1,43 +1,55 @@
+% my %opt = @_;
+% my $pager = '';
 %
+% if ( $opt{'total'} != $opt{'num_rows'} && $opt{'maxrecords'} ) {
 %
-%  my %opt = @_;
-%
-%  my $pager = '';
-%  if ( $opt{'total'} != $opt{'num_rows'} && $opt{'maxrecords'} ) {
-%    unless ( $opt{'offset'} == 0 ) {
-%      $cgi->param('offset', $opt{'offset'} - $opt{'maxrecords'});
-%
-
+%   unless ( $opt{'offset'} == 0 ) {
+%     $cgi->param('offset', $opt{'offset'} - $opt{'maxrecords'});
 
       <A HREF="<% $cgi->self_url %>"><B><FONT SIZE="+1">Previous</FONT></B></A>
+
+%   }
 %
-%    }
-%    my $page = 0;
-%    for ( my $poff = 0; $poff < $opt{'total'}; $poff += $opt{'maxrecords'} ) {
-%      $page++;
-%      if ( $opt{'offset'} == $poff ) {
+%   my $page = 0;
+%   my $prevpage = 0;
+%   my $over = 0;
+%   my $step = $opt{total} / 10; # 10 evenly spaced
+%   for ( my $poff = 0; $poff < $opt{total}; $poff += $opt{maxrecords} ) {
+%     $page++;
+%      
+%     next unless
+%          $page <= 4                                          #first four
+%       || $page >= ( $opt{total} / $opt{maxrecords} ) - 3     #last four
+%       || abs( ($opt{offset}-$poff) / $opt{maxrecords} ) <= 3 #w/i 3 of current
+%       || $poff > $over                                       # evenly spaced
+%     ;           
 %
-
+%     $over += $step if $poff > $over;
+%
+%     if ( $opt{'offset'} == $poff ) {
 
         <FONT SIZE="+2"><% $page %></FONT>
-%
-%      } else {
-%        $cgi->param('offset', $poff);
-%
 
+%     } else {
+%       $cgi->param('offset', $poff);
+%
+%       if ( $page > $prevpage+1 ) {
+          ...
+%       }
 
         <A HREF="<% $cgi->self_url %>"><% $page %></A>
+
+%     }
 %
-%      }
-%    }
-%    unless ( $opt{'offset'} + $opt{'maxrecords'} > $opt{'total'} ) {
-%      $cgi->param('offset', $opt{'offset'} + $opt{'maxrecords'});
+%     $prevpage = $page;
 %
-
+%   }
+%
+%   unless ( $opt{'offset'} + $opt{'maxrecords'} > $opt{'total'} ) {
+%     $cgi->param('offset', $opt{'offset'} + $opt{'maxrecords'});
 
       <A HREF="<% $cgi->self_url %>"><B><FONT SIZE="+1">Next</FONT></B></A>
 %
-%    }
-%  }
+%   }
 %
-
+% }
index 509fb29..1b6b526 100755 (executable)
 
 
   <% $total %> matching customers found
-%
-%  #begin pager
-%  my $pager = '';
-%  if ( $total != scalar(@cust_main) && $maxrecords ) {
-%    unless ( $offset == 0 ) {
-%      $cgi->param('offset', $offset - $maxrecords);
-%      $pager .= '<A HREF="'. $cgi->self_url.
-%                '"><B><FONT SIZE="+1">Previous</FONT></B></A> ';
-%    }
-%    my $poff;
-%    my $page;
-%    for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
-%      $page++;
-%      if ( $offset == $poff ) {
-%        $pager .= qq!<FONT SIZE="+2">$page</FONT> !;
-%      } else {
-%        $cgi->param('offset', $poff);
-%        $pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
-%      }
-%    }
-%    unless ( $offset + $maxrecords > $total ) {
-%      $cgi->param('offset', $offset + $maxrecords);
-%      $pager .= '<A HREF="'. $cgi->self_url.
-%                '"><B><FONT SIZE="+1">Next</FONT></B></A> ';
-%    }
-%  }
-%  #end pager
+
+% my $pager = include( '/elements/pager.html',
+%                        'offset'     => $offset,
+%                       'num_rows'   => scalar(@cust_main),
+%                       'total'      => $total,
+%                       'maxrecords' => $maxrecords,
+%                    );
 %
 %  unless ( $cgi->param('cancelled') ) {
 %    if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
index 14e1dd0..689cbe3 100644 (file)
@@ -1,5 +1,3 @@
-%
-%
 %  # options example...  
 %  # (everything not commented required is optional)
 %  #
@@ -98,7 +96,7 @@
 %
 %  my $type = '';
 %  my $limit = '';
-%  my($maxrecords, $total, $offset, $count_arrayref);
+%  my($confmax, $maxrecords, $total, $offset, $count_arrayref);
 %
 %  if ( $cgi->param('_type') =~ /^(csv|\w*\.xls)$/ ) {
 %  
 %
 %    unless (exists($opt{'count_query'}) && length($opt{'count_query'})) {
 %      ( $opt{'count_query'} = $opt{'query'} ) =~
-%        s/^\s*SELECT\s*(.*?)\s+FROM\s/SELECT COUNT(*) FROM /i;
+%        s/^\s*SELECT\s*(.*?)\s+FROM\s/SELECT COUNT(*) FROM /i; #silly vim:/
 %    }
 %
 %    my $conf = new FS::Conf;
-%    $maxrecords = $conf->config('maxsearchrecordsperpage');
+%    $confmax = $conf->config('maxsearchrecordsperpage');
+%    if ( $cgi->param('maxrecords') =~ /^(\d+)$/ ) {
+%      $maxrecords = $1;
+%    } else {
+%      $maxrecords ||= $confmax;
+%    }
 %
 %    $limit = $maxrecords ? "LIMIT $maxrecords" : '';
 %
           )
         : ''
   %>
-% my $pager = include ( '/elements/pager.html',
-%                             'offset'     => $offset,
-%                             'num_rows'   => scalar(@$rows),
-%                             'total'      => $total,
-%                             'maxrecords' => $maxrecords,
-%                         );
 %  
 % unless ( $total ) { 
 % unless ( $opt{'disable_nonefound'} ) { 
 % } 
 % } else { 
 
-  
     <TABLE>
       <TR>
+
         <TD VALIGN="bottom">
+
+          <FORM>
+
           <% $total %> total <% $opt{'name'} %>
+
+%         if ( $confmax && $total > $confmax ) {
+%           $cgi->delete('maxrecords');
+%           $cgi->param('_dummy', 1);
+
+%#           ( show <SELECT NAME="maxrecords" onChange="this.form.submit();">
+            ( show <SELECT NAME="maxrecords" onChange="window.location = '<% $cgi->self_url %>;maxrecords=' + this.options[this.selectedIndex].value;">
+
+%             foreach my $max ( map { $_ * $confmax } qw( 1 5 10 25 ) ) {
+                <OPTION VALUE="<% $max %>" <% ( $maxrecords == $max ) ? 'SELECTED' : '' %>><% $max %></OPTION>
+%             }
+
+           </SELECT> per page )
+
+%           $cgi->param('maxrecords', $maxrecords);
+%         }
+
+
           <% defined($opt{'html_posttotal'}) 
                 ? ( ref($opt{'html_posttotal'})
                       ? &{$opt{'html_posttotal'}}()
                 : ''
           %>
           <BR>
+
 % if ( $opt{'count_addl'} ) { 
-% my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) { 
+%   my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) { 
 
-              <% sprintf( $count, $count_arrayref->[++$n] ) %><BR>
-% } 
+      <% sprintf( $count, $count_arrayref->[++$n] ) %><BR>
+
+%   } 
 % } 
+          </FORM>
 
         </TD>
+
 % unless ( $opt{'disable_download'} ) { 
 
           <TD ALIGN="right">
 
             as <A HREF="<% $cgi->self_url %>">CSV file</A>
           </TD>
+% $cgi->param('_type', "html" ); 
 % } 
 
       </TR>
       <TR>
         <TD COLSPAN=2>
-            <% $pager %>
+
+            <% my $pager = include ( '/elements/pager.html',
+                           'offset'     => $offset,
+                           'num_rows'   => scalar(@$rows),
+                           'total'      => $total,
+                           'maxrecords' => $maxrecords,
+            ) %>
 
             <% include('/elements/table-grid.html') %>
 
   <% include( '/elements/footer.html' ) %>
 % } 
 % } 
-