Comparing version 3.1.0 to 3.2.0
@@ -0,1 +1,5 @@ | ||
### v3.2.0 | ||
- __Addition:__ Option to sort on a particular column [Ref](https://github.com/tristen/tablesort/pull/97). | ||
### v3.1.0 | ||
@@ -2,0 +6,0 @@ |
{ | ||
"name": "tablesort", | ||
"description": "A sorting component for HTML tables", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"author": "tristen", | ||
@@ -6,0 +6,0 @@ "ender": "./ender.js", |
@@ -14,3 +14,3 @@ tablesort | ||
<!-- Include sort types you need --> | ||
<script src='tablesort.numeric.js'></script> | ||
<script src='tablesort.number.js'></script> | ||
<script src='tablesort.date.js'></script> | ||
@@ -39,2 +39,10 @@ | ||
To override the sort order of a particular column, a `data-sort-order` | ||
attribute can be added to its `th` element. Accepted values are `asc` for | ||
ascending and `desc` for descending. | ||
``` html | ||
<th data-sort-order='desc'>Name</th> | ||
``` | ||
#### Exclude columns or rows | ||
@@ -41,0 +49,0 @@ For columns or rows that do not require sorting, you can add a class of |
@@ -131,3 +131,4 @@ ;(function() { | ||
sortDir, | ||
sortMethod = header.getAttribute('data-sort-method'); | ||
sortMethod = header.getAttribute('data-sort-method'), | ||
sortOrder = header.getAttribute('data-sort-order'); | ||
@@ -144,2 +145,6 @@ that.table.dispatchEvent(createEvent('beforeSort')); | ||
sortDir = 'sort-up'; | ||
} else if (sortOrder === 'asc') { | ||
sortDir = 'sort-down'; | ||
} else if (sortOrder === 'desc') { | ||
sortDir = 'sort-up'; | ||
} else { | ||
@@ -146,0 +151,0 @@ sortDir = that.options.descending ? 'sort-up' : 'sort-down'; |
/*! | ||
* tablesort v3.1.0 (2015-07-03) | ||
* tablesort v3.2.0 (2015-12-11) | ||
* http://tristen.ca/tablesort/demo/ | ||
* Copyright (c) 2015 ; Licensed MIT | ||
*/!function(){function a(b,c){if(!(this instanceof a))return new a(b,c);if(!b||"TABLE"!==b.tagName)throw new Error("Element must be a table");this.init(b,c||{})}var b=[],c=function(a){var b;return window.CustomEvent&&"function"==typeof window.CustomEvent?b=new CustomEvent(a):(b=document.createEvent("CustomEvent"),b.initCustomEvent(a,!1,!1,void 0)),b},d=function(a){return a.getAttribute("data-sort")||a.textContent||a.innerText||""},e=function(a,b){return a=a.toLowerCase(),b=b.toLowerCase(),a===b?0:b>a?1:-1},f=function(a,b){return function(c,d){var e=a(c.td,d.td);return 0===e?b?d.index-c.index:c.index-d.index:e}};a.extend=function(a,c,d){if("function"!=typeof c||"function"!=typeof d)throw new Error("Pattern and sort must be a function");b.push({name:a,pattern:c,sort:d})},a.prototype={init:function(a,b){var c,d,e,f,g=this;if(g.table=a,g.thead=!1,g.options=b,a.rows&&a.rows.length>0&&(a.tHead&&a.tHead.rows.length>0?(c=a.tHead.rows[a.tHead.rows.length-1],g.thead=!0):c=a.rows[0]),c){var h=function(){g.current&&g.current!==this&&(g.current.classList.remove("sort-up"),g.current.classList.remove("sort-down")),g.current=this,g.sortTable(this)};for(e=0;e<c.cells.length;e++)f=c.cells[e],f.classList.contains("no-sort")||(f.classList.add("sort-header"),f.tabindex=0,f.addEventListener("click",h,!1),f.classList.contains("sort-default")&&(d=f));d&&(g.current=d,g.sortTable(d))}},sortTable:function(a,g){var h,i=this,j=a.cellIndex,k=e,l="",m=[],n=i.thead?0:1,o=a.getAttribute("data-sort-method");if(i.table.dispatchEvent(c("beforeSort")),g?h=a.classList.contains("sort-up")?"sort-up":"sort-down":(h=a.classList.contains("sort-up")?"sort-down":a.classList.contains("sort-down")?"sort-up":i.options.descending?"sort-up":"sort-down",a.classList.remove("sort-down"===h?"sort-up":"sort-down"),a.classList.add(h)),!(i.table.rows.length<2)){if(!o){for(;m.length<3&&n<i.table.tBodies[0].rows.length;)l=d(i.table.tBodies[0].rows[n].cells[j]),l=l.trim(),l.length>0&&m.push(l),n++;if(!m)return}for(n=0;n<b.length;n++)if(l=b[n],o){if(l.name===o){k=l.sort;break}}else if(m.every(l.pattern)){k=l.sort;break}i.col=j;var p,q=[],r={},s=0,t=0;for(n=0;n<i.table.tBodies.length;n++)for(p=0;p<i.table.tBodies[n].rows.length;p++)l=i.table.tBodies[n].rows[p],l.classList.contains("no-sort")?r[s]=l:q.push({tr:l,td:d(l.cells[i.col]),index:s}),s++;for("sort-down"===h?(q.sort(f(k,!0)),q.reverse()):q.sort(f(k,!1)),n=0;s>n;n++)r[n]?(l=r[n],t++):l=q[n-t].tr,i.table.tBodies[0].appendChild(l);i.table.dispatchEvent(c("afterSort"))}},refresh:function(){void 0!==this.current&&this.sortTable(this.current,!0)}},"undefined"!=typeof module&&module.exports?module.exports=a:window.Tablesort=a}(); | ||
*/!function(){function a(b,c){if(!(this instanceof a))return new a(b,c);if(!b||"TABLE"!==b.tagName)throw new Error("Element must be a table");this.init(b,c||{})}var b=[],c=function(a){var b;return window.CustomEvent&&"function"==typeof window.CustomEvent?b=new CustomEvent(a):(b=document.createEvent("CustomEvent"),b.initCustomEvent(a,!1,!1,void 0)),b},d=function(a){return a.getAttribute("data-sort")||a.textContent||a.innerText||""},e=function(a,b){return a=a.toLowerCase(),b=b.toLowerCase(),a===b?0:b>a?1:-1},f=function(a,b){return function(c,d){var e=a(c.td,d.td);return 0===e?b?d.index-c.index:c.index-d.index:e}};a.extend=function(a,c,d){if("function"!=typeof c||"function"!=typeof d)throw new Error("Pattern and sort must be a function");b.push({name:a,pattern:c,sort:d})},a.prototype={init:function(a,b){var c,d,e,f,g=this;if(g.table=a,g.thead=!1,g.options=b,a.rows&&a.rows.length>0&&(a.tHead&&a.tHead.rows.length>0?(c=a.tHead.rows[a.tHead.rows.length-1],g.thead=!0):c=a.rows[0]),c){var h=function(){g.current&&g.current!==this&&(g.current.classList.remove("sort-up"),g.current.classList.remove("sort-down")),g.current=this,g.sortTable(this)};for(e=0;e<c.cells.length;e++)f=c.cells[e],f.classList.contains("no-sort")||(f.classList.add("sort-header"),f.tabindex=0,f.addEventListener("click",h,!1),f.classList.contains("sort-default")&&(d=f));d&&(g.current=d,g.sortTable(d))}},sortTable:function(a,g){var h,i=this,j=a.cellIndex,k=e,l="",m=[],n=i.thead?0:1,o=a.getAttribute("data-sort-method"),p=a.getAttribute("data-sort-order");if(i.table.dispatchEvent(c("beforeSort")),g?h=a.classList.contains("sort-up")?"sort-up":"sort-down":(h=a.classList.contains("sort-up")?"sort-down":a.classList.contains("sort-down")?"sort-up":"asc"===p?"sort-down":"desc"===p?"sort-up":i.options.descending?"sort-up":"sort-down",a.classList.remove("sort-down"===h?"sort-up":"sort-down"),a.classList.add(h)),!(i.table.rows.length<2)){if(!o){for(;m.length<3&&n<i.table.tBodies[0].rows.length;)l=d(i.table.tBodies[0].rows[n].cells[j]),l=l.trim(),l.length>0&&m.push(l),n++;if(!m)return}for(n=0;n<b.length;n++)if(l=b[n],o){if(l.name===o){k=l.sort;break}}else if(m.every(l.pattern)){k=l.sort;break}i.col=j;var q,r=[],s={},t=0,u=0;for(n=0;n<i.table.tBodies.length;n++)for(q=0;q<i.table.tBodies[n].rows.length;q++)l=i.table.tBodies[n].rows[q],l.classList.contains("no-sort")?s[t]=l:r.push({tr:l,td:d(l.cells[i.col]),index:t}),t++;for("sort-down"===h?(r.sort(f(k,!0)),r.reverse()):r.sort(f(k,!1)),n=0;t>n;n++)s[n]?(l=s[n],u++):l=r[n-u].tr,i.table.tBodies[0].appendChild(l);i.table.dispatchEvent(c("afterSort"))}},refresh:function(){void 0!==this.current&&this.sortTable(this.current,!0)}},"undefined"!=typeof module&&module.exports?module.exports=a:window.Tablesort=a}(); |
@@ -61,2 +61,40 @@ tape('sorts insensitive', function(t) { | ||
tape('uses data-sort-order', function(t) { | ||
// Ascending table, descending column. | ||
var el1 = table.querySelector('th:nth-child(11)'); | ||
// Descending table, ascending column. | ||
var el2 = tableDescend.querySelector('th:nth-child(2)'); | ||
var event = document.createEvent('HTMLEvents'); | ||
event.initEvent('click', true, false); | ||
el1.dispatchEvent(event); | ||
t.equal(table.rows[1].cells[0].innerHTML, '3'); | ||
t.equal(table.rows[2].cells[0].innerHTML, '2'); | ||
t.equal(table.rows[3].cells[0].innerHTML, '1'); | ||
event.initEvent('click', true, false); | ||
el1.dispatchEvent(event); | ||
t.equal(table.rows[1].cells[0].innerHTML, '1'); | ||
t.equal(table.rows[2].cells[0].innerHTML, '2'); | ||
t.equal(table.rows[3].cells[0].innerHTML, '3'); | ||
event.initEvent('click', true, false); | ||
el2.dispatchEvent(event); | ||
t.equal(tableDescend.rows[1].cells[0].innerHTML, '1'); | ||
t.equal(tableDescend.rows[2].cells[0].innerHTML, '2'); | ||
t.equal(tableDescend.rows[3].cells[0].innerHTML, '3'); | ||
event.initEvent('click', true, false); | ||
el2.dispatchEvent(event); | ||
t.equal(tableDescend.rows[1].cells[0].innerHTML, '3'); | ||
t.equal(tableDescend.rows[2].cells[0].innerHTML, '2'); | ||
t.equal(tableDescend.rows[3].cells[0].innerHTML, '1'); | ||
t.end(); | ||
}); | ||
tape('sorts descending', function(t) { | ||
@@ -63,0 +101,0 @@ var el = tableDescend.querySelector('th:nth-child(1)'); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
266029
6829
230