you are here: codestackercodes [RSS] → tag table [RSS]

simple jquery table sort

requires jquery.sortElements.js (https://github.com/jamespadolsey/jQuery-Plugins/blob/master/sortElements/jquery.sortElements.js)

via http://stackoverflow.com/questions/3160277/jquery-table-sort

var table = $('table');

$('th').wrapInner('<span title="Sort this column"/>').each(function(){
     var th = $(this), thIndex = th.index(), inverse = false;
     th.click(function(){
         table.find('td').filter(function(){
             return $(this).index() === thIndex;
         }).sortElements(function(a, b){
             return $.text([a]) > $.text([b]) ?
                 inverse ? -1 : 1
                 : inverse ? 1 : -1;
         }, function(){
			return this.parentNode; 
         });
         inverse = !inverse;
     });
});
Displaying 1 Code