alt
Sponsored links
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow jQuery arrow jQuery - Finessing the Sort Key
Site Search


jQuery - Finessing the Sort Key

Learning jQuery - Finessing the Sort Keys

Now we want to apply the same kind of sorting behavior to the Author(s) column of our table. By adding the sort-alpha class to its table header cell, the Author(s) column can be sorted with our existing code. But ideally authors should be sorted by last name, not first. Since some books have multiple authors, and some authors have middle names or initials listed, we need outside guidance to determine what part of the text to use as our sort key. We can supply this guidance by wrapping the relevant part of the cell in a tag:

  1. <tr>
  2. </tr>
  3.  
  4. <td> </td>
  5.  
  6. Building Websites with Joomla! 1.5 Beta 1
  7. <td> </td>
  8. <td> </td>
  9. Hagen <span class="sort-key">Graf</span>
  10. <td> </td>
  11. Feb 2007
  12. <td> </td>
  13. $40.49
  14.  
  15. <tr>
  16. </tr>
  17.  
  18. <td> </td>
  19.  
  20. Learning Mambo: A Step-by-Step Tutorial to Building Your Website
  21.  
  22. <td> </td>
  23. Douglas <span class="sort-key">Paterson</span>
  24. <td> </td>
  25. Dec 2006
  26. <td> </td>
  27. $40.49
  28.  
  29. <tr>
  30. </tr>
  31.  
  32. <td> </td>
  33.  
  34. <td> </td>
  35. Moodle E-Learning Course Development
  36. <td> </td>
  37. William <span class="sort-key">Rice</span>
  38. <td> </td>
  39. May 2006
  40. <td> </td>
  41. $35.99
  42.  
 

Now we have to modify our sorting code to take this tag into account, without disturbing the existing behavior for the Title column, which is working well. By prepending the marked sort key to the key we have previously calculated, we can sort first on the last name if it is called out, but on the whole string as a fallback:

  1. $.each(rows, function(index, row) {
  2.  
  3. var $cell = $(row).children('td').eq(column);
  4.  
  5. row.sortKey = $cell.find('.sort-key').text().toUpperCase()
  6.  
  7. + ' ' + $cell.text().toUpperCase();
  8.  
  9. });
 

Sorting by the Author(s) column now uses the last name:

If two last names are identical, the sort uses the entire string as a tiebreaker for positioning.


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