Custom table sort using jquery or Javascript -
i have following table 3 columns (id, date, , source). want sort table based on source column. i don't want sort either alphabetic or numeric. want them sorted rows on sources appear first in table, after rows mb source, , on using either javascript or jquery. searched threads related sorting. failed solve problem. new please me done. appreciated.
<table> <tr> <th>id</th> <th>date</th> <th>source</th> </tr> <tr> <td>50</td> <td>2012-01-01</td> <td>on</td> </tr> <tr> <td>94</td> <td>2013-06-05</td> <td>mb</td> </tr> <tr> <td>80</td> <td>2011-07-08</td> <td>ab</td> </tr> <tr> <td>50</td> <td>2012-01-01</td> <td>on</td> </tr> <tr> <td>50</td> <td>2012-01-01</td> <td>mb</td> </tr> </table>
can make array of order since source known , use array index sort by
var sorters =['on','ab','mb'] var $rows = $('tr:gt(0)').sort(function(a, b){ var asrcidx =sorters.indexof( $(a).find('td:last').text() ); var bsrcidx = sorters.indexof( $(b).find('td:last').text()); return asrcidx > bsrcidx; }); $('table').append($rows);
i left html alone improved data attributes make more efficient
Comments
Post a Comment