Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lowercase and upercase in dropdown box #12

Open
Muhahe opened this issue Jul 8, 2012 · 5 comments
Open

Lowercase and upercase in dropdown box #12

Muhahe opened this issue Jul 8, 2012 · 5 comments

Comments

@Muhahe
Copy link

Muhahe commented Jul 8, 2012

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
Copy link

tpcrr commented Nov 7, 2012

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

@tpcrr
Copy link

tpcrr commented Nov 8, 2012

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
Copy link
Author

Muhahe commented Nov 28, 2012

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
Copy link

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

fnSort: function(a, b) {
    return a.toLowerCase().localeCompare(b.toLowerCase());
}

@LazyDev2k
Copy link

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();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants