Exforsys.com
 

Sponsored Links

 

jQuery Tutorials

 
Home Tutorials jQuery
 

jQuery - Column Highlighting

 

Learning jQuery - Column Highlighting

It can be a nice user interface enhancement to visually remind the user of what has been done in the past. By highlighting the column that was most recently used for sorting, we can focus the user's attention on the part of the table that is most likely to be relevant. Fortunately, since we've already determined how to select the table cells in the column, applying a class to those cells is simple:



Sample Code
  1. $table.find('td').removeClass('sorted')
  2. .filter(':nth-child(' + (column + 1) + ')').addClass('sorted')
Copyright exforsys.com



Note that we have to add one to the column index we found earlier, since the :nth-child() selector is one-based rather than zero-based. With this code in place, we get a highlighted column after any sort operation:



Alternating Sort Directions

Our final sorting enhancement is to allow for both ascending and descending sort orders. When the user clicks on a column that is already sorted, we want to reverse the current sort order.


To reverse a sort, all we have to do is to invert the values returned by our comparator. We can do this with a simple variable:


Sample Code
  1. if (a.sortKey < b.sortKey) return -newDirection
  2. if (a.sortKey > b.sortKey) return newDirection
Copyright exforsys.com




If newDirection equals 1, then the sort will be the same as before. If it equals -1, the sort will be reversed. We can use classes to keep track of the current sort order of a column:


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. $('th', $table).each(function(column) {
  15. var findSortKey
  16. if ($(this).is('.sort-alpha')) {
  17. findSortKey = function($cell) {
  18. return $cell.find('.sort-key').text().toUpperCase() + ' ' +
  19. $cell.text().toUpperCase()
  20. }
  21. }
  22. else if ($(this).is('.sort-numeric')) {
  23. findSortKey = function($cell) {
  24. var key = parseFloat($cell.text().replace(/^[^\d.]*/, ''))
  25. return isNaN(key) ? 0 : key
  26. }
  27. }
  28. else if ($(this).is('.sort-date')) {
  29. findSortKey = function($cell) {
  30. return Date.parse('1 ' + $cell.text())
  31. }
  32. }
  33. if (findSortKey) {
  34. $(this).addClass('clickable').hover(function() {
  35. $(this).addClass('hover')
  36. }, function() {
  37. $(this).removeClass('hover')
  38. }).click(function() {
  39. +var newDirection = 1
  40. if ($(this).is('.sorted-asc')) {
  41. newDirection = -1
  42. }
  43. var rows = $table.find('tbody > tr').get()
  44. $.each(rows, function(index, row) {
  45. row.sortKey =
  46. findSortKey($(row).children('td').eq(column))
  47. })
  48. rows.sort(function(a, b) {
  49. if (a.sortKey < b.sortKey) return -newDirection
  50. if (a.sortKey > b.sortKey) return newDirection
  51. return 0
  52. })
  53. $.each(rows, function(index, row) {
  54. $table.children('tbody').append(row)
  55. row.sortKey = null
  56. })
  57. $table.find('th').removeClass('sorted-asc')
  58. .removeClass('sorted-desc')
  59. var $sortHead = $table.find('th').filter('
  60. :nth-child(' + (column + 1) + ')')
  61. if (newDirection == 1) {
  62. $sortHead.addClass('sorted-asc')
  63. } else {
  64. $sortHead.addClass('sorted-desc')
  65. }
  66. $table.find('td').removeClass('sorted')
  67. .filter(':nth-child(' + (column + 1) + ')')
  68. .addClass('sorted')
  69. $table.alternateRowColors($table)
  70. })
  71. }
  72. })
  73. })
  74. })
Copyright exforsys.com





As a side benefit, since we use classes to store the sort direction we can style the columns headers to indicate the current order as well:




Read Next: jQuery Pagination



 

 

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 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape