Permalink
Browse files

Add support to exclude rows

  • Loading branch information...
1 parent 064ee61 commit 8965e59905ff92a953a53998bb27655fe5f0ef3e @tristen committed Nov 6, 2012
Showing with 37 additions and 14 deletions.
  1. +3 −3 Makefile
  2. +10 −2 README.md
  3. +12 −4 demo/index.html
  4. +1 −1 package.json
  5. +8 −2 tablesort.js
  6. +3 −2 tablesort.min.js
View
6 Makefile
@@ -1,5 +1,5 @@
-lint:
- ./node_modules/.bin/jshint tablesort.js
-
tablesort.min.js:
./node_modules/.bin/uglifyjs tablesort.js > tablesort.min.js
+
+lint:
+ ./node_modules/.bin/jshint tablesort.js
View
12 README.md
@@ -28,10 +28,18 @@ new Tablesort(document.getElementById('table-id'), {
});
```
-__Exclude columns__
-For columns that do not require sorting, you can add a class of `no-sort` to a columns `th` element.
+__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.
``` html
<th class='no-sort'>Name</th>
+
+<tr class='no-sort'>
+ <td>1</td>
+ <td>Gonzo the Great</td>
+ <td>12-2-70</td>
+ <td>Radishes</td>
+ <td>$0.63</td>
+</tr>
```
__Refresh sort on appended data__
View
16 demo/index.html
@@ -123,11 +123,19 @@
descending: true
});</pre>
<p class='notice'><strong>Note:</strong> If you are using the default CSS provided you'll need to reverse the class names that style the arrows.</p>
- <h3>Exclude columns</h3>
- For columns that do not require sorting, you can add a class of <code>no-sort</code> to a columns <code>th</code> element.
- <pre class='prettyprint'>
-&lt;th class='no-sort'&gt;Name&lt;/th&gt;</pre>
+ <h3>Exclude columns or rows</h3>
+ For columns or rows that do not require sorting, you can add a class of <code>no-sort</code> to a columns <code>th</code> or a <code>tr</code> element.
+<pre class='prettyprint'>
+&lt;th class='no-sort'&gt;Name&lt;/th&gt;
+&lt;tr class='no-sort'&gt;
+ &lt;td&gt;1&lt;/td&gt;
+ &lt;td&gt;Gonzo the Great&lt;/td&gt;
+ &lt;td&gt;12-2-70&lt;/td&gt;
+ &lt;td&gt;Radishes&lt;/td&gt;
+ &lt;td&gt;$0.63&lt;/td&gt;
+&lt;/tr&gt;
+</pre>
<h3 id='refresh'>Refresh sort on appended data</h3>
<p>Tablesort supports sorting when new data has been added. Simply call the refresh method.</p>
<pre class='prettyprint'>
View
2 package.json
@@ -1,7 +1,7 @@
{
"name": "tablesort",
"description": "A sorting component for HTML tables",
- "version": "1.4.2",
+ "version": "1.5.0",
"author": "tristen <@fallsemo>",
"main": "./tablesort.js",
"ender": "./ender.js",
View
10 tablesort.js
@@ -1,5 +1,7 @@
// tablesort.js
-// tristen @fallsemo
+// Version: 1.5.0
+// Author: tristen @fallsemo
+
(function () {
function Tablesort(el, options) {
el.tagName === 'TABLE' ? this.init(el, options || {}) : console.error('Element must be a table');
@@ -144,7 +146,11 @@
// append rows that already exist rather than creating new ones
for(i = 0; i < newRows.length; i++) {
- t.tBodies[0].appendChild(newRows[i]);
+ // Don't sort on rows specified. TODO might want to
+ // do this more upstream.
+ if(!hasClass(newRows[i], 'no-sort')) {
+ t.tBodies[0].appendChild(newRows[i]);
+ }
}
},
refresh: function() {
View
5 tablesort.min.js
@@ -1,3 +1,4 @@
// tablesort.js
-// tristen @fallsemo
-(function(){function a(a,b){a.tagName==="TABLE"?this.init(a,b||{}):console.error("Element must be a table")}a.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];l(f,"no-sort")||(f.className+=" sort-header",m(f,"click",function(a){var b=g(f,"tr").getElementsByTagName("th");for(var d=0;d<b.length;d++)(l(b[d],"sort-up")||l(b[d],"sort-down"))&&b[d]!==this&&(b[d].className=b[d].className.replace(" sort-down","").replace(" sort-up",""));c.current=this,c.sortTable(this)}))}},sortTable:function(a,b){var c=this,d=a.cellIndex,m=g(a,"table");if(m.rows.length<=1)return;var n="",o=0;while(n===""&&o<m.tBodies[0].rows.length){n=h(m.tBodies[0].rows[o].cells[d]),n=j(n);if(n.substr(0,4)==="<!--"||n.length===0)n="";o++}if(n==="")return;var p,q=function(a,b){var d=h(a.cells[c.col]).toLowerCase(),e=h(b.cells[c.col]).toLowerCase();return d===e?0:d<e?1:-1},r=function(a,b){var d=h(a.cells[c.col]);d=k(d);var e=h(b.cells[c.col]);return e=k(e),i(e,d)},s=function(a,b){var d=h(a.cells[c.col]).toLowerCase(),e=h(b.cells[c.col]).toLowerCase();return f(e)-f(d)};n.match(/^-?[£$€Û¢´]\d/)||n.match(/^-?(\d+[,\.]?)+(E[-+][\d]+)?%?$/)?p=r:e(n)?p=s:p=q,this.col=d;var t=[],u=[],v,w;for(v=0;v<m.tBodies.length;v++)for(o=0;o<m.tBodies[v].rows[0].length;o++)t[o]=m.tBodies[v].rows[0][o];for(v=0;v<m.tBodies.length;v++)if(!c.thead)for(w=1;w<m.tBodies[v].rows.length;w++)u[w-1]=m.tBodies[v].rows[w];else for(w=0;w<m.tBodies[v].rows.length;w++)u[w]=m.tBodies[v].rows[w];u.sort(p),b||(c.options.d?l(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"):l(a,"sort-down")?(a.className=a.className.replace(/ sort-down/,""),a.className+=" sort-up"):(a.className=a.className.replace(/ sort-up/,""),a.className+=" sort-down")),l(a,"sort-down")&&u.reverse();for(o=0;o<u.length;o++)m.tBodies[0].appendChild(u[o])},refresh:function(){this.current!==undefined&&this.sortTable(this.current,!0)}};var b=/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\.?\,?\s*/i,c=/\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}/,d=/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/i,e=function(a){return(a.search(b)!==-1||a.search(c)!==-1||a.search(d!==-1))!==-1},f=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()},g=function(a,b){return a===null?null:a.nodeType===1&&a.tagName.toLowerCase()===b.toLowerCase()?a:g(a.parentNode,b)},h=function(a){var b=this;if(typeof a=="string"||typeof a=="undefined")return a;if(a.textContent)return a.textContent;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},i=function(a,b){var c=parseFloat(a);a=isNaN(c)?0:c;var d=parseFloat(b);return b=isNaN(d)?0:d,a-b},j=function(a){return a.replace(/^\s+|\s+$/g,"")},k=function(a){return a.replace(/[^\-?0-9.]/g,"")},l=function(a,b){return(" "+a.className+" ").indexOf(" "+b+" ")>-1},m=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)};window.Tablesort=a})();
+// Version: 1.5.0
+// Author: tristen @fallsemo
+(function(){function a(a,b){a.tagName==="TABLE"?this.init(a,b||{}):console.error("Element must be a table")}a.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];l(f,"no-sort")||(f.className+=" sort-header",m(f,"click",function(a){var b=g(f,"tr").getElementsByTagName("th");for(var d=0;d<b.length;d++)(l(b[d],"sort-up")||l(b[d],"sort-down"))&&b[d]!==this&&(b[d].className=b[d].className.replace(" sort-down","").replace(" sort-up",""));c.current=this,c.sortTable(this)}))}},sortTable:function(a,b){var c=this,d=a.cellIndex,m=g(a,"table");if(m.rows.length<=1)return;var n="",o=0;while(n===""&&o<m.tBodies[0].rows.length){n=h(m.tBodies[0].rows[o].cells[d]),n=j(n);if(n.substr(0,4)==="<!--"||n.length===0)n="";o++}if(n==="")return;var p,q=function(a,b){var d=h(a.cells[c.col]).toLowerCase(),e=h(b.cells[c.col]).toLowerCase();return d===e?0:d<e?1:-1},r=function(a,b){var d=h(a.cells[c.col]);d=k(d);var e=h(b.cells[c.col]);return e=k(e),i(e,d)},s=function(a,b){var d=h(a.cells[c.col]).toLowerCase(),e=h(b.cells[c.col]).toLowerCase();return f(e)-f(d)};n.match(/^-?[£$€Û¢´]\d/)||n.match(/^-?(\d+[,\.]?)+(E[-+][\d]+)?%?$/)?p=r:e(n)?p=s:p=q,this.col=d;var t=[],u=[],v,w;for(v=0;v<m.tBodies.length;v++)for(o=0;o<m.tBodies[v].rows[0].length;o++)t[o]=m.tBodies[v].rows[0][o];for(v=0;v<m.tBodies.length;v++)if(!c.thead)for(w=1;w<m.tBodies[v].rows.length;w++)u[w-1]=m.tBodies[v].rows[w];else for(w=0;w<m.tBodies[v].rows.length;w++)u[w]=m.tBodies[v].rows[w];u.sort(p),b||(c.options.d?l(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"):l(a,"sort-down")?(a.className=a.className.replace(/ sort-down/,""),a.className+=" sort-up"):(a.className=a.className.replace(/ sort-up/,""),a.className+=" sort-down")),l(a,"sort-down")&&u.reverse();for(o=0;o<u.length;o++)l(u[o],"no-sort")||m.tBodies[0].appendChild(u[o])},refresh:function(){this.current!==undefined&&this.sortTable(this.current,!0)}};var b=/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\.?\,?\s*/i,c=/\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}/,d=/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/i,e=function(a){return(a.search(b)!==-1||a.search(c)!==-1||a.search(d!==-1))!==-1},f=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()},g=function(a,b){return a===null?null:a.nodeType===1&&a.tagName.toLowerCase()===b.toLowerCase()?a:g(a.parentNode,b)},h=function(a){var b=this;if(typeof a=="string"||typeof a=="undefined")return a;if(a.textContent)return a.textContent;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},i=function(a,b){var c=parseFloat(a);a=isNaN(c)?0:c;var d=parseFloat(b);return b=isNaN(d)?0:d,a-b},j=function(a){return a.replace(/^\s+|\s+$/g,"")},k=function(a){return a.replace(/[^\-?0-9.]/g,"")},l=function(a,b){return(" "+a.className+" ").indexOf(" "+b+" ")>-1},m=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)};window.Tablesort=a})();

0 comments on commit 8965e59

Please sign in to comment.