Exforsys.com
 

Sponsored Links

 

jQuery Tutorials

 
Home Tutorials jQuery
 

jQuery - Enabling the Pager Buttons

 

Learning jQuery - Enabling the Pager Buttons

To make these new buttons actually work, we need to update the currentPage variable and then run our pagination routine. At first blush, it seems we should be able to do this by setting currentPage to page, which is the current value of the iterator that creates the buttons:



Sample Code
  1. $(document).ready(function() {
  2. $('table.paginated').each(function() {
  3. var currentPage = 0
  4. var numPerPage = 10
  5. var $table = $(this)
  6. var repaginate = function() {
  7. $table.find('tbody tr').show()
  8. .lt(currentPage * numPerPage)
  9. .hide()
  10. .end()
  11. .gt((currentPage + 1) * numPerPage - 1)
  12. .hide()
  13. .end()
  14. }
  15. var numRows = $table.find('tbody tr').length
  16. var numPages = Math.ceil(numRows / numPerPage)
  17. var $pager = $('</p><br>
  18. <div class="pager"></div>
  19. ')
  20. for (var page = 0 page < numPages page++) {
  21. $('<span class="page-number">' + (page + 1) + '</span>')
  22. .click(function() {
  23. currentPage = page
  24. repaginate()
  25. })
  26. .appendTo($pager).addClass('clickable')
  27. }
  28. $pager.insertBefore($table)
  29. repaginate()
  30. })
  31. })
Copyright exforsys.com


 


This mostly works. The new repaginate() function is called when the page loads and when any button is clicked. All of the buttons take us to a page with no rows on it, though:




The problem is that in defining our click handler, we have created a closure. The click handler refers to the page variable, which is defined outside the function. When the variable changes the next time through the loop, this affects the click handlers that we have already set up for the earlier buttons. The net effect is that, for a pager with 7 pages, each button directs us to page 8 (the final value of page). More information on how closures work can be found in Appendix C, JavaScript Closures.


To correct this problem, we'll take advantage of one of the more advanced features of jQuery's event binding methods. We can add a set of data to the handler when we bind it that will still be available when the handler is eventually called. With this capability in our bag of tricks, we can write:

Sample Code
  1. $('<span class="page-number">' + (page + 1) + '</span>')
  2. .bind('click', {'newPage': page}, function(event) {
  3. currentPage = event.data['newPage']
  4. repaginate()
  5. })
  6. .appendTo($pager).addClass('clickable')
Copyright exforsys.com



The new page number is passed into the handler by way of the event's data property. In this way the page number escapes the closure, and is frozen in time at the value it contained when the handler was bound. Now our pager buttons can correctly take us to different pages:




Read Next: jQuery - Marking the Current Page



 

 

Comments



Post Your Comment:

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

Sponsored Links

 

Subscribe via RSS


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