Mottie / tablesorter

Github fork of Christian Bach's tablesorter plugin + awesomeness ~
https://mottie.github.io/tablesorter/docs/
2.6k stars 754 forks source link

filter don't use textExtraction rules #149

Closed 1000i100 closed 11 years ago

1000i100 commented 11 years ago

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 commented 11 years ago

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! :)

1000i100 commented 11 years ago

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 commented 11 years ago

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?

1000i100 commented 11 years ago

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 commented 11 years ago

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:

1000i100 commented 11 years ago

so this should work :

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

and it don't :(

Mottie commented 11 years ago

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;
    }
}
1000i100 commented 11 years ago

Great ! It works perfectly ! Thanks !