Pagination
Pagination
If your application needs to display a lot of data, you probably need to paginate. Even if you use the “Endless Pageless” Ajax technique of displaying items, your server will need to return chunks of results at a time instead of all at once. Otherwise, either the server would hang, the browser would time out, or your visitor will leave. We wouldn’t want that, now would we?
Fortunately, if your application is querying directly against a database, WebGUI has solved pagination for you:
sub view { my $self = shift; my $baseUrl = q{}; my $paginateAfter = 10; # Display 10 items per page my $formVar = ‘page’; # Defaults to “pn” otherwise my $sql = qq{ SELECT id FROM stuff WHERE title LIKE ? ORDER BY id }; my $placeholders = [ ‘%’ . $self->form->get( ‘title’ ) . ‘%’ ]; my $p = WebGUI::Paginator->new( $self->session, $baseUrl, $paginateAfter, $formVar, ); # Magically LIMIT results to just the requested page $p->setDataByQuery( $sql, undef, undef, $placeholders || undef ); my @ids; foreach my $row ( @{ $p->getPageData( ) } ) { push @ids, $row->{id}; # Depending on your SQL, you can fetch other fields too } my $var = {}; $p->appendTemplateVars( $var ); # Prefills special template variables for pagination for my $id ( @ids ) { push @{ $var->{results} }, $self->getEvenMoreDetailsForId( $id ); } return $self->processTemplate( $var, undef, $self->{_viewTemplate} ); }
In another posting, I (or possibly someone else) will describe the templating and design of our pagination. |

