diff --git a/coffee/sortable.coffee b/coffee/sortable.coffee index c8bca86..171251e 100644 --- a/coffee/sortable.coffee +++ b/coffee/sortable.coffee @@ -26,11 +26,11 @@ sortable = table.setAttribute 'data-sortable-initialized', 'true' - ths = table.querySelectorAll('th') - - for th, i in ths - if th.getAttribute('data-sortable') isnt 'false' + i = 0 + for th in table.tHead.rows[0].cells + if th.getAttribute('data-sortable') isnt 'false' and th.tagName is 'TH' sortable.setupClickableTH table, th, i + i += th.colSpan table @@ -84,11 +84,17 @@ sortable = _compare a[0], b[0] for row, position in tBody.rows - value = sortable.getNodeValue(row.cells[i]) - if type.comparator? - value = type.comparator(value) + j = 0 + + for cell in row.cells + if j == i + value = sortable.getNodeValue(cell) + if type.comparator? + value = type.comparator(value) + + rowArray.push [value, row, position] - rowArray.push [value, row, position] + j += cell.colSpan rowArray.sort compare tBody.appendChild row[1] for row in rowArray diff --git a/js/sortable.js b/js/sortable.js index cb3e293..e880cf1 100644 --- a/js/sortable.js +++ b/js/sortable.js @@ -41,7 +41,7 @@ return _results; }, initTable: function(table) { - var i, th, ths, _i, _len, _ref; + var i, th, _i, _len, _ref, _ref1; if (((_ref = table.tHead) != null ? _ref.rows.length : void 0) !== 1) { return; } @@ -49,12 +49,14 @@ return; } table.setAttribute('data-sortable-initialized', 'true'); - ths = table.querySelectorAll('th'); - for (i = _i = 0, _len = ths.length; _i < _len; i = ++_i) { - th = ths[i]; - if (th.getAttribute('data-sortable') !== 'false') { + i = 0; + _ref1 = table.tHead.rows[0].cells; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + th = _ref1[_i]; + if (th.getAttribute('data-sortable') !== 'false' && th.tagName === 'TH') { sortable.setupClickableTH(table, th, i); } + i += th.colSpan; } return table; }, @@ -62,7 +64,7 @@ var eventName, onClick, type, _i, _len, _results; type = sortable.getColumnType(table, i); onClick = function(e) { - var compare, item, newSortedDirection, position, row, rowArray, sorted, sortedDirection, tBody, ths, value, _compare, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _m, _ref, _ref1; + var cell, compare, item, j, newSortedDirection, position, row, rowArray, sorted, sortedDirection, tBody, ths, value, _compare, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _len5, _m, _n, _ref, _ref1, _ref2; if (e.handled !== true) { e.handled = true; } else { @@ -106,26 +108,34 @@ _ref = tBody.rows; for (position = _j = 0, _len1 = _ref.length; _j < _len1; position = ++_j) { row = _ref[position]; - value = sortable.getNodeValue(row.cells[i]); - if (type.comparator != null) { - value = type.comparator(value); + j = 0; + _ref1 = row.cells; + for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) { + cell = _ref1[_k]; + if (j === i) { + value = sortable.getNodeValue(cell); + if (type.comparator != null) { + value = type.comparator(value); + } + rowArray.push([value, row, position]); + } + j += cell.colSpan; } - rowArray.push([value, row, position]); } rowArray.sort(compare); - for (_k = 0, _len2 = rowArray.length; _k < _len2; _k++) { - row = rowArray[_k]; + for (_l = 0, _len3 = rowArray.length; _l < _len3; _l++) { + row = rowArray[_l]; tBody.appendChild(row[1]); } } else { - _ref1 = tBody.rows; - for (_l = 0, _len3 = _ref1.length; _l < _len3; _l++) { - item = _ref1[_l]; + _ref2 = tBody.rows; + for (_m = 0, _len4 = _ref2.length; _m < _len4; _m++) { + item = _ref2[_m]; rowArray.push(item); } rowArray.reverse(); - for (_m = 0, _len4 = rowArray.length; _m < _len4; _m++) { - row = rowArray[_m]; + for (_n = 0, _len5 = rowArray.length; _n < _len5; _n++) { + row = rowArray[_n]; tBody.appendChild(row); } } diff --git a/js/sortable.min.js b/js/sortable.min.js index 8278f50..658ac58 100644 --- a/js/sortable.min.js +++ b/js/sortable.min.js @@ -1,2 +1,2 @@ /*! sortable.js 0.8.0 */ -(function(){var a,b,c,d,e,f,g;a="table[data-sortable]",d=/^-?[£$¤]?[\d,.]+%?$/,g=/^\s+|\s+$/g,c=["click"],f="ontouchstart"in document.documentElement,f&&c.push("touchstart"),b=function(a,b,c){return null!=a.addEventListener?a.addEventListener(b,c,!1):a.attachEvent("on"+b,c)},e={init:function(b){var c,d,f,g,h;for(null==b&&(b={}),null==b.selector&&(b.selector=a),d=document.querySelectorAll(b.selector),h=[],f=0,g=d.length;g>f;f++)c=d[f],h.push(e.initTable(c));return h},initTable:function(a){var b,c,d,f,g,h;if(1===(null!=(h=a.tHead)?h.rows.length:void 0)&&"true"!==a.getAttribute("data-sortable-initialized")){for(a.setAttribute("data-sortable-initialized","true"),d=a.querySelectorAll("th"),b=f=0,g=d.length;g>f;b=++f)c=d[b],"false"!==c.getAttribute("data-sortable")&&e.setupClickableTH(a,c,b);return a}},setupClickableTH:function(a,d,f){var g,h,i,j,k,l;for(i=e.getColumnType(a,f),h=function(b){var c,g,h,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D;if(b.handled===!0)return!1;for(b.handled=!0,m="true"===this.getAttribute("data-sorted"),n=this.getAttribute("data-sorted-direction"),h=m?"ascending"===n?"descending":"ascending":i.defaultSortDirection,p=this.parentNode.querySelectorAll("th"),s=0,w=p.length;w>s;s++)d=p[s],d.setAttribute("data-sorted","false"),d.removeAttribute("data-sorted-direction");if(this.setAttribute("data-sorted","true"),this.setAttribute("data-sorted-direction",h),o=a.tBodies[0],l=[],m){for(D=o.rows,v=0,z=D.length;z>v;v++)g=D[v],l.push(g);for(l.reverse(),B=0,A=l.length;A>B;B++)k=l[B],o.appendChild(k)}else{for(r=null!=i.compare?i.compare:function(a,b){return b-a},c=function(a,b){return a[0]===b[0]?a[2]-b[2]:i.reverse?r(b[0],a[0]):r(a[0],b[0])},C=o.rows,j=t=0,x=C.length;x>t;j=++t)k=C[j],q=e.getNodeValue(k.cells[f]),null!=i.comparator&&(q=i.comparator(q)),l.push([q,k,j]);for(l.sort(c),u=0,y=l.length;y>u;u++)k=l[u],o.appendChild(k[1])}return"function"==typeof window.CustomEvent&&"function"==typeof a.dispatchEvent?a.dispatchEvent(new CustomEvent("Sortable.sorted",{bubbles:!0})):void 0},l=[],j=0,k=c.length;k>j;j++)g=c[j],l.push(b(d,g,h));return l},getColumnType:function(a,b){var c,d,f,g,h,i,j,k,l,m,n;if(d=null!=(l=a.querySelectorAll("th")[b])?l.getAttribute("data-sortable-type"):void 0,null!=d)return e.typesObject[d];for(m=a.tBodies[0].rows,h=0,j=m.length;j>h;h++)for(c=m[h],f=e.getNodeValue(c.cells[b]),n=e.types,i=0,k=n.length;k>i;i++)if(g=n[i],g.match(f))return g;return e.typesObject.alpha},getNodeValue:function(a){var b;return a?(b=a.getAttribute("data-value"),null!==b?b:"undefined"!=typeof a.innerText?a.innerText.replace(g,""):a.textContent.replace(g,"")):""},setupTypes:function(a){var b,c,d,f;for(e.types=a,e.typesObject={},f=[],c=0,d=a.length;d>c;c++)b=a[c],f.push(e.typesObject[b.name]=b);return f}},e.setupTypes([{name:"numeric",defaultSortDirection:"descending",match:function(a){return a.match(d)},comparator:function(a){return parseFloat(a.replace(/[^0-9.-]/g,""),10)||0}},{name:"date",defaultSortDirection:"ascending",reverse:!0,match:function(a){return!isNaN(Date.parse(a))},comparator:function(a){return Date.parse(a)||0}},{name:"alpha",defaultSortDirection:"ascending",match:function(){return!0},compare:function(a,b){return a.localeCompare(b)}}]),setTimeout(e.init,0),"function"==typeof define&&define.amd?define(function(){return e}):"undefined"!=typeof exports?module.exports=e:window.Sortable=e}).call(this); \ No newline at end of file +(function(){var a,b,c,d,e,f,g;a="table[data-sortable]",d=/^-?[£$¤]?[\d,.]+%?$/,g=/^\s+|\s+$/g,c=["click"],f="ontouchstart"in document.documentElement,f&&c.push("touchstart"),b=function(a,b,c){return null!=a.addEventListener?a.addEventListener(b,c,!1):a.attachEvent("on"+b,c)},e={init:function(b){var c,d,f,g,h;for(null==b&&(b={}),null==b.selector&&(b.selector=a),d=document.querySelectorAll(b.selector),h=[],f=0,g=d.length;g>f;f++)c=d[f],h.push(e.initTable(c));return h},initTable:function(a){var b,c,d,f,g,h;if(1===(null!=(g=a.tHead)?g.rows.length:void 0)&&"true"!==a.getAttribute("data-sortable-initialized")){for(a.setAttribute("data-sortable-initialized","true"),b=0,h=a.tHead.rows[0].cells,d=0,f=h.length;f>d;d++)c=h[d],"false"!==c.getAttribute("data-sortable")&&"TH"===c.tagName&&e.setupClickableTH(a,c,b),b+=c.colSpan;return a}},setupClickableTH:function(a,d,f){var g,h,i,j,k,l;for(i=e.getColumnType(a,f),h=function(b){var c,g,h,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I;if(b.handled===!0)return!1;for(b.handled=!0,o="true"===this.getAttribute("data-sorted"),p=this.getAttribute("data-sorted-direction"),k=o?"ascending"===p?"descending":"ascending":i.defaultSortDirection,r=this.parentNode.querySelectorAll("th"),u=0,y=r.length;y>u;u++)d=r[u],d.setAttribute("data-sorted","false"),d.removeAttribute("data-sorted-direction");if(this.setAttribute("data-sorted","true"),this.setAttribute("data-sorted-direction",k),q=a.tBodies[0],n=[],o){for(I=q.rows,E=0,C=I.length;C>E;E++)h=I[E],n.push(h);for(n.reverse(),F=0,D=n.length;D>F;F++)m=n[F],q.appendChild(m)}else{for(t=null!=i.compare?i.compare:function(a,b){return b-a},g=function(a,b){return a[0]===b[0]?a[2]-b[2]:i.reverse?t(b[0],a[0]):t(a[0],b[0])},G=q.rows,l=v=0,z=G.length;z>v;l=++v)for(m=G[l],j=0,H=m.cells,w=0,A=H.length;A>w;w++)c=H[w],j===f&&(s=e.getNodeValue(c),null!=i.comparator&&(s=i.comparator(s)),n.push([s,m,l])),j+=c.colSpan;for(n.sort(g),x=0,B=n.length;B>x;x++)m=n[x],q.appendChild(m[1])}return"function"==typeof window.CustomEvent&&"function"==typeof a.dispatchEvent?a.dispatchEvent(new CustomEvent("Sortable.sorted",{bubbles:!0})):void 0},l=[],j=0,k=c.length;k>j;j++)g=c[j],l.push(b(d,g,h));return l},getColumnType:function(a,b){var c,d,f,g,h,i,j,k,l,m,n;if(d=null!=(l=a.querySelectorAll("th")[b])?l.getAttribute("data-sortable-type"):void 0,null!=d)return e.typesObject[d];for(m=a.tBodies[0].rows,h=0,j=m.length;j>h;h++)for(c=m[h],f=e.getNodeValue(c.cells[b]),n=e.types,i=0,k=n.length;k>i;i++)if(g=n[i],g.match(f))return g;return e.typesObject.alpha},getNodeValue:function(a){var b;return a?(b=a.getAttribute("data-value"),null!==b?b:"undefined"!=typeof a.innerText?a.innerText.replace(g,""):a.textContent.replace(g,"")):""},setupTypes:function(a){var b,c,d,f;for(e.types=a,e.typesObject={},f=[],c=0,d=a.length;d>c;c++)b=a[c],f.push(e.typesObject[b.name]=b);return f}},e.setupTypes([{name:"numeric",defaultSortDirection:"descending",match:function(a){return a.match(d)},comparator:function(a){return parseFloat(a.replace(/[^0-9.-]/g,""),10)||0}},{name:"date",defaultSortDirection:"ascending",reverse:!0,match:function(a){return!isNaN(Date.parse(a))},comparator:function(a){return Date.parse(a)||0}},{name:"alpha",defaultSortDirection:"ascending",match:function(){return!0},compare:function(a,b){return a.localeCompare(b)}}]),setTimeout(e.init,0),"function"==typeof define&&define.amd?define(function(){return e}):"undefined"!=typeof exports?module.exports=e:window.Sortable=e}).call(this); \ No newline at end of file