[dancer-users] Example of pagination with DBIC?
John Stoffel
john at stoffel.org
Wed Oct 22 15:12:21 BST 2014
Jacques> Since a GET will put parameters into the URL, you can just
Jacques> re-use the URL and change the page parameter.
Jacques> /?name=test&city=montreal&page=1 <- to change pages
So, I've been slowly hacking away at this and I think I have it mostly
working, but the question I have now is what is the best way to
build/modify the URL to support paging?
For example, I use the following URL
http://localhost:3000/?Name=&Sex=F&submit=Search
when I do my simple query. My base page is 1 if I have multiple
results. And after a bit of reading, I figure I can just use the
request->request_uri helper to get when I need and strip out more
stuff. Something like this, assuming I have more than 10 (default)
results to show, etc:
my $page_prev = $page - 1;
my $page_next = $page + 1;
my $rec_start = (($page-1) * $limit) + 1;
my $rec_end = ($page * $limit) - 1;
my $page_prev_uri = "";
my $page_next_uri = "";
if ($page_prev > 1) {
my $uri = request->request_uri;
$uri =~ s/\&page=\d+//;
$page_prev_uri = join("",$uri, '&page=' . $page-1);
}
if ($page_next < POSIX::ceil($count / $limit)) {
my $uri = request->request_uri;
$uri =~ s/\&page=\d+//;
$page_next_uri = join("",$uri,'&page=',$page+1);
}
All this is just basic math and works fine. The trick seems to be in
the URI re-writing, which *almost* works. So my search form just has:
<FORM ACTION="/" METHOD="get">
Search Name: <INPUT TYPE="text" NAME="Name" /><br>
Sex: <SELECT NAME="Sex">
<OPTION>
<OPTION VALUE="F">Female</OPTION>
<OPTION VALUE="M">Male</OPTION>
<OPTION VALUE="U">Unknown</OPTION>
</SELECT>
<INPUT TYPE="submit" NAME="submit" VALUE="Search" />
</form>
And the URI when submitted looks like:
/?Name=&Sex=F&submit=Search
So my questions are:
1. Should I strip off the "&submit=Search" part when I build my <A
HREF=...>prev</A> and next links?
2. Should I pre-place a <INPUT type="hidden" name="page" value="1" />
field in my search form, with the obvious updating if I'm searching
on a different page?
3. Right now I'm using a single template to both show the search form
and the results. I'm thinking I really want to split this
up... and of course I want to allow the users to "refine" a search
so I need to pass back defaults for the various forms. Time to
learn how Dancer does sessions... :-)
Thanks,
John
More information about the dancer-users
mailing list