Exforsys.com
 

Sponsored Links

 

jQuery Tutorials

 
Home Tutorials jQuery
 

jQuery - Marking the Current Page

 

Learning jQuery - Marking the Current Page

Our pager can be made more user-friendly by highlighting the current page number. We just need to update the classes on the buttons every time one is clicked:



Sample Code
  1. var $pager = $('</p><br>
  2. <div class="pager"></div>
  3. ')
  4. for (var page = 0 page < numPages page++) {
  5. $('<span class="page-number">' + (page + 1) + '</span>')
  6. .bind('click', {'newPage': page}, function(event) {
  7. currentPage = event.data['newPage']
  8. repaginate()
  9. $(this).addClass('active').siblings().removeClass('active')
  10. })
  11. .appendTo($pager).addClass('clickable')
  12. }
  13. $pager.find('span.page-number:first').addClass('active')
  14. $pager.insertBefore($table)
Copyright exforsys.com




Now we have an indicator of the current status of the pager:

 



Paging with Sorting

We began this discussion by noting that sorting and paging controls needed to be aware of one another to avoid confusing results. Now that we have a working pager, we need to make sort operations respect the current page selection.


Doing this is as simple as calling our repaginate() function whenever a sort is performed. The scope of the function, though, makes this problematic. We can't reach repaginate() from our sorting routine because it is contained inside a different $(document).ready() handler. We could just consolidate the two pieces of code, but instead let's be a bit sneakier. We can decouple the behaviors, so that a sort calls the repaginate behavior if it exists, but ignores it otherwise. To accomplish this, we'll use a handler for a custom event.


In our earlier event handling discussion, we limited ourselves to event names that were triggered by the web browser, such as click and mouseup. The .bind() and .trigger() methods are not limited to these events, though; we can use any string as an event name. In this case, we can define a new event called repaginate as a stand-in for the function we've been calling:


Sample Code
  1. $table.bind('repaginate', function() {
  2. $table.find('tbody tr').show()
  3. .lt(currentPage * numPerPage)
  4. .hide()
  5. .end()
  6. .gt((currentPage + 1) * numPerPage - 1)
  7. .hide()
  8. .end()
  9. })
Copyright exforsys.com



Now in places where we were calling repaginate(), we can call:


Sample Code
  1. $table.trigger('repaginate')
Copyright exforsys.com



We can issue this call in our sort code as well. It will do nothing if the table does not have a pager, so we can mix and match the two capabilities as desired.



Read Next: jQuery Completed sorting and paging code



 

 

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