Exforsys.com
 
Home Tutorials jQuery
 

jQuery - JavaScript Pagination

 

Learning jQuery - JavaScript Pagination

Let's examine how we would add JavaScript pagination to the table we have already made sortable in the browser. First, we'll focus on displaying a particular page of data, disregarding user interaction for now:<

 

Sponsored Links

 

v style='clear: both;'>
Sample Code
  1. $(document).ready(function() {
  2. $('table.paginated').each(function() {
  3. var currentPage = 0
  4. var numPerPage = 10
  5. var $table = $(this)
  6. $table.find('tbody tr').show()
  7. .lt(currentPage * numPerPage)
  8. .hide()
  9. .end()
  10. .gt((currentPage + 1) * numPerPage - 1)
  11. .hide()
  12. .end()
  13. })
  14. })
Copyright exforsys.com



This code displays the first page—ten rows of data.


Once again we rely on the presence of a &tl; tbody > element to separate data from headers; we don't want to have the headers or footers disappear when moving on to the second page. For selecting the rows containing data, we show all the rows first, then select the rows before and after the current page, hiding them.


The method chaining supported by jQuery makes another appearance here when we filter the set of matched rows twice, using .end() in between to pop the current filter off the stack and start afresh with a new filter.


The most error-prone task in writing this code is formulating the expressions to use in the filters. To use the .lt() and .gt() methods, we need to find the indices of the rows at the beginning and end of the current page. For the beginning row, we just multiply the current page number by the number of rows on each page.


Multiplying the number of rows by one more than the current page number gives us the beginning row of the next page; to find the last row of the current page, we must subtract one from this.


Displaying the Pager

To add user interaction to the mix, we need to place the pager itself next to the table. We could do this by simply inserting links for the pages in the HTML markup, but this would violate the progressive enhancement principle we've been espousing. Instead, we should add the links using JavaScript, so that users without scripting available are not misled by links that cannot work.


To display the links, we need to calculate the number of pages and create a corresponding number of DOM elements:

Sample Code
  1. var numRows = $table.find('tbody tr').length
  2. var numPages = Math.ceil(numRows / numPerPage)
  3. var $pager = $('</p><br>
  4. <div class="pager"></div>
  5. ')
  6. for (var page = 0 page < numPages page++) {
  7. $('<span class="page-number">' + (page + 1) + '</span>')
  8. .appendTo($pager).addClass('clickable')
  9. }
  10. $pager.insertBefore($table)
Copyright exforsys.com


 


The number of pages can be found by dividing the number of data rows by the number of items we wish to display on each page. If the division does not yield an integer, we must round the result up using Math.ceil() to ensure that the final partial page will be accessible. Then, with this number in hand, we create buttons for each page and position the new pager before the table:

 

Sponsored Links

 



Read Next: jQuery - Enabling the Pager Buttons



 

 

Comments


chrispie.com said:

  Use instead of the lt() and gt() the slice function the code above isn't working for me.

fix:
var start=currentPage * numPerPage;
var end=(currentPage + 1) * numPerPage;
$table.find('div.teaserBox')
.slice(start, end).fadeIn().end()
.slice(0, start).hide().end()
.slice(end).hide().end()
January 16, 2009, 10:40 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

 
 


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape