alt
Advertisement
Sponsored links
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow jQuery arrow jQuery - Sorting Other Types of Data
Site Search


jQuery - Sorting Other Types of Data

Learning jQuery - Sorting Other Types of Data

Our sort routine should be able to handle not just the Title and Author columns, but the Publish Dates and Price as well. Since we streamlined our comparator function, it can handle all kinds of data, but the computed keys will need to be adjusted for other data types. For example, in the case of prices we need to strip off the leading $ character and parse the rest, then compare them:

  1. var key = parseFloat($cell.text().replace(/^[^\d.]*/, ''));
  2.  
  3. row.sortKey = isNaN(key) ? 0 : key;
 

The result of parseFloat() needs to be checked, because if no number can be extracted from the text, NaN is returned, which can wreak havoc on .sort(). For the date cells, we can use the JavaScript Date object:

  1. row.sortKey = Date.parse('1 ' + $cell.text());
 

The dates in this table contain a month and year only; Date.parse() requires a fully‑specified date, so we prepend the string with 1. This provides a day to complement the month and year, and the combination is then converted into a timestamp, which can be sorted using our normal comparator.

We can apportion these expressions across separate functions, and call the appropriate one based on the class applied to the table header:

  1. $.fn.alternateRowColors = function() {
  2.  
  3. $('tbody tr:odd', this).removeClass('even').addClass('odd');
  4.  
  5. $('tbody tr:even', this).removeClass('odd').addClass('even');
  6.  
  7. return this;
  8.  
  9. };
  10.  
  11. $(document).ready(function() {
  12.  
  13. var alternateRowColors = function($table) {
  14.  
  15. $('tbody tr:odd', $table).removeClass('even').addClass('odd');
  16.  
  17. $('tbody tr:even', $table).removeClass('odd').addClass('even');
  18.  
  19. };
  20.  
  21. $('table.sortable').each(function() {
  22.  
  23. var $table = $(this);
  24.  
  25. $table.alternateRowColors($table);
  26.  
  27. $('th', $table).each(function(column) {
  28.  
  29. var findSortKey;
  30.  
  31. if ($(this).is('.sort-alpha')) {
  32.  
  33. findSortKey = function($cell) {
  34.  
  35. return $cell.find('.sort-key').text().toUpperCase()
  36.  
  37. + ' ' + $cell.text().toUpperCase();
  38.  
  39. };
  40.  
  41. }
  42.  
  43. else if ($(this).is('.sort-numeric')) {
  44.  
  45. findSortKey = function($cell) {
  46.  
  47. var key = parseFloat($cell.text().replace(/^[^\d.]*/, ''));
  48.  
  49. return isNaN(key) ? 0 : key;
  50.  
  51. };
  52.  
  53. }
  54.  
  55. else if ($(this).is('.sort-date')) {
  56.  
  57. findSortKey = function($cell) {
  58.  
  59. return Date.parse('1 ' + $cell.text());
  60.  
  61. };
  62.  
  63. }
  64.  
  65. if (findSortKey) {
  66.  
  67. $(this).addClass('clickable').hover(function() {
  68.  
  69. $(this).addClass('hover');
  70.  
  71. }, function() {
  72.  
  73. $(this).removeClass('hover');
  74.  
  75. }).click(function() {
  76.  
  77. var rows = $table.find('tbody > tr').get();
  78.  
  79. $.each(rows, function(index, row) {
  80.  
  81. row.sortKey =
  82.  
  83. findSortKey($(row).children('td').eq(column));
  84.  
  85. });
  86.  
  87. rows.sort(function(a, b) {
  88.  
  89. if (a.sortKey < b.sortKey) return -1;
  90.  
  91. if (a.sortKey > b.sortKey) return 1;
  92.  
  93. return 0;
  94.  
  95. });
  96.  
  97. $.each(rows, function(index, row) {
  98.  
  99. $table.children('tbody').append(row);
  100.  
  101. row.sortKey = null;
  102.  
  103. });
  104.  
  105. $table.alternateRowColors($table);
  106.  
  107. });
  108.  
  109. }
  110.  
  111. });
  112.  
  113. });
  114.  
  115. });
 

The findSortKey variable doubles as the function to calculate the key and a flag to indicate whether the column header is marked with a class making it sortable. We can now sort on date or price:


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