-
Notifications
You must be signed in to change notification settings - Fork 2
/
sortChildren.min.js
1 lines (1 loc) · 2.17 KB
/
sortChildren.min.js
1
!function(t){"use strict";t.fn.sortChildren=function(r){var e=t.isPlainObject(r)?t.extend({cmp:function(){return t(this).text()},ignoreFirst:0,ignoreLast:0,reverse:!1},r):{cmp:arguments[0],ignoreFirst:arguments[1],ignoreLast:arguments[2],reverse:arguments[3]};return this.each(function(){var r=t(this),n=t.makeArray(r.children()),i=n.splice(0,e.ignoreFirst||0),s=n.splice(-(e.ignoreLast||0),e.ignoreLast);t.each(n.sort(e.cmp),function(t,n){e.reverse?r.prepend(n):r.append(n)}),r.prepend(i).append(s)})},t.fn.sortTable=function(){return this.each(function(){var r=t("thead th",this).map(function(r){return t.sortKeys.call(0,t(this).data("sort")||[{childAlpha:r}])}).get();t("thead th",this).click(function(){if(!t(this).hasClass("no-sort")){var e=t(this).closest("table").children("tbody"),n=t(this).index();t(this).hasClass("sorted")&&t(this).toggleClass("reversed").siblings().removeClass("sorted reversed"),t(this).addClass("sorted").siblings().removeClass("sorted reversed"),e.sortChildren({cmp:r[n],reverse:t(this).hasClass("reversed")})}}).attr("title","sortable").first().click()})},t.sortKeys=function(r){return t.isArray(r)?t.sortFunc(t.map(r,function(r,e){return t.map(r,function(r,e){return t.sortFunc[e](r)})})):t.sortFunc(t.map(arguments,function(r,e){return r?t.sortFunc[r](e):void 0}))},t.sortFunc=t.extend(function(r,e){e=e||[];var n=r.map(function(r){if(t.isFunction(r))return e.push(""),r;var n=r.split("::"),i={};return(1==n.length?[]:n[1].toLowerCase().split(" ")).map(function(t){i[t]=!0}),e.push(i),new Function("return "+n[0])});return function(t,r){for(var i=0;i<n.length;i++){var s=n[i],c=s.call(t),u=s.call(r);e[i].numeric&&(c=+c,u=+u);var o=e[i].reverse?-1:1;if(c<u)return-1*o;if(c>u)return 1*o}return 0}},{numeric:function(){return+t(this).text()},alpha:function(){return t(this).text()},reverseNumeric:function(){return-+t(this).text()},reverseAlpha:"$(this).text()::reverse",childNumeric:function(r){return function(){return parseFloat(t.trim(t(this).children().eq(r).text()))}},childReverseNumeric:function(r){return function(){return-parseFloat(t.trim(t(this).children().eq(r).text()))}},childAlpha:function(r){return function(){return t.trim(t(this).children().eq(r).text())}}})}(jQuery);