Comparing version 1.0.3 to 1.1.0
!function ($) { | ||
$.ender({ | ||
tablesort: function () { | ||
tablesort: function (options) { | ||
return this.forEach(function (el) { | ||
new Tablesort(el); | ||
new Tablesort(el, options); | ||
}); | ||
@@ -7,0 +7,0 @@ } |
{ | ||
"name": "tablesort", | ||
"description": "tablesort BRAP tablesort BRAP", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"author": "tristen <@fallsemo>", | ||
@@ -13,3 +13,3 @@ "main": "./tablesort.js", | ||
"licenses": [{ | ||
"type": "BSD" | ||
"type": "MIT" | ||
}], | ||
@@ -16,0 +16,0 @@ "devDependencies": { |
@@ -14,2 +14,11 @@ # tablesort.js | ||
## Additional options | ||
You can pass in options as a second parameter. Currently one option is supported: `descending: true`. By default, sort is set to ascending. | ||
``` html | ||
new Tablesort(document.getElementById('table-id'), { | ||
descending: true | ||
}); | ||
``` | ||
## Features | ||
@@ -16,0 +25,0 @@ |
// tablesort.js | ||
// tristen @fallsemo | ||
function Tablesort(el) { | ||
el.tagName === 'TABLE' ? this.init(el) : console.error('Element must be a table'); | ||
function Tablesort(el, options) { | ||
el.tagName === 'TABLE' ? this.init(el, options || {}) : console.error('Element must be a table'); | ||
} | ||
Tablesort.prototype = { | ||
thead: false, | ||
col: undefined, | ||
init: function(el) { | ||
init: function(el, options) { | ||
var that = this; | ||
var firstRow; | ||
this.thead = false; | ||
this.options = options; | ||
this.options.d = options.descending || false; | ||
if (el.rows && el.rows.length > 0) { | ||
@@ -22,3 +24,3 @@ if (el.tHead && el.tHead.rows.length > 0) { | ||
} | ||
if (!firstRow) { return; } | ||
if (!firstRow) return; | ||
@@ -28,3 +30,3 @@ // Assume first row is the header and attach a click handler to each. | ||
var cell = firstRow.cells[i]; | ||
cell.className += ' sort-header'; | ||
cell.className += 'sort-header'; | ||
this.addEvent(cell, 'click', function(e) { | ||
@@ -53,5 +55,9 @@ // Delete any sort classes on table headers that are not the current one. | ||
var bb = that.getInnerText(b.cells[that.col]).toLowerCase(); | ||
if (aa === bb) { return 0; } | ||
if (aa < bb) { return -1; } | ||
return 1; | ||
if (aa === bb) return 0; | ||
if (that.options.d) { | ||
if (aa < bb) return -1; | ||
return 1; | ||
} | ||
if (aa < bb) return 1; | ||
return -1; | ||
} | ||
@@ -63,3 +69,5 @@ var sortNumber = function(a, b) { | ||
bb = that.cleanNumber(bb); | ||
return that.compareNumber(aa, bb); | ||
if (that.options.d) return that.compareNumber(aa, bb); | ||
return that.compareNumber(bb, aa); | ||
} | ||
@@ -156,5 +164,5 @@ | ||
var a = parseFloat(a); | ||
a = (isNaN(a) ? 0 : a); | ||
a = isNaN(a) ? 0 : a; | ||
var b = parseFloat(b); | ||
b = (isNaN(b) ? 0 : b); | ||
b = isNaN(b) ? 0 : b; | ||
return a - b; | ||
@@ -161,0 +169,0 @@ }, |
// tablesort.js | ||
// tristen @fallsemo | ||
function Tablesort(a){a.tagName==="TABLE"?this.init(a):console.error("Element must be a table")}Tablesort.prototype={thead:!1,col:undefined,init:function(a){var b=this,c;a.rows&&a.rows.length>0&&(a.tHead&&a.tHead.rows.length>0?(c=a.tHead.rows[a.tHead.rows.length-1],b.thead=!0):c=a.rows[0]);if(!c)return;for(var d=0;d<c.cells.length;d++){var e=c.cells[d];e.className+=" sort-header",this.addEvent(e,"click",function(a){var c=b.getParent(e,"tr").getElementsByTagName("th");for(var d=0;d<c.length;d++)(b.hasClass(c[d],"sort-up")||b.hasClass(c[d],"sort-down"))&&c[d]!==this&&(c[d].className=c[d].className.replace(" sort-up","").replace(" sort-down",""));b.sortTable(this)})}},sortTable:function(a){var b=this,c=a.cellIndex,d=this.getParent(a,"table"),e=function(a,c){var d=b.getInnerText(a.cells[b.col]).toLowerCase(),e=b.getInnerText(c.cells[b.col]).toLowerCase();return d===e?0:d<e?-1:1},f=function(a,c){var d=b.getInnerText(a.cells[b.col]);d=b.cleanNumber(d);var e=b.getInnerText(c.cells[b.col]);return e=b.cleanNumber(e),b.compareNumber(d,e)};if(d.rows.length<=1)return;var g="",h=0;while(g===""&&h<d.tBodies[0].rows.length){var g=b.getInnerText(d.tBodies[0].rows[h].cells[c]);g=b.trim(g);if(g.substr(0,4)==="<!--"||g.length===0)g="";h++}if(g==="")return;var i;g.match(/^-?[£$Û¢´]\d/)||g.match(/^-?(\d+[,\.]?)+(E[-+][\d]+)?%?$/)?i=f:i=e,this.col=c;var j=[],k=[];for(var l=0;l<d.tBodies.length;l++)for(var h=0;h<d.tBodies[l].rows[0].length;h++)j[h]=d.tBodies[l].rows[0][h];for(var l=0;l<d.tBodies.length;l++)if(!b.thead)for(var m=1;m<d.tBodies[l].rows.length;m++)k[m-1]=d.tBodies[l].rows[m];else for(var m=0;m<d.tBodies[l].rows.length;m++)k[m]=d.tBodies[l].rows[m];k.sort(i),b.hasClass(a,"sort-up")?(a.className=a.className.replace(/ sort-up/,""),a.className+=" sort-down"):(a.className=a.className.replace(/ sort-down/,""),a.className+=" sort-up",k.reverse());for(var h=0;h<k.length;h++)k[h].className||d.tBodies[0].appendChild(k[h])},getInnerText:function(a){var b=this;if(typeof a=="string"||typeof a=="undefined")return a;if(a.innerText)return a.innerText;var c="",d=a.childNodes,e=d.length;for(var f=0;f<e;f++)switch(d[f].nodeType){case 1:c+=b.getInnerText(d[f]);break;case 3:c+=d[f].nodeValue}return c},getParent:function(a,b){return a===null?null:a.nodeType===1&&a.tagName.toLowerCase()===b.toLowerCase()?a:this.getParent(a.parentNode,b)},compareNumber:function(a,b){var a=parseFloat(a);a=isNaN(a)?0:a;var b=parseFloat(b);return b=isNaN(b)?0:b,a-b},trim:function(a){return a.replace(/^\s+|\s+$/g,"")},cleanNumber:function(a){return a.replace(/[^-?0-9.]/g,"")},hasClass:function(a,b){return(" "+a.className+" ").indexOf(" "+b+" ")>-1},addEvent:function(a,b,c){a.attachEvent?(a["e"+b+c]=c,a[b+c]=function(){a["e"+b+c](window.event)},a.attachEvent("on"+b,a[b+c])):a.addEventListener(b,c,!1)}}; | ||
function Tablesort(a,b){a.tagName==="TABLE"?this.init(a,b||{}):console.error("Element must be a table")}Tablesort.prototype={init:function(a,b){var c=this,d;this.thead=!1,this.options=b,this.options.d=b.descending||!1,a.rows&&a.rows.length>0&&(a.tHead&&a.tHead.rows.length>0?(d=a.tHead.rows[a.tHead.rows.length-1],c.thead=!0):d=a.rows[0]);if(!d)return;for(var e=0;e<d.cells.length;e++){var f=d.cells[e];f.className+="sort-header",this.addEvent(f,"click",function(a){var b=c.getParent(f,"tr").getElementsByTagName("th");for(var d=0;d<b.length;d++)(c.hasClass(b[d],"sort-up")||c.hasClass(b[d],"sort-down"))&&b[d]!==this&&(b[d].className=b[d].className.replace(" sort-up","").replace(" sort-down",""));c.sortTable(this)})}},sortTable:function(a){var b=this,c=a.cellIndex,d=this.getParent(a,"table"),e=function(a,c){var d=b.getInnerText(a.cells[b.col]).toLowerCase(),e=b.getInnerText(c.cells[b.col]).toLowerCase();return d===e?0:b.options.d?d<e?-1:1:d<e?1:-1},f=function(a,c){var d=b.getInnerText(a.cells[b.col]);d=b.cleanNumber(d);var e=b.getInnerText(c.cells[b.col]);return e=b.cleanNumber(e),b.options.d?b.compareNumber(d,e):b.compareNumber(e,d)};if(d.rows.length<=1)return;var g="",h=0;while(g===""&&h<d.tBodies[0].rows.length){var g=b.getInnerText(d.tBodies[0].rows[h].cells[c]);g=b.trim(g);if(g.substr(0,4)==="<!--"||g.length===0)g="";h++}if(g==="")return;var i;g.match(/^-?[£$Û¢´]\d/)||g.match(/^-?(\d+[,\.]?)+(E[-+][\d]+)?%?$/)?i=f:i=e,this.col=c;var j=[],k=[];for(var l=0;l<d.tBodies.length;l++)for(var h=0;h<d.tBodies[l].rows[0].length;h++)j[h]=d.tBodies[l].rows[0][h];for(var l=0;l<d.tBodies.length;l++)if(!b.thead)for(var m=1;m<d.tBodies[l].rows.length;m++)k[m-1]=d.tBodies[l].rows[m];else for(var m=0;m<d.tBodies[l].rows.length;m++)k[m]=d.tBodies[l].rows[m];k.sort(i),b.hasClass(a,"sort-up")?(a.className=a.className.replace(/ sort-up/,""),a.className+=" sort-down"):(a.className=a.className.replace(/ sort-down/,""),a.className+=" sort-up",k.reverse());for(var h=0;h<k.length;h++)k[h].className||d.tBodies[0].appendChild(k[h])},getInnerText:function(a){var b=this;if(typeof a=="string"||typeof a=="undefined")return a;if(a.innerText)return a.innerText;var c="",d=a.childNodes,e=d.length;for(var f=0;f<e;f++)switch(d[f].nodeType){case 1:c+=b.getInnerText(d[f]);break;case 3:c+=d[f].nodeValue}return c},getParent:function(a,b){return a===null?null:a.nodeType===1&&a.tagName.toLowerCase()===b.toLowerCase()?a:this.getParent(a.parentNode,b)},compareNumber:function(a,b){var a=parseFloat(a);a=isNaN(a)?0:a;var b=parseFloat(b);return b=isNaN(b)?0:b,a-b},trim:function(a){return a.replace(/^\s+|\s+$/g,"")},cleanNumber:function(a){return a.replace(/[^-?0-9.]/g,"")},hasClass:function(a,b){return(" "+a.className+" ").indexOf(" "+b+" ")>-1},addEvent:function(a,b,c){a.attachEvent?(a["e"+b+c]=c,a[b+c]=function(){a["e"+b+c](window.event)},a.attachEvent("on"+b,a[b+c])):a.addEventListener(b,c,!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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
60395
11
0
1155
58
0