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

emptyTo not working with custom parser #1278

Closed
ghost opened this issue Aug 30, 2016 · 6 comments
Closed

emptyTo not working with custom parser #1278

ghost opened this issue Aug 30, 2016 · 6 comments

Comments

@ghost
Copy link

ghost commented Aug 30, 2016

empty or emptyTo seem to be working correctly, but not if applied to a column with a unique parser. My parser is as follows, if that helps:

$.tablesorter.addParser({
    id: 'pihmsDate',
    is: function(s){
        return false;
    },
    format: function(s){
        s = s.replace(/JAN/,01).replace(/FEB/,02).replace(/MAR/,03).replace(/APR/,04).replace(/MAY/,05).replace(/JUN/,06).replace(/JUL/,07).replace(/AUG/,08).replace(/SEP/,09).replace(/OCT/,10).replace(/NOV/,11).replace(/DEC/,12);
        s = s.replace(/(\d{2})[\/\-](\d{1,2})[\/\-](\d{4})/, '$3/$2/$1');
        return $.tablesorter.formatFloat(new Date(s).getTime());
    },
    type: 'numeric'
});

@Mottie
Copy link
Owner

Mottie commented Aug 30, 2016

Hi @aetna-wsa!

The parser isn't returning an empty string, it is returning NaN because new Date("").getTime() returns NaN which is what the formatFloat function returns. This isn't an empty string, so the emptyTo option won't treat it as such.

Try these changes:

$.tablesorter.addParser({
    id: 'pihmsDate',
    is: function(s){
        return false;
    },
    format: function(s){
        var str = s.replace(/JAN/,01).replace(/FEB/,02).replace(/MAR/,03).replace(/APR/,04).replace(/MAY/,05).replace(/JUN/,06).replace(/JUL/,07).replace(/AUG/,08).replace(/SEP/,09).replace(/OCT/,10).replace(/NOV/,11).replace(/DEC/,12);
        str = str.replace(/(\d{2})[\/\-](\d{1,2})[\/\-](\d{4})/, '$3/$2/$1');
        var date = str ? new Date(str) : str;
        return date instanceof Date && isFinite( date ) ? date.getTime() : s;
    },
    type: 'numeric'
});

Actually, I'm not sure why that parser is needed, at least for English named dates. I believe all modern browsers will correctly parse dates with the three letter month name:

> new Date('JAN 02 2016')
// Sat Jan 02 2016 00:00:00 GMT-0600 (Central Standard Time)
> new Date('02 JAN 2016')
// Sat Jan 02 2016 00:00:00 GMT-0600 (Central Standard Time)

@ghost
Copy link
Author

ghost commented Aug 31, 2016

Wow... I never considered that NaN was the value being returned. Nor did I know that three letter month names can be parsed as easily as any other date - I always found it to be an awkward format. Both these solutions work, but I'm going to go with shortDate, now, with the format of dd-mmm-yyyy

I appreciate the help, Rob. Thank you!

@ghost
Copy link
Author

ghost commented Aug 31, 2016

I am now using shortDate with the format described above. When I sort that column in either ascending or descending order, all empty cells stay at the bottom. Shouldn't the behavior reverse when moving from asc to desc (or vice versa)?

@Mottie
Copy link
Owner

Mottie commented Aug 31, 2016

The emptyTo option has multiple settings - try them in this demo.

Edit: actually, that last demo is missing two newer settings: emptyMax and emptyMin.

Mottie added a commit that referenced this issue Aug 31, 2016
@ghost
Copy link
Author

ghost commented Aug 31, 2016

Perfect! The additions of emptyMax and emptyMin are fantastic. Thank you.

@Mottie
Copy link
Owner

Mottie commented Sep 23, 2016

I'm guessing this issue has been resolved, so I'm going to close it. If you continue to have problems, please feel free to continue the discussion in this thread.

@Mottie Mottie closed this as completed Sep 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant