Exforsys.com
 

Sponsored Links

 

jQuery Tutorials

 
Home Tutorials jQuery
 

jQuery Completed sorting and paging code

 

Learning jQuery - The Finished Code

The completed sorting and paging code in its entirety follows:



Sample Code
  1.  $.fn.alternateRowColors = function() {
  2. $('tbody tr:odd', this).removeClass('even').addClass('odd')
  3. $('tbody tr:even', this).removeClass('odd').addClass('even')
  4. return this
  5. }
  6. $(document).ready(function() {
  7. var alternateRowColors = function($table) {
  8. $('tbody tr:odd', $table).removeClass('even').addClass('odd')
  9. $('tbody tr:even', $table).removeClass('odd').addClass('even')
  10. }
  11. $('table.sortable').each(function() {
  12. var $table = $(this)
  13. $table.alternateRowColors($table)
  14. $table.find('th').each(function(column) {
  15. var findSortKey
  16. if ($(this).is('.sort-alpha')) {
  17. findSortKey = function($cell) {
  18. return $cell.find('.sort-key').text().toUpperCase() + ' ' + $cell.text().toUpperCase()
  19. }
  20. }
  21. else if ($(this).is('.sort-numeric')) {
  22. findSortKey = function($cell) {
  23. var key = parseFloat($cell.text().replace(/^[^\d.]*/, ''))
  24. return isNaN(key) ? 0 : key
  25. }
  26. }
  27. else if ($(this).is('.sort-date')) {
  28. findSortKey = function($cell) {
  29. return Date.parse('1 ' + $cell.text())
  30. }
  31. }
  32. if (findSortKey) {
  33. $(this).addClass('clickable').hover(function() {
  34. $(this).addClass('hover')
  35. }, function() {
  36. $(this).removeClass('hover')
  37. }).click(function() {
  38. var newDirection = 1
  39. if ($(this).is('.sorted-asc')) {
  40. newDirection = -1
  41. }
  42. rows = $table.find('tbody > tr').get()
  43. $.each(rows, function(index, row) {
  44. row.sortKey =
  45. findSortKey($(row).children('td').eq(column))
  46. })
  47. rows.sort(function(a, b) {
  48. if (a.sortKey < b.sortKey) return -newDirection
  49. if (a.sortKey > b.sortKey) return newDirection
  50. return 0
  51. })
  52. $.each(rows, function(index, row) {
  53. $table.children('tbody').append(row)
  54. row.sortKey = null
  55. })
  56. $table.find('th').removeClass('sorted-asc') .removeClass('sorted-desc')
  57. var $sortHead = $table.find('th').filter(':nth-child(' + (column + 1) + ')')
  58. if (newDirection == 1) {
  59. $sortHead.addClass('sorted-asc')
  60. } else {
  61. $sortHead.addClass('sorted-desc')
  62. }
  63. $table.find('td').removeClass('sorted')
  64. .filter(':nth-child(' + (column + 1) + ')')
  65. .addClass('sorted')
  66. $table.alternateRowColors($table)
  67. $table.trigger('repaginate')
  68. })
  69. }
  70. })
  71. })
  72. })
  73. $(document).ready(function() {
  74. $('table.paginated').each(function() {
  75. var currentPage = 0
  76. var numPerPage = 10
  77. var $table = $(this)
  78. $table.bind('repaginate', function() {
  79. $table.find('tbody tr').show()
  80. .lt(currentPage * numPerPage)
  81. .hide()
  82. .end()
  83. .gt((currentPage + 1) * numPerPage - 1)
  84. .hide()
  85. .end()
  86. })
  87. var numRows = $table.find('tbody tr').length
  88. var numPages = Math.ceil(numRows / numPerPage)
  89. var $pager = $('</p><br>
  90. <div class="pager"></div>
  91. ')
  92. for (var page = 0 page < numPages page++) {
  93. $('<span class="page-number">' + (page + 1) + '</span>')
  94. .bind('click', {'newPage': page}, function(event) {
  95. currentPage = event.data['newPage']
  96. $table.trigger('repaginate')
  97. $(this).addClass('active').siblings().removeClass('active')
  98. })
  99. .appendTo($pager).addClass('clickable')
  100. }
  101. $pager.find('span.page-number:first').addClass('active')
  102. $pager.insertBefore($table)
  103. $table.trigger('repaginate')
  104. })
  105. })
Copyright exforsys.com


 




Read Next: jQuery Advanced Row Striping



 

 

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