cyberhobo / ColumnFilterWidgets

This is an add-on for the DataTables plugin for jQuery that creates filtering widgets based on the data in table columns.
69 stars 34 forks source link

Lowercase and upercase in dropdown box #12

Open Muhahe opened 12 years ago

Muhahe commented 12 years ago

Hi, i found problem. When i have informations in table in lower and uper case, uper case sorts first, and lower after it. Can it be somehow repaired?

for example Box, Car, Zero, ant, glove is in dropdown box, but it have to be ant, Box, Car, glove, Zero

tpcrr commented 11 years ago

Having the same issue and came here to look for a fix.

tpcrr commented 11 years ago

In case someone else comes across the same issue.

The fnDraw() method appears to have code that lets you specify your own sort function via an 'fnSort' property. I wasn't sure where to specify that so I added it to the code.

So, at around line 164 I added my own fnSort function. This took care of the sorting issue.

    widget.sSeparator = '';  // Line 163
    widget.bSort = true;

    // Start add
    widget.fnSort = function (a, b) {
        a = a.toLowerCase();
        b = b.toLowerCase();
        if (a == b) return 0;

        var cnt = 0, tem;
        var x = /^(\.)?\d/;
        var len = Math.min(a.length, b.length) + 1;
        while (cnt < len && a.charAt(cnt) === b.charAt(cnt) &&
        x.test(b.substring(cnt)) == false && x.test(a.substring(cnt)) == false) cnt++;
        a = a.substring(cnt);
        b = b.substring(cnt);

        if (x.test(a) || x.test(b)) {
            if (x.test(a) == false) return (a) ? 1 : -1;
            else if (x.test(b) == false) return (b) ? -1 : 1;
            else {
                tem = parseFloat(a) - parseFloat(b);
                if (tem != 0) return tem;
                else tem = a.search(/[^\.\d]/);
                if (tem == -1) tem = b.search(/[^\.\d]/);
                a = a.substring(tem);
                b = b.substring(tem);
            }
        }
        if (a == b) return 0;
        else return (a > b) ? 1 : -1;
    };
    // End add

    widget.iMaxSelections = -1;
    if ('oColumnFilterWidgets' in oDataTableSettings.oInit) {
Muhahe commented 11 years ago

Nice solve of problem, but i met another one, when i have column with czech date format (dd.mm.yyyy) its sortet bad too. I trying to modify you code, but you know ...

LazyDev2k commented 11 years ago

tpcrr, there is much simpler way to do same sorting:

fnSort: function(a, b) {
    return a.toLowerCase().localeCompare(b.toLowerCase());
}
LazyDev2k commented 11 years ago

To enable sorting you can fix the code in ColumnFilterWidged.js. Change

if ( widget.hasOwnProperty( 'fnSort' ) ) {
    aDistinctOptions.sort( widget.fnSort );
} else {
    aDistinctOptions.sort();
}

to

if ( widget.oColumn.hasOwnProperty( 'fnSort' ) ) {
    aDistinctOptions.sort( widget.oColumn.fnSort );
} else {
    aDistinctOptions.sort();
}