Pagination is a bare necessity when it comes to handling lists in your web applications. And a good pagination always makes your application more user friendly. Google, the foremost search engine has one of the best Pagination for it. So this article helps you work out a pagination for your lists. Sit tight.
In this article, we will discuss the logic around paginating a list with the Google-Pagination like Look and Feel. As you would have noticed there in Google, the current page is always displayed at the centre of the links, unless otherwise it can't be so.
The scope of the article is just to give you a brief idea on how you can go about working a pagination around your data, and this does not cover retrieval of data and/or caching your data from the database. Right, Having known this let's make a list of requirements that our pagination has to meet.
- Feature Analysis:
- Links to show on the page
- Showing previous and next Buttons when needed
- Highlighting of current page number
- Handling a random selection of page
- Showing the current page link at the centre
- Links to show on the page
(i) Defining the Properties:
Let's say right now we will allow only a maximum of 10 links to be displayed on a page. Clicking a link gets a page with say, 10 rows of data. So at a stretch a user can see 10 links/pages of 10 rows each. Doesn't matter if the total number of results is less than 100 rows of data. We will show them all. But if we get some 500 results as an example, we will not be showing 500/10=50 links. Rather we will show only the first 10 links. We will hold this in a global variable and this is a final one. Here goes the snippet:
|
Deduce the total number of result pages to be listed. This in fact we will pre-determine at the beginning. Although we may have to change this at the later course. We will come back to that shortly, be rest assured.
|
We also need to initialize certain variables like Current Page, Page to Start :the first link to show up on the page, Page to Finish :the last link to show up on the page, MidPosition :the middle link position.
|
Create the accessors/mutators for the above said variables.
(ii) Links to show on the page:
At the client side you have to know the starting-link/ending-link to display. You have to highlight the current page number while showing the links. The values for these variables have to be updated whenever you access any page links. The following code snippet demonstrates this.
|
(iii) Navigating through the Links
You need to track back one page on 'previous' action, and go forward one page from the current page for 'next' action. Then you have to update the links to display.
|
What if you want to access a page directly? Its so simple! Just set the current page number to the page you are going to access. Then don't forget to update the links to display.
|
Before displaying Next/Previous links on your page, make sure they are available for you. This is a best practice.
|
(iv) Handling page results
Using the currentPageNumber and pageSize, show the records on the page. I leave this as an exercise for you!
While this article guides you through a way of paginating your data, hundreds of open-source & commercial paging tools and tag libraries are available online. My favorite one is the Pager Tag Library. Other tools include the Display Tag Library among several others. Hope this helps and is of use to you.
EDIT:
JSP Part of the code.
|
samjosh...
No comments:
Post a Comment