Skip to content
This repository
Browse code

Add support to exclude rows

  • Loading branch information...
commit 8965e59905ff92a953a53998bb27655fe5f0ef3e 1 parent 064ee61
authored
6 Makefile
... ... @@ -1,5 +1,5 @@
1   -lint:
2   - ./node_modules/.bin/jshint tablesort.js
3   -
4 1 tablesort.min.js:
5 2 ./node_modules/.bin/uglifyjs tablesort.js > tablesort.min.js
  3 +
  4 +lint:
  5 + ./node_modules/.bin/jshint tablesort.js
12 README.md
Source Rendered
@@ -28,10 +28,18 @@ new Tablesort(document.getElementById('table-id'), {
28 28 });
29 29 ```
30 30
31   -__Exclude columns__
32   -For columns that do not require sorting, you can add a class of `no-sort` to a columns `th` element.
  31 +__Exclude columns or rows__
  32 +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.
33 33 ``` html
34 34 <th class='no-sort'>Name</th>
  35 +
  36 +<tr class='no-sort'>
  37 + <td>1</td>
  38 + <td>Gonzo the Great</td>
  39 + <td>12-2-70</td>
  40 + <td>Radishes</td>
  41 + <td>$0.63</td>
  42 +</tr>
35 43 ```
36 44
37 45 __Refresh sort on appended data__
16 demo/index.html
@@ -123,11 +123,19 @@
123 123 descending: true
124 124 });</pre>
125 125 <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>
126   - <h3>Exclude columns</h3>
127   - For columns that do not require sorting, you can add a class of <code>no-sort</code> to a columns <code>th</code> element.
128   - <pre class='prettyprint'>
129   -&lt;th class='no-sort'&gt;Name&lt;/th&gt;</pre>
  126 + <h3>Exclude columns or rows</h3>
  127 + 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.
  128 +<pre class='prettyprint'>
  129 +&lt;th class='no-sort'&gt;Name&lt;/th&gt;
130 130
  131 +&lt;tr class='no-sort'&gt;
  132 + &lt;td&gt;1&lt;/td&gt;
  133 + &lt;td&gt;Gonzo the Great&lt;/td&gt;
  134 + &lt;td&gt;12-2-70&lt;/td&gt;
  135 + &lt;td&gt;Radishes&lt;/td&gt;
  136 + &lt;td&gt;$0.63&lt;/td&gt;
  137 +&lt;/tr&gt;
  138 +</pre>
131 139 <h3 id='refresh'>Refresh sort on appended data</h3>
132 140 <p>Tablesort supports sorting when new data has been added. Simply call the refresh method.</p>
133 141 <pre class='prettyprint'>
2  package.json
... ... @@ -1,7 +1,7 @@
1 1 {
2 2 "name": "tablesort",
3 3 "description": "A sorting component for HTML tables",
4   - "version": "1.4.2",
  4 + "version": "1.5.0",
5 5 "author": "tristen <@fallsemo>",
6 6 "main": "./tablesort.js",
7 7 "ender": "./ender.js",
10 tablesort.js
... ... @@ -1,5 +1,7 @@
1 1 // tablesort.js
2   -// tristen @fallsemo
  2 +// Version: 1.5.0
  3 +// Author: tristen @fallsemo
  4 +
3 5 (function () {
4 6 function Tablesort(el, options) {
5 7 el.tagName === 'TABLE' ? this.init(el, options || {}) : console.error('Element must be a table');
@@ -144,7 +146,11 @@
144 146
145 147 // append rows that already exist rather than creating new ones
146 148 for(i = 0; i < newRows.length; i++) {
147   - t.tBodies[0].appendChild(newRows[i]);
  149 + // Don't sort on rows specified. TODO might want to
  150 + // do this more upstream.
  151 + if(!hasClass(newRows[i], 'no-sort')) {
  152 + t.tBodies[0].appendChild(newRows[i]);
  153 + }
148 154 }
149 155 },
150 156 refresh: function() {
5 tablesort.min.js
... ... @@ -1,3 +1,4 @@
1 1 // tablesort.js
2   -// tristen @fallsemo
3   -(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})();
  2 +// Version: 1.5.0
  3 +// Author: tristen @fallsemo
  4 +(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.
Something went wrong with that request. Please try again.