Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Untitled
Sunday, January 27th, 2008 at 10:06:42pm MST 

  1. /***
  2. |''Name:''|TableSortingPlugin|
  3. |''Description:''|Dynamically sort tables by clicking on column headers|
  4. |''Author:''|Saq Imtiaz ( lewcid@gmail.com )|
  5. |''Source:''|http://tw.lewcid.org/#TableSortingPlugin|
  6. |''Code Repository:''|http://tw.lewcid.org/svn/plugins|
  7. |''Version:''|2.02|
  8. |''Date:''|25-01-2008|
  9. |''License:''|[[Creative Commons Attribution-ShareAlike 3.0 License|http://creativecommons.org/licenses/by-sa/3.0/]]|
  10. |''~CoreVersion:''|2.2.3|
  11. !!Usage:
  12. * Make sure your table has a header row
  13. ** {{{|Name|Phone Number|Address|h}}}<br> Note the /h/ that denote a header row
  14. * Give the table a class of 'sortable'
  15. ** {{{
  16. |sortable|k
  17. |Name|Phone Number|Address|h
  18. }}}<br>Note the /k/ that denotes a class name being assigned to the table.
  19. * To disallow sorting by a column, place {{{<<nosort>>}}} in it's header
  20. * To automatically sort a table by a column, place {{{<<autosort>>}}} in the header for that column
  21. ** Or to sort automatically but in reverse order, use {{{<<autosort reverse>>}}}
  22.  
  23. !!Example:
  24. |sortable|k
  25. |Name |Salary |Extension |Performance |File Size |Start date |h
  26. |ZBloggs, Fred |$12000.00 |1353 |+1.2 |74.2Kb |Aug 19, 2003 21:34:00 |
  27. |ABloggs, Fred |$12000.00 |1353 |1.2 |3350b |09/18/2003 |
  28. |CBloggs, Fred |$12000 |1353 |1.200 |55.2Kb |August 18, 2003 |
  29. |DBloggs, Fred |$12000.00 |1353 |1.2 |2100b |07/18/2003 |
  30. |Bloggs, Fred |$12000.00 |1353 |01.20 |6.156Mb |08/17/2003 05:43 |
  31. |Turvey, Kevin |$191200.00 |2342 |-33 |1b |02/05/1979 |
  32. |Mbogo, Arnold |$32010.12 |2755 |-21.673 |1.2Gb |09/08/1998 |
  33. |Shakespeare, Bill |£122000.00|3211 |6 |33.22Gb |12/11/1961 |
  34. |Shakespeare, Hamlet |£9000 |9005 |-8 |3Gb |01/01/2002 |
  35. |Fitz, Marvin |€3300.30 |5554 |+5 |4Kb |05/22/1995 |
  36.  
  37. ***/
  38. // /%
  39. //!BEGIN-PLUGIN-CODE
  40. config.tableSorting = {
  41.        
  42.         darrow: "\u2193",
  43.        
  44.         uarrow: "\u2191",
  45.        
  46.         getText : function (o) {
  47.                 var p = o.cells[SORT_INDEX];
  48.                 return p.innerText || p.textContent || '';
  49.         },
  50.        
  51.         sortTable : function (o,rev) {
  52.                 SORT_INDEX = o.getAttribute("index");
  53.                 var c = config.tableSorting;
  54.                 var T = findRelated(o.parentNode,"TABLE");
  55.                 if(T.tBodies[0].rows.length<=1)
  56.                         return;
  57.                 var itm = "";
  58.                 var i = 0;
  59.                 while (itm == "" && i < T.tBodies[0].rows.length) {
  60.                         itm = c.getText(T.tBodies[0].rows[i]).trim();
  61.                         i++;
  62.                 }
  63.                 if (itm == "")
  64.                         return;        
  65.                 var r = [];
  66.                 var S = o.getElementsByTagName("span")[0];           
  67.                 c.fn = c.sortAlpha;
  68.                 if(!isNaN(Date.parse(itm)))
  69.                         c.fn = c.sortDate;
  70.                 else if(itm.match(/^[$|£|€|\+|\-]{0,1}\d*\.{0,1}\d+$/))
  71.                         c.fn = c.sortNumber;
  72.                 else if(itm.match(/^\d*\.{0,1}\d+[K|M|G]{0,1}b$/))
  73.                         c.fn = c.sortFile;
  74.                 for(i=0; i<T.tBodies[0].rows.length; i++) {
  75.                          r[i]=T.tBodies[0].rows[i];
  76.                 }
  77.                 r.sort(c.reSort);
  78.                 if(S.firstChild.nodeValue==c.darrow || rev) {
  79.                         r.reverse();
  80.                         S.firstChild.nodeValue=c.uarrow;
  81.                 }
  82.                 else
  83.                         S.firstChild.nodeValue=c.darrow;
  84.                 var thead = T.getElementsByTagName('thead')[0];
  85.                 var headers = thead.rows[thead.rows.length-1].cells;
  86.                 for(var k=0; k<headers.length; k++) {
  87.                         if(!hasClass(headers[k],"nosort"))
  88.                                 addClass(headers[k].getElementsByTagName("span")[0],"hidden");
  89.                 }
  90.                 removeClass(S,"hidden");
  91.                 for(i=0; i<r.length; i++) {
  92.                         T.tBodies[0].appendChild(r[i]);
  93.                         c.stripe(r[i],i);
  94.                         for(var j=0; j<r[i].cells.length;j++){
  95.                                 removeClass(r[i].cells[j],"sortedCol");
  96.                         }
  97.                         addClass(r[i].cells[SORT_INDEX],"sortedCol");
  98.                 }
  99.         },
  100.        
  101.         stripe : function (e,i){
  102.                 var cl = ["oddRow","evenRow"];
  103.                 i&1? cl.reverse() : cl;
  104.                 removeClass(e,cl[1]);
  105.                 addClass(e,cl[0]);
  106.         },
  107.        
  108.         sortNumber : function(v) {
  109.                 var x = parseFloat(this.getText(v).replace(/[^0-9.-]/g,''));
  110.                 return isNaN(x)? 0: x;
  111.         },
  112.        
  113.         sortDate : function(v) {
  114.                 return Date.parse(this.getText(v));
  115.         },
  116.  
  117.         sortAlpha : function(v) {
  118.                 return this.getText(v).toLowerCase();
  119.         },
  120.        
  121.         sortFile : function(v) {               
  122.                 var j, q = config.messages.sizeTemplates, s = this.getText(v);
  123.                 for (var i=0; i<q.length; i++) {
  124.                         if ((j = s.toLowerCase().indexOf(q[i].template.replace("%0\u00a0","").toLowerCase())) != -1)
  125.                                 return q[i].unit * s.substr(0,j);
  126.                 }
  127.                 return parseFloat(s);
  128.         },
  129.        
  130.         reSort : function(a,b){
  131.                 var c = config.tableSorting;
  132.                 var aa = c.fn(a);
  133.                 var bb = c.fn(b);
  134.                 return ((aa==bb)? 0 : ((aa<bb)? -1:1));
  135.         },
  136.        
  137.         setupTable : function(table){
  138.                 var c = config.tableSorting;       
  139.                 var x = null, rev, thead = table.getElementsByTagName('thead')[0], headers = thead.rows[thead.rows.length-1].cells;
  140.                 for (var j=0; j<headers.length; j++){
  141.                         var h = headers[j];
  142.                         if (hasClass(h,"nosort"))
  143.                                 continue;
  144.                         h.setAttribute("index",j);
  145.                         h.onclick = function(){c.sortTable(this); return false;};
  146.                         h.ondblclick = stopEvent;
  147.                         if(h.getElementsByTagName("span").length == 0)
  148.                                 createTiddlyElement(h,"span",null,"hidden",c.uarrow);
  149.                         if(!x && hasClass(h,"autosort")) {
  150.                                 x = j;
  151.                                 rev = hasClass(h,"reverse");
  152.                         }
  153.                 }
  154.                 if(x)
  155.                         c.sortTable(headers[x],rev);               
  156.         },
  157.        
  158.         hijackTableHandler : function(){
  159.                 var tableFormatter;
  160.                 for (var i=0; i<config.formatters.length; i++){
  161.                         if(config.formatters[i]['name'] == 'table')
  162.                                 tableFormatter = config.formatters[i];
  163.                 }
  164.                 tableFormatter.old_tableSorting_handler = tableFormatter.handler;
  165.                 tableFormatter.handler = function(w){
  166.                         this.old_tableSorting_handler.apply(this,arguments);
  167.                         var table = w.output.lastChild;
  168.                         if(hasClass(table,"sortable")){
  169.                                 addClass(table,"_ts_setup");
  170.                                 config.tableSorting.setupTable(table);
  171.                         }
  172.                 }
  173.         }       
  174. };
  175.  
  176. config.tableSorting.hijackTableHandler();
  177.  
  178. Story.prototype.tSort_refreshTiddler = Story.prototype.refreshTiddler;
  179. Story.prototype.refreshTiddler = function(title,template,force,customFields,defaultText){
  180.         var elem = this.tSort_refreshTiddler.apply(this,arguments);
  181.         if(elem){
  182.                 var tables = elem.getElementsByTagName("TABLE");
  183.                 for(var i=0; i<tables.length; i++){
  184.                         if(hasClass(tables[i],"sortable") && !hasClass(tables[i],"_ts_setup")){
  185.                                 config.tableSorting.setupTable(tables[i]);
  186.                         }
  187.                 }
  188.         }
  189.         return elem;
  190. };
  191.  
  192. setStylesheet("table.sortable span.hidden {visibility:hidden;}\n"+
  193.         "table.sortable thead {cursor:pointer;}\n"+
  194.         "table.sortable .nosort {cursor:default;}\n"+
  195.         "table.sortable td.sortedCol {background:#ffc;}","TableSortingPluginStyles");
  196.  
  197. function stopEvent(e){
  198.         var ev = e? e : window.event;
  199.         ev.cancelBubble = true;
  200.         if (ev.stopPropagation) ev.stopPropagation();
  201.         return false;   
  202. }       
  203.  
  204. config.macros.nosort={
  205.         handler : function(place){
  206.                 addClass(place,"nosort");
  207.         }       
  208. };
  209.  
  210. config.macros.autosort={
  211.         handler : function(place,m,p,w,pS){
  212.                 addClass(place,"autosort"+" "+pS);           
  213.         }       
  214. };
  215. //!END-PLUGIN-CODE
  216. // %/

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right
worth-right fantasy-obligation