Created
November 7, 2016 14:26
-
-
Save iebb/f86597b3290744d38013b28337e6a888 to your computer and use it in GitHub Desktop.
jquery.sorter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getidx(base, idx) {return $(base).children('td').eq(idx).text();} | |
function cmp(a, b, desc) {return desc?a<b:a>b;} | |
$.fn.sorter = function() { | |
$(this).each(function() { | |
var tbl = this; | |
$($(this).find("thead").find("th")).click(function(){ | |
var idx = $(this).index(), order = 0; | |
$(tbl).find("thead").find("th").each(function() {if (idx != $(this).index()) $(this).removeClass("sort-desc").removeClass("sort-asc");}); | |
if ($(this).hasClass("sort-desc")) {order = 1; $(this).removeClass("sort-desc").addClass("sort-asc");} | |
else $(this).removeClass("sort-asc").addClass("sort-desc"); | |
var _ = $(tbl).find("tbody").find("tr"); | |
for(var i=0;i<_.length;i++) for(var j=0;j<i;j++) | |
if (cmp(getidx(_[i], idx), getidx(_[j], idx), order)) { | |
var tmp = $(_[j]).html(); $(_[j]).html($(_[i]).html()); $(_[i]).html(tmp); | |
} | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment