mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-12-05 05:04:20 +00:00
6 lines
3.5 KiB
JavaScript
6 lines
3.5 KiB
JavaScript
/*! tablesorter tbody sorting widget (BETA) - 11/26/2016 (v2.28.0)
|
|
* Requires tablesorter v2.22.2+ and jQuery 1.4+
|
|
* by Rob Garrison
|
|
* Contributors: Chris Rogers
|
|
*/
|
|
!function(t){"use strict";var o=t.tablesorter;o.sortTbodies={init:function(r,e){var s,n,i,d,a,b=r.namespace+"sortTbody",l=r.$table.children("tbody"),c=l.length;for(e.sortTbody_original_serverSideSorting=r.serverSideSorting,e.sortTbody_original_cssInfoBlock=r.cssInfoBlock,r.cssInfoBlock=e.sortTbody_noSort,o.sortTbodies.setTbodies(r,e),s=0;s<c;s++)l.eq(s).attr("data-ts-original-order",s);for(r.$table.unbind("sortBegin updateComplete ".split(" ").join(b+" ")).bind("sortBegin"+b,function(){o.sortTbodies.sorter(r)}).bind("updateComplete"+b,function(){o.sortTbodies.setTbodies(r,e),o.updateCache(r,null,r.$tbodies)}).bind("sortEnd",function(){var o=e.sortTbody_primaryRow;e.sortTbody_lockHead&&o&&r.$table.find(o).each(function(){t(this).parents("tbody").prepend(this)})}),(t.isEmptyObject(r.parsers)||r.$tbodies.length!==l.length)&&(o.sortTbodies.setTbodies(r,e),o.updateCache(r,null,r.$tbodies)),c=(a=l.children("tr")).length,s=0;s<r.columns;s++){if(d=0,"numeric"===r.parsers[s].type)for(n=0;n<c;n++)i=o.getParsedText(r,a.eq(n).children()[s],s),d=Math.max(Math.abs(i)||0,d);r.$headerIndexed[s].attr("data-ts-col-max-value",d)}},setTbodies:function(t,o){t.$tbodies=t.$table.children("tbody").not("."+o.sortTbody_noSort)},sorter:function(r){var e=r.$table,s=r.widgetOptions;if(!0!==s.sortTbody_busy){s.sortTbody_busy=!0;var n=e.children("tbody").not("."+s.sortTbody_noSort),i=s.sortTbody_primaryRow||"tr:eq(0)",d=r.sortList||[],a=d.length;a&&(r.serverSideSorting=!s.sortTbody_sortRows,n.sort(function(e,s){var n,b,l,c,y,T,p,f,u,g,h,_,m=r.table,S=r.parsers,v=r.textSorter||"",$=t(e),x=t(s),q=$.find(i).children("td, th"),B=x.find(i).children("td, th");for(n=0;n<a;n++){if(p=d[n][0],f=d[n][1],l=0===f,b=o.getElementText(r,q.eq(p),p),u=S[p].format(b,m,q[p],p),b=o.getElementText(r,B.eq(p),p),g=S[p].format(b,m,B[p],p),r.sortStable&&u===g&&1===a)return $.attr("data-ts-original-order")-x.attr("data-ts-original-order");if(c=/n/i.test(S&&S[p]?S[p].type||"":""),c&&r.strings[p]?(y=r.$headerIndexed[p].attr("data-ts-col-max-value")||1.79e308,c="boolean"==typeof o.string[r.strings[p]]?(l?1:-1)*(o.string[r.strings[p]]?-1:1):r.strings[p]?o.string[r.strings[p]]||0:0,T=r.numberSorter?r.numberSorter(u,g,l,y,m):o["sortNumeric"+(l?"Asc":"Desc")](u,g,c,y,p,r)):(h=l?u:g,_=l?g:u,T="function"==typeof v?v(h,_,l,p,m):"object"==typeof v&&v.hasOwnProperty(p)?v[p](h,_,l,p,m):o["sortNatural"+(l?"Asc":"Desc")](u,g,p,r)),T)return T}return $.attr("data-ts-original-order")-x.attr("data-ts-original-order")}),o.sortTbodies.restoreTbodies(r,s,n),s.sortTbody_busy=!1)}},restoreTbodies:function(t,o,r){var e,s,n,i,d,a,b,l=t.$table,c=!0,y=0;if(l.hide(),r.appendTo(l),s=l.children("tbody"),i=s.length,e=s.filter("."+o.sortTbody_noSort).appendTo(l),d=e.length)for(;c&&y<d;){for(c=!1,a=0;a<d;a++)(b=(b=parseInt(e.eq(a).attr("data-ts-original-order"),10))>=i?i:b<0?0:b)!==e.eq(a).index()&&(c=!0,n=e.eq(a).detach(),b>=i?n.appendTo(l):0===b?n.prependTo(l):n.insertBefore(l.children("tbody:eq("+b+")")));y++}l.show()}},o.addWidget({id:"sortTbody",priority:40,options:{sortTbody_lockHead:!1,sortTbody_primaryRow:null,sortTbody_sortRows:!1,sortTbody_noSort:"tablesorter-no-sort-tbody"},init:function(t,r,e,s){o.sortTbodies.init(e,s)},remove:function(t,o,r){o.$table.unbind("sortBegin updateComplete ".split(" ").join(o.namespace+"sortTbody ")),o.serverSideSorting=r.sortTbody_original_serverSideSorting,o.cssInfoBlock=r.sortTbody_original_cssInfoBlock}})}(jQuery); |