0

I recently had trouble with the tablesorter.js where I couldnt sort by date because whitespace was breaking my script (tablesorter, wont sort by date correctly)

I finally figured this out and changed the code to the following

$.tablesorter.addParser({
    id: 'dayMonthYear',
    is: function (s) {
        return false;
    },
    format: function (s) {
        s = $.trim(s.replace(/\s+/g, ' '));
        var date = s.match(/^(\d{1,2})[ ](\w{3})[ ](\d{4})$/);
        var day = String(date[1]);
        if (day.length == 1) {
            day = "0" + day;
        }
        var month = monthNames[date[2]];
        var year = date[3];
        return sortableDate = '' + year + month + day;
    },
    type: 'numeric'
});
var monthNames = {};
monthNames["Jan"] = "01";
monthNames["Feb"] = "02";
monthNames["Mar"] = "03";
monthNames["Apr"] = "04";
monthNames["May"] = "05";
monthNames["Jun"] = "06";
monthNames["Jul"] = "07";
monthNames["Aug"] = "08";
monthNames["Sep"] = "09";
monthNames["Oct"] = "10";
monthNames["Nov"] = "11";
monthNames["Dec"] = "12";

This seemed to do the trick but i've just noticed that it doesn't work in IE8 or IE7. Seems to be fine in IE9.

Does older versions of IE have a problem with $.trim? If so is there a work around?

Thanks


Edit: Think i may be getting closer to an answer, just found this http://www.javascripter.net/faq/trim.htm

Community
  • 1
  • 1
Gillian
  • 401
  • 1
  • 5
  • 14
  • 1
    *i've just noticed that it doesn't work in IE8 or IE7* what doesn't work ? what errors ? – Manse May 21 '12 at 13:35
  • Sorry ManseUK, I didnt explain that too well. My initial problem was that I couldnt sort by date (dd mmm yyyy) but it turned out that it was whitespace that was causing my script to break. I've now removed the spaces using $.trim and the script seems to run perfectly - allowing me to sort by date - but for some reason it is still broke in IE8/7. Works fine in IE9 though.... – Gillian May 21 '12 at 13:46
  • Understand that .... but how doesnt it work ? what errors do you get ? is the whitespace removed ? – Manse May 21 '12 at 13:50
  • check -> http://stackoverflow.com/questions/360491/how-do-i-strip-white-space-when-grabbing-text-with-jquery – Kanishka Panamaldeniya May 21 '12 at 13:57
  • @Gillian just a guess ... but this line `return sortableDate = '' + year + month + day;` doesnt validate on jslint - try changing it to `return '' + year + month + day;` and running it in IE8/7 – Manse May 21 '12 at 14:00
  • I don't actually get an error message, the table sorter just fails to sort any column in IE8/7. If i remove the header:{0:sorter...etc} I can sort the columns again, so it must be something to do with the 'dayMonthYear' parser... – Gillian May 21 '12 at 14:14
  • @ManseUK - just tried this but didnt seem to change anything. – Gillian May 21 '12 at 14:22
  • @Gillian no probs - just an idea ....sorry ! – Manse May 21 '12 at 14:24
  • Just checked and IE8/7 still appear to have whitespace – Gillian May 21 '12 at 14:44
  • possible duplicate of [.trim() in JavaScript not working in IE](http://stackoverflow.com/questions/2308134/trim-in-javascript-not-working-in-ie) – Alex Angas May 03 '14 at 11:48

2 Answers2

0

i may be wrong , but i think you are not replacing the white space properly

s = $.trim(s.replace(/\s+/g, ' '));

in your code you can see the white space is not removing .

try

s = $.trim(s.replace(/\s+/g, ''));

and also ne point , you can not remove white spaces in a word by $.trim it removes white spaces , before and after a word .

ex :-

var trimmed_text = $.trim('hello world '); 


$.trim  will only remove the space after **world**

so the variable trimmed_text is now 'hello world'

UPDATE

try this code

s.replace(/ /g,'');
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
  • Didnt seem to make a difference in IE8/7 unfortunately. Thanks though :) . Maybe i should try something different to remove the whitespace. – Gillian May 21 '12 at 13:53
0

Thanks for troubleshooting this with me yesterday really appreciated it.

I do now have this working.

The problem I had was that the tablesorter would not work if the table contained any whitespace, my solution was to remove this using $.trim but unknown to me (until yesterday) $.trim does not work in IE8/7/6.

I didn't find an alternative to $.trim to make this work in IE8/7/6 - although the following posts might be useful

.trim() in JavaScript not working in IE

http://www.javascripter.net/faq/trim.htm

I did however notice that in one of the xsl files the following code existed #160; which was causing all the issues. And after questioning this line with the devs, it would appear that this was added to prevent a layout issue that appeared in browsers over 8 years ago. Needless to say I've now removed that and the $.trim line in my parser and it is working as it should in all browsers. Phew....

Community
  • 1
  • 1
Gillian
  • 401
  • 1
  • 5
  • 14