Comparing version 2.0.1 to 2.1.0
{ | ||
"name": "tablesort", | ||
"description": "A sorting component for HTML tables", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"author": "tristen <@fallsemo>", | ||
@@ -6,0 +6,0 @@ "ender": "./ender.js", |
@@ -1,9 +0,10 @@ | ||
# tablesort.js | ||
tablesort | ||
--- | ||
A small & simple sorting component for tables written in Javascript. | ||
[![Build Status](https://travis-ci.org/tristen/tablesort.png?Zeqckz55oF1LjKHEqHT7)](https://travis-ci.org/tristen/tablesort) | ||
Tablesort is a small & simple sorting component for tables written in Javascript. It has no dependencies and should have no interference with other libraries. | ||
### Basic usage | ||
## Basic usage | ||
``` html | ||
@@ -15,3 +16,3 @@ <script src='tablesort.min.js'></script> | ||
``` | ||
## Sort Types | ||
### Sort Types | ||
@@ -22,6 +23,7 @@ * strings | ||
* Basic dates in `dd/mm/yy` or `dd-mm-yy` format. Years can be 4 digits. Days and Months can be 1 or 2 digits. | ||
* Dot separated values. E.g. IP addresses or version numbers. | ||
## Additional options | ||
### Additional options | ||
__Ascending/Descending__ | ||
#### Ascending/Descending | ||
You can pass in options as a second parameter. Currently one option is supported: `descending: true`. By default, sort is set to ascending. | ||
@@ -35,3 +37,3 @@ | ||
__Exclude columns or rows__ | ||
#### Exclude columns or rows | ||
For columns or rows that do not require sorting, you can add a class of `no-sort` to a columns `th` or a `tr` element. | ||
@@ -50,3 +52,3 @@ ``` html | ||
__Override data that is sorted on__ | ||
#### Override data that is sorted on | ||
Sometimes text inside cells is not normalized. Using a `data-sort` attribute you can use optional data to sort on. | ||
@@ -65,3 +67,3 @@ | ||
__Default sort on tablesort initialization__ | ||
#### Default sort on tablesort initialization | ||
It is possible to automatically sort the table once you create a Tablesort instance by adding `sort-default` class. | ||
@@ -73,3 +75,3 @@ | ||
__Refresh sort on appended data__ | ||
#### Refresh sort on appended data | ||
Tablesort supports sorting when new data has been added. Simply call the refresh method. | ||
@@ -87,19 +89,26 @@ | ||
## Ender support | ||
Add `tablesort` as an internal chain method to your [Ender](http://ender.no.de) compilation. | ||
### Node/Browserify | ||
``` shell | ||
$ ender add tablesort | ||
``` js | ||
// npm install tablesort | ||
var tablesort = require('tablesort'); | ||
tablesort(el, options); | ||
``` | ||
Use it: | ||
### Ender support | ||
Add `tablesort` as an internal chain method to your [Ender](https://github.com/ender-js/Ender/) compilation. | ||
``` js | ||
// ender add tablesort | ||
$('.table').tablesort(); | ||
``` | ||
## Default style | ||
### Default style | ||
Add the styling below to your CSS or roll with your own. | ||
``` css | ||
th.sort-header::-moz-selection { background:transparent; } | ||
th.sort-header::selection { background:transparent; } | ||
th.sort-header { | ||
@@ -136,3 +145,3 @@ cursor:pointer; | ||
## Building | ||
### Building | ||
@@ -142,7 +151,7 @@ Tablesort relies on [Grunt](http://gruntjs.com) as its build tool. Simply run `grunt` to package code | ||
## Licence | ||
### Licence | ||
MIT | ||
## Tests | ||
### Tests | ||
@@ -153,4 +162,4 @@ ```sh | ||
## Bugs? | ||
### Bugs? | ||
[Create an issue](https://github.com/tristen/tablesort/issues) |
@@ -1,7 +0,5 @@ | ||
;(function () { | ||
;(function() { | ||
function Tablesort(el, options) { | ||
if (el.tagName !== 'TABLE') { | ||
throw new Error('Element must be a table'); | ||
} | ||
if (!el) throw new Error('Element not found'); | ||
if (el.tagName !== 'TABLE') throw new Error('Element must be a table'); | ||
this.init(el, options || {}); | ||
@@ -27,7 +25,5 @@ } | ||
if (!firstRow) { | ||
return; | ||
} | ||
if (!firstRow) return; | ||
var onClick = function () { | ||
var onClick = function() { | ||
if (that.current && that.current !== this) { | ||
@@ -60,3 +56,3 @@ if (that.current.classList.contains(classSortUp)) { | ||
} | ||
if (defaultSort) { | ||
@@ -101,3 +97,3 @@ that.current = defaultSort; | ||
// Possible sortFunction scenarios | ||
var sortCaseInsensitive = function (a, b) { | ||
var sortCaseInsensitive = function(a, b) { | ||
var aa = getInnerText(a.cells[that.col]).toLowerCase(), | ||
@@ -112,3 +108,3 @@ bb = getInnerText(b.cells[that.col]).toLowerCase(); | ||
var sortNumber = function (a, b) { | ||
var sortNumber = function(a, b) { | ||
var aa = getInnerText(a.cells[that.col]), | ||
@@ -128,7 +124,25 @@ bb = getInnerText(b.cells[that.col]); | ||
var sortDotSep = function(a, b) { | ||
var aa = getInnerText(a.cells[that.col]).split('.'), | ||
bb = getInnerText(b.cells[that.col]).split('.'); | ||
for (var i = 0, len = aa.length; i < len; i++) { | ||
var aai = parseInt(aa[i]), | ||
bbi = parseInt(bb[i]); | ||
if (aai == bbi) continue; | ||
if (aai < bbi) return -1; | ||
if (aai > bbi) return 1; | ||
} | ||
return 0; | ||
}; | ||
// Sort dot separted numbers, e.g. ip addresses or version numbers | ||
if (/^(\d+\.)+\d+$/.test(item)) { | ||
sortFunction = sortDotSep; | ||
// Sort as number if a currency key exists or number | ||
if (item.match(/^-?[£\x24Û¢´€]?\d+\s*([,\.]\d{0,2})/) || // prefixed currency | ||
} else if (item.match(/^-?[£\x24Û¢´€]?\d+\s*([,\.]\d{0,2})/) || // prefixed currency | ||
item.match(/^-?\d+\s*([,\.]\d{0,2})?[£\x24Û¢´€]/) || // suffixed currency | ||
item.match(/^-?(\d)*-?([,\.]){0,1}-?(\d)+([E,e][\-+][\d]+)?%?$/) // number | ||
) { | ||
) { | ||
sortFunction = sortNumber; | ||
@@ -181,4 +195,4 @@ } else if (testDate(item)) { | ||
// Make a stable sort function | ||
var stabilize = function (sort) { | ||
return function (a, b) { | ||
var stabilize = function(sort) { | ||
return function(a, b) { | ||
var unstableResult = sort(a.tr, b.tr); | ||
@@ -195,4 +209,4 @@ if (unstableResult === 0) { | ||
// reversed. | ||
var antiStabilize = function (sort) { | ||
return function (a, b) { | ||
var antiStabilize = function(sort) { | ||
return function(a, b) { | ||
var unstableResult = sort(a.tr, b.tr); | ||
@@ -254,3 +268,3 @@ if (unstableResult === 0) { | ||
parseDate = function (date) { | ||
parseDate = function(date) { | ||
date = date.replace(/\-/g, '/'); | ||
@@ -261,3 +275,3 @@ date = date.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{2})/, '$1/$2/$3'); // format before getTime | ||
getParent = function (el, pTagName) { | ||
getParent = function(el, pTagName) { | ||
if (el === null) { | ||
@@ -272,8 +286,6 @@ return null; | ||
getInnerText = function (el) { | ||
getInnerText = function(el) { | ||
var that = this; | ||
if (typeof el === 'string' || typeof el === 'undefined') { | ||
return el; | ||
} | ||
if (typeof el === 'string' || typeof el === 'undefined') return el; | ||
@@ -284,7 +296,5 @@ var str = el.getAttribute('data-sort') || ''; | ||
return str; | ||
} | ||
else if (el.textContent) { | ||
} else if (el.textContent) { | ||
return el.textContent; | ||
} | ||
else if (el.innerText) { | ||
} else if (el.innerText) { | ||
return el.innerText; | ||
@@ -312,3 +322,3 @@ } | ||
compareNumber = function (a, b) { | ||
compareNumber = function(a, b) { | ||
var aa = parseFloat(a), | ||
@@ -322,3 +332,3 @@ bb = parseFloat(b); | ||
cleanNumber = function (i) { | ||
cleanNumber = function(i) { | ||
return i.replace(/[^\-?0-9.]/g, ''); | ||
@@ -325,0 +335,0 @@ }; |
/*! | ||
* tablesort v2.0.1 (2014-10-30) | ||
* tablesort v2.1.0 (2015-01-09) | ||
* http://tristen.ca/tablesort/demo | ||
* Copyright (c) 2014 ; Licensed MIT | ||
*/!function(){function a(a,b){if("TABLE"!==a.tagName)throw new Error("Element must be a table");this.init(a,b||{})}a.prototype={init:function(a,d){var e,f=this;if(this.thead=!1,this.options=d,a.rows&&a.rows.length>0&&(a.tHead&&a.tHead.rows.length>0?(e=a.tHead.rows[a.tHead.rows.length-1],f.thead=!0):e=a.rows[0]),e){for(var g,h=function(){f.current&&f.current!==this&&(f.current.classList.contains(b)?f.current.classList.remove(b):f.current.classList.contains(c)&&f.current.classList.remove(c)),f.current=this,f.sortTable(this)},i=0;i<e.cells.length;i++){var j=e.cells[i];j.classList.contains("no-sort")||(j.classList.add("sort-header"),j.addEventListener("click",h,!1),j.classList.contains("sort-default")&&(g=j))}g&&(f.current=g,f.sortTable(g,!0))}},getFirstDataRowIndex:function(){return this.thead?0:1},sortTable:function(a,d){var e,f=this,m=a.cellIndex,n=i(a,"table"),o="",p=f.getFirstDataRowIndex();if(!(n.rows.length<=1)){for(;""===o&&p<n.tBodies[0].rows.length;)o=j(n.tBodies[0].rows[p].cells[m]),o=o.trim(),("<!--"===o.substr(0,4)||0===o.length)&&(o=""),p++;if(""!==o){var q=function(a,b){var c=j(a.cells[f.col]).toLowerCase(),d=j(b.cells[f.col]).toLowerCase();return c===d?0:d>c?1:-1},r=function(a,b){var c=j(a.cells[f.col]),d=j(b.cells[f.col]);return c=l(c),d=l(d),k(d,c)},s=function(a,b){var c=j(a.cells[f.col]).toLowerCase(),d=j(b.cells[f.col]).toLowerCase();return h(d)-h(c)};e=o.match(/^-?[£\x24Û¢´€]?\d+\s*([,\.]\d{0,2})/)||o.match(/^-?\d+\s*([,\.]\d{0,2})?[£\x24Û¢´€]/)||o.match(/^-?(\d)*-?([,\.]){0,1}-?(\d)+([E,e][\-+][\d]+)?%?$/)?r:g(o)?s:q,this.col=m;var t,u=[],v={},w=0;for(p=0;p<n.tBodies.length;p++)for(t=0;t<n.tBodies[p].rows.length;t++){var x=n.tBodies[p].rows[t];x.classList.contains("no-sort")?v[w]=x:u.push({tr:x,index:w}),w++}var y=f.options.descending?c:b,z=f.options.descending?b:c;d?a.classList.contains(y)||a.classList.contains(z)||a.classList.add(y):a.classList.contains(y)?(a.classList.remove(y),a.classList.add(z)):(a.classList.remove(z),a.classList.add(y));var A=function(a){return function(b,c){var d=a(b.tr,c.tr);return 0===d?b.index-c.index:d}},B=function(a){return function(b,c){var d=a(b.tr,c.tr);return 0===d?c.index-b.index:d}};a.classList.contains(c)?(u.sort(B(e)),u.reverse()):u.sort(A(e));var C=0;for(p=0;w>p;p++){var D;v[p]?(D=v[p],C++):D=u[p-C].tr,n.tBodies[0].appendChild(D)}}}},refresh:function(){void 0!==this.current&&this.sortTable(this.current,!0)}};var b="sort-up",c="sort-down",d=/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\.?\,?\s*/i,e=/\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}/,f=/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/i,g=function(a){return-1!==(-1!==a.search(d)||-1!==a.search(e)||a.search(-1!==f))&&!isNaN(h(a))},h=function(a){return a=a.replace(/\-/g,"/"),a=a.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{2})/,"$1/$2/$3"),new Date(a).getTime()},i=function(a,b){return null===a?null:1===a.nodeType&&a.tagName.toLowerCase()===b.toLowerCase()?a:i(a.parentNode,b)},j=function(a){var b=this;if("string"==typeof a||"undefined"==typeof a)return a;var c=a.getAttribute("data-sort")||"";if(c)return c;if(a.textContent)return a.textContent;if(a.innerText)return a.innerText;for(var d=a.childNodes,e=d.length,f=0;e>f;f++)switch(d[f].nodeType){case 1:c+=b.getInnerText(d[f]);break;case 3:c+=d[f].nodeValue}return c},k=function(a,b){var c=parseFloat(a),d=parseFloat(b);return a=isNaN(c)?0:c,b=isNaN(d)?0:d,a-b},l=function(a){return a.replace(/[^\-?0-9.]/g,"")};"undefined"!=typeof module&&module.exports?module.exports=a:window.Tablesort=a}(); | ||
* Copyright (c) 2015 ; Licensed MIT | ||
*/!function(){function a(a,b){if(!a)throw new Error("Element not found");if("TABLE"!==a.tagName)throw new Error("Element must be a table");this.init(a,b||{})}a.prototype={init:function(a,d){var e,f=this;if(this.thead=!1,this.options=d,a.rows&&a.rows.length>0&&(a.tHead&&a.tHead.rows.length>0?(e=a.tHead.rows[a.tHead.rows.length-1],f.thead=!0):e=a.rows[0]),e){for(var g,h=function(){f.current&&f.current!==this&&(f.current.classList.contains(b)?f.current.classList.remove(b):f.current.classList.contains(c)&&f.current.classList.remove(c)),f.current=this,f.sortTable(this)},i=0;i<e.cells.length;i++){var j=e.cells[i];j.classList.contains("no-sort")||(j.classList.add("sort-header"),j.addEventListener("click",h,!1),j.classList.contains("sort-default")&&(g=j))}g&&(f.current=g,f.sortTable(g,!0))}},getFirstDataRowIndex:function(){return this.thead?0:1},sortTable:function(a,d){var e,f=this,m=a.cellIndex,n=i(a,"table"),o="",p=f.getFirstDataRowIndex();if(!(n.rows.length<=1)){for(;""===o&&p<n.tBodies[0].rows.length;)o=j(n.tBodies[0].rows[p].cells[m]),o=o.trim(),("<!--"===o.substr(0,4)||0===o.length)&&(o=""),p++;if(""!==o){var q=function(a,b){var c=j(a.cells[f.col]).toLowerCase(),d=j(b.cells[f.col]).toLowerCase();return c===d?0:d>c?1:-1},r=function(a,b){var c=j(a.cells[f.col]),d=j(b.cells[f.col]);return c=l(c),d=l(d),k(d,c)},s=function(a,b){var c=j(a.cells[f.col]).toLowerCase(),d=j(b.cells[f.col]).toLowerCase();return h(d)-h(c)},t=function(a,b){for(var c=j(a.cells[f.col]).split("."),d=j(b.cells[f.col]).split("."),e=0,g=c.length;g>e;e++){var h=parseInt(c[e]),i=parseInt(d[e]);if(h!=i){if(i>h)return-1;if(h>i)return 1}}return 0};e=/^(\d+\.)+\d+$/.test(o)?t:o.match(/^-?[£\x24Û¢´€]?\d+\s*([,\.]\d{0,2})/)||o.match(/^-?\d+\s*([,\.]\d{0,2})?[£\x24Û¢´€]/)||o.match(/^-?(\d)*-?([,\.]){0,1}-?(\d)+([E,e][\-+][\d]+)?%?$/)?r:g(o)?s:q,this.col=m;var u,v=[],w={},x=0;for(p=0;p<n.tBodies.length;p++)for(u=0;u<n.tBodies[p].rows.length;u++){var y=n.tBodies[p].rows[u];y.classList.contains("no-sort")?w[x]=y:v.push({tr:y,index:x}),x++}var z=f.options.descending?c:b,A=f.options.descending?b:c;d?a.classList.contains(z)||a.classList.contains(A)||a.classList.add(z):a.classList.contains(z)?(a.classList.remove(z),a.classList.add(A)):(a.classList.remove(A),a.classList.add(z));var B=function(a){return function(b,c){var d=a(b.tr,c.tr);return 0===d?b.index-c.index:d}},C=function(a){return function(b,c){var d=a(b.tr,c.tr);return 0===d?c.index-b.index:d}};a.classList.contains(c)?(v.sort(C(e)),v.reverse()):v.sort(B(e));var D=0;for(p=0;x>p;p++){var E;w[p]?(E=w[p],D++):E=v[p-D].tr,n.tBodies[0].appendChild(E)}}}},refresh:function(){void 0!==this.current&&this.sortTable(this.current,!0)}};var b="sort-up",c="sort-down",d=/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\.?\,?\s*/i,e=/\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}/,f=/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/i,g=function(a){return-1!==(-1!==a.search(d)||-1!==a.search(e)||a.search(-1!==f))&&!isNaN(h(a))},h=function(a){return a=a.replace(/\-/g,"/"),a=a.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{2})/,"$1/$2/$3"),new Date(a).getTime()},i=function(a,b){return null===a?null:1===a.nodeType&&a.tagName.toLowerCase()===b.toLowerCase()?a:i(a.parentNode,b)},j=function(a){var b=this;if("string"==typeof a||"undefined"==typeof a)return a;var c=a.getAttribute("data-sort")||"";if(c)return c;if(a.textContent)return a.textContent;if(a.innerText)return a.innerText;for(var d=a.childNodes,e=d.length,f=0;e>f;f++)switch(d[f].nodeType){case 1:c+=b.getInnerText(d[f]);break;case 3:c+=d[f].nodeValue}return c},k=function(a,b){var c=parseFloat(a),d=parseFloat(b);return a=isNaN(c)?0:c,b=isNaN(d)?0:d,a-b},l=function(a){return a.replace(/[^\-?0-9.]/g,"")};"undefined"!=typeof module&&module.exports?module.exports=a:window.Tablesort=a}(); |
Sorry, the diff of this file is not supported yet
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
248040
156
6501