alt
Advertisement
Sponsored links
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow jQuery arrow jQuery - Enabling the Pager Buttons
Site Search


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:

  1. $(document).ready(function() {
  2.  
  3. $('table.paginated').each(function() {
  4.  
  5. var currentPage = 0;
  6.  
  7. var numPerPage = 10;
  8.  
  9. var $table = $(this);
  10.  
  11. var repaginate = function() {
  12.  
  13. $table.find('tbody tr').show()
  14.  
  15. .lt(currentPage * numPerPage)
  16.  
  17. .hide()
  18.  
  19. .end()
  20.  
  21. .gt((currentPage + 1) * numPerPage - 1)
  22.  
  23. .hide()
  24.  
  25. .end();
  26.  
  27. };
  28.  
  29. var numRows = $table.find('tbody tr').length;
  30.  
  31. var numPages = Math.ceil(numRows / numPerPage);
  32.  
  33. var $pager = $('</p>
  34. <div class="pager"></div>
  35. ');
  36.  
  37. for (var page = 0; page < numPages; page++) {
  38.  
  39. $('<span class="page-number">' + (page + 1) + '</span>')
  40.  
  41. .click(function() {
  42.  
  43. currentPage = page;
  44.  
  45. repaginate();
  46.  
  47. })
  48.  
  49. .appendTo($pager).addClass('clickable');
  50.  
  51. }
  52.  
  53. $pager.insertBefore($table);
  54.  
  55. repaginate();
  56.  
  57. });
  58.  
  59. });
 

 

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:

  1. $('<span class="page-number">' + (page + 1) + '</span>')
  2.  
  3. .bind('click', {'newPage': page}, function(event) {
  4.  
  5. currentPage = event.data['newPage'];
  6.  
  7. repaginate();
  8.  
  9. })
  10.  
  11. .appendTo($pager).addClass('clickable');
 

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:


Trackback(0)
Comments (0)add comment

Write comment

busy
 
< Prev   Next >
Exforsys Offers
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape