>I am using the tablesorter plugin, which I really like. However, in
>several of my table cells I have oddly formatted time values that look
>like the following:
>
>1d 12h 34m 25s
>
>Which maps to:
>
>1 day, 12 hours, 34 mins and 25 seconds
>
>I cannot change how these values are displayed - they have to be in
>that format. However, in my code I also create an attribute to the
>table cell called sorttable_customval in which I insert the raw number
>format as a float:
>
><td sorttable_customkey="dlt"  sorttable_customval="65.093" >1m 5s</
>td>
>
> I have tried to write my own add-on to the tablesorter source code to
>get this attribute value in the confines of the tablesorter
>textExtraction method:
>
>Javascript at the top of my webpage with the table
>======================================
>("#monitor").tablesorter({
>    textExtraction: 'customattribute',
>    debug: true,
>    widgets:['zebra']
>})
>
>The modified part of tablesorter.js (rows 233 and 234)
>=========================================
>221             function getElementText(config,node) {
>222
>223                 if(!node) return "";
>224
>225                 var t = "";
>226
>227                 if(config.textExtraction == "simple") {
>228                     if(node.childNodes[0] &&
>node.childNodes[0].hasChildNodes()) {
>229                         t = node.childNodes[0].innerHTML;
>230                     } else {
>231                         t = node.innerHTML;
>232                     }
>233                 } else if(config.textExtraction ==
>"customattribute") {
>234                         t = $(node).attr('sorttable_customval');
>235                 } else {
>236                     if(typeof(config.textExtraction) ==
>"function") {
>237                         t = config.textExtraction(node);
>238                     } else {
>239                         t = $(node).text();
>240                     }
>241                 }
>242                 return t;
>243             }
>
>I thought this would work, but I get the following error in Firebug:
>
>s is undefined    jquery.tablesorter.js (line 703)
>    return $.trim(s.toLowerCase());

The problem with using the textExtraction (at least from my understanding)
is that it works for all columns. So, I suspect the error is coming from the
fact that the 'sorttable_customval' only existing on some columns and is
returning undefined for the other columns (thus breaking $.trim().)

You were close when you wrote a custom parser--which is the way I've solved
this problem in the past. The problem is you used the "is" option instead of
the "function" option.

Try this:

$.tablesorter.addParser({
    id: 'dlt',
    is: function (s){ return false },
    format: function(value, table, cell){
        return cell.parentNode.getAttribute('sorttable_customval');
    },
    type: 'numeric'
});

In the "format" option, the "value" is what's coming back from the
textExtraction method. In this case, we want to ignore this value all
together, and instead use the cell to extract the attribute.

-Dan

Reply via email to