Can anyone provide some idea/logic to write the pagination logic for the search page i am working on?
The information i have is total number of pages for that search- 10 records per page also i am been sent the both the previous and next page number(no problem writing the logic all i need to do i pull that info and populate. I am also getting the info which page i am on. I can only display 10 pages like below
<previous 1 |2 |3 | 4| 5 | 6 | 7 | 8 | 9 | 10 next>
Say if total pages are 15 and when user click next then i need to display like this
<previous 2 |3 |4 |5 |6 |7 |8 |9 |10 |11 next>
At any time i just need to show then 10 pages in the pagination.
#set($start = 1)
#set($end = $Integer.parseInt($searchTO.getPagination().getNumberofPages()))
#set($range = [$start..$end])
#set($iter = 1)
#foreach($i in $range)
#foreach($link in $searchTO.getPagination().getDirectPageLinks())
#if($i == $iter)
#if ($Integer.parseInt($searchTO.getPagination().getPageNumber())==$iter)
<a class="search_current" href="/?_page=SEARCH&_action=SEARCH$link">$i  |</a>
#else
<a href="/?_page=SEARCH&_action=SEARCH$link">$i  |</a>
#end
#set($iter = 1)
#break
#else
#set($iter=$iter+1)
#end
#end
#end
Here is how I would implement it:
It is generally a good idea to create a Filter class that filters data and contains pagination related information for you. I use something like this:
You could have special subclasses of filters (depending on what you want to retrieve) and and each subclass would implement it’s
createCountQuery()andcreateQuery().You would put then your
Filterto theVelocitycontext and you could retrieve all the information you need from this class.When you set the current page pf course you update all the other information that you need (i.e. currentStartPageNo, currentEndPageNo).
You could also have a
refresh()method to put the filter back to its initial state.And of course you should keep the instance of the same filter on the session (I mean you web framework like Struts, Turbine etc.) while the user navigates on the search page to which the
Filterbelongs.This is just a guideline, an idea, it is not fully written executable code, just an example to get you started in a direction.
I would also recommend you a jQuery plugin called jqGrid that has pagination support (although you have to have a backed to retrieve data) and a lot more cool stuff.
You can use it to display your data in a grid. I use it together with
Velocitywith no problem. It has a lot of very nice and useful features like filter toolbar, editable cells, data transfer inJSON,XMLetc.Honestly, I do not know if it has pagination like you need (I use it with only one page displayed in the navigation and you can not click on a page just use the next a prev buttons to navigate), but it may have support for that.