filter don't use textExtraction rules #149

Closed
GammaNu opened this Issue Oct 9, 2012 · 8 comments

2 participants

@GammaNu

I use a custom function for textExtraction and it's perfect for sort actions. But the filter widget don't use it for filtering, it use the default text content instead.

Here an exemple :

html sample

<tr><td><span title="7 375 490 540">7,4G</span></td></tr>
<tr><td><span title="996 451 605">996M</span></td></tr>

JS config and textExtraction function

    var spanTitleSorter = function(node) 
    { 
        if($(node).text()=='-') return '-0.000001';
        if($('span[title]',node).length>0){
            floatRes = String(parseFloat($('span[title]',node).eq(0).attr('title').replace(/ /g,'').replace(/,/g,'.')));
            if(floatRes) return floatRes;
            else return $('span[title]',node).eq(0).attr('title').replace(/ /g,'').replace(/,/g,'.');
        } else return $(node).text();
    };
    $(".tabExplicit").tablesorter({
        textExtraction: spanTitleSorter,
        widgets: ['columns', 'filter', 'stickyHeaders', 'zebra'], 
    });

I hope an easy fix in the filter widget is possible !

@Mottie
Owner

Hi GammaNu!

There is a filter widget option named filter_useParsedData which forces the filter widget to only use parsed data. Or, as shown in the documentation, you can alternately add filter-parsed to the header class name, or any of the other methods listed, to have the filter widget use parsed data for a single column.

I hope that clears things up! :)

@GammaNu

Hi, i've seen that, but it look like an other way to get the same result, it don't change anything about textExtraction that's not used in filter widget, and so a fix could help the users, like me, who have used the textExtration property earlier.

But yes, i will use the other method to get it work fast ;)
Thank you !

@Mottie
Owner

The textExtraction function is used to provide the text to the parser, so the column will sort by the parsed data. The filter widget will normally look at the contents of the cell and not the parsed data because users will want to look for a date like "Feb" instead of the parsed data value which is a really long number - the number of milliseconds since 1 January 1970 00:00:00 UTC (ref).

So you've got it all working now?

@GammaNu

sure but with filter_useParsedData true, it don't use textExtraction func. It should, isn't-it ?

I've not tried an other way yet (not enougth time, i hope i will be able to try it soon)

@Mottie
Owner

The textExtraction function is part of tablesorter's core. The filter_useParsedData is part of the filter widget. Setting the filter widget option to true will only make the filter widget look at the parsed data that was already obtained using the textExtraction function and column parsers.

Here is another way to say it:

  • textExtraction tells tablesorter where to look for data. Is it just text, or inside a span or image title?
  • The parsers (text, currency, shortDate, etc) tell tablesorter how to interpret the data it gets from the textExtraction function.
  • The filter_useParsedData (if true) tells the filter widget to use the data that the parsers obtained when it filters the rows, otherwise it looks at the raw table cell text (not HTML).
@GammaNu

so this should work :

    $(".tabExplicit").tablesorter({
        textExtraction: myExtractionFunc,
        widgets: ['columns', 'filter', 'stickyHeaders', 'zebra'], 
    widgetOptions : {
        filter_useParsedData : true
    }
    });

and it don't :(

@Mottie
Owner

Ahh, you're right! That causes a javascript error in the filter widget script. I'll have it fixed soon.

After the update, check out this demo

I've set the usNumberFormat option to false to make the formatFloat function properly parse numbers like this 1.234.567,89 or 1 234 567,89

Also the headers option needed the sorter set to digit and the textExtraction function reduced down to:

// extract text from the table
textExtraction: function(node) {
    if ($('span[title]', node).length > 0) {
        return $('span[title]', node).eq(0).attr('title');
    } else {
        var txt = $(node).text();
        return txt === '-' ? '-0.000001' : txt;
    }
}
@GammaNu

Great ! It works perfectly ! Thanks !

@GammaNu GammaNu closed this Oct 14, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment