
Pagination seems like a server side solution if you have a huge number of data , it is allows the user to browse through the rows in a database table. Lets say you have small amount of data you need to do pagination. Then it will be good idea if you go for client site solution. In this tutorial I will show you how to paginating a table in jQuery.
We will start with markup
HTML
Styling with CSS
I have created the “style.css” file into “css” folder. All our css styling will go to this file
Above code we can place end of <tittle> tag.
Basic CSS
Zebra Strips
CSS 3 introduces the pseudo-class “nth-child”, which makes it ridiculously simple to do this without javascript.To add Zebra Strips to a table, all you need to do is adding the following pseudo-class.
- nth-of-type(even) add background color to every even rows likewise odd.
- thead th is a header.
In my previous tutorial Zebra-striping Tables with CSS3 you can learn more about Zebra-striping.
Javascript
Download the Following
Dowonload jquery-1.4.4.min.js
I have created a "pagination.js" file into “js” folder. All of our jQuery code will go into this javascript file.
Above code we can place end of <tittle> tag.
The jQuery code to divide the rows of the table in to pages (depending on the number of rows that we want to see per page) and to display the respective rows when a page number is clicked.
We already created the pagination.js. Add the following code into pagination.js file file.
Above jQuery code start with counting the number of rows (tr elements nested inside the tbody element) and storing the count in variable rows.
In this example we'll assume that we need to see only 5 rows per page, so we initialize the value of the variable no_rec_per_page equal to 5.
We next have to find out the total number of pages by dividing the total count of the rows by the number of records that we want to see per page. The count of the pages is assigned to the variable no_pages.
The final action to take before we start on the event handlers is to set up the page-number display. We start this by defining a div element of ID pages and assigning it to the variable $pagenumbers.
Inside the for loop, we create a few span elements (equal to the number of pages) that contain the sequence of page numbers 1,2… and the span element is assigned the class name page so that the style properties defined in the class selector page class is automatically applied to all the page numbers.
Finally, all the span elements containing the page numbers are appended to the div element of ID pages.
insertAfter and insertBefore :
To finish the job we insert the div element of ID pages that was stored in the variable $pagenumbers after the table element, which makes the page numbers appear below the table. Likewise if you need to insert the div element before the table element you can use insertBefore which make page number above the table element.
Next the hover() event to the page numbers (span element of the class .pages). In the event handler, and we highlight the page numbers when the mouse pointer moves over them. Conversely, when the mouse pointer moves away from them we remove the style properties of the style rule .hover.
The for loop is added to make the rows of the first page number (depending on the value assigned to the variable no_rec_per_page)
In the click event handler of the page numbers, we hide all the rows of the table. We then display the rows that fall within the clicked page number using the tr array.






Comments
Its really so useful and very clear code to understand.
Thank you once again.
1 2 3 4 5 Next
this numbers should be in the boxes then what is the code for this do reply me and where we need to add that code.....
20. var tr=$('table tbody tr');
21. for(var i=0;i<=no_rec_per_page -1;i++)
22. {
23. $(tr).show();
24. }
In case your table has less rows than the number of rows per page it shows an error.
I have changed those lines for these ones:
20. var tr=$('#'+tableId+' tbody tr');
21. for(var i=0; i<no_rec_per_p age && i<tr.length; i++)
22. {
23. $(tr).show();
24. }
Everything works perfect now.
Thanks again!
RSS feed for comments to this post