NOTE! Assigning the parser using a class name was added in version 2.0.11 (it is not part of the original plugin).
Demo
Name |
Major |
Gender |
English |
Japanese |
Calculus |
Overall grades |
---|---|---|---|---|---|---|
Student01 | Languages | male | 80 | 70 | 75 | medium |
Student02 | Mathematics | male | 90 | 88 | 100 | good |
Student03 | Languages | female | 85 | 95 | 80 | good |
Student04 | Languages | male | 20 | 50 | 65 | bad |
Student05 | Mathematics | female | 70 | 78 | 70 | medium |
Student06 | Mathematics | male | 44 | 65 | 60 | bad |
Javascript
// add parser through the tablesorter addParser method $.tablesorter.addParser({ // set a unique id id: 'grades', is: function(s) { // return false so this parser is not auto detected return false; }, format: function(s, table, cell, cellIndex) { // format your data for normalization return s.toLowerCase() .replace(/good/,2) .replace(/medium/,1) .replace(/bad/,0); }, // set type, either numeric or text type: 'numeric' }); $(function() { $("table").tablesorter({ theme: 'blue' // "sorter-grades" added as a class name in the HTML (new in v2.0.11) // or un-comment out the option below // ,headers: { 6: { sorter: 'grades' } } }); });
HTML
<table class="tablesorter"> <thead> <tr> <th class="sorter-text">Name</th> <th>Major</th> <th>Gender</th> <th>English</th> <th>Japanese</th> <th>Calculus</th> <th class="sorter-grades">Overall grades</th> <!-- set the column parser using a class name --> </tr> </thead> <tbody> <tr> <td>Student01</td> <td>Languages</td> <td>male</td> <td>80</td> <td>70</td> <td>75</td> <td>medium</td> </tr> <tr> <td>Student02</td> <td>Mathematics</td> <td>male</td> <td>90</td> <td>88</td> <td>100</td> <td>good</td> </tr> <tr> <td>Student03</td> <td>Languages</td> <td>female</td> <td>85</td> <td>95</td> <td>80</td> <td>good</td> </tr> <tr> <td>Student04</td> <td>Languages</td> <td>male</td> <td>20</td> <td>50</td> <td>65</td> <td>bad</td> </tr> <tr> <td>Student05</td> <td>Mathematics</td> <td>female</td> <td>70</td> <td>78</td> <td>70</td> <td>medium</td> </tr> <tr> <td>Student06</td> <td>Mathematics</td> <td>male</td> <td>44</td> <td>65</td> <td>60</td> <td>bad</td> </tr> </tbody> </table>