Link to home
Start Free TrialLog in
Avatar of Niall Gallagher
Niall GallagherFlag for Ireland

asked on

Sorting with js tablesorter

Hi again,
I'm using  tablesorter for the first time and it's working pretty good but it is messing me up on dates and on empty cells .
I am trying to build some script to take the dates and rearrange it so it would be easier to sort
Instead of mm/dd/yyyy I am trying to parse it as yyyymmdd but it just isn't working

This is what I have for sorting but I have to admit I put this in because of the dates and empty cells not sorting but it has made no difference.
$(function () {
            $("table.tablesorter").tablesorter({
                widthFixed: false,
                sortList: [[0, 0]],
                headers: {
                    0: { sorter: false },
                    1: { sorter: false },
                    2: { sorter: false },
                    3: { sorter: "digit" },
                    4: { sorter: "text" },
                    5: { sorter: "text" },
                    6: { sorter: "text" },
                    7: { sorter: "text" },
                    8: { sorter: "text" },
                    9: { sorter: "myDateColumn" },
                    10: { sorter: "myDateColumn" },
                    11: { sorter: "myDateColumn" },
                    12: { sorter: "text" },
                    13: { sorter: "text" },
                    14: { sorter: "text" }
                }
            }) 
     });

Open in new window


and then I tried to do an addparse to try and do something with the date
  $.tablesorter.addparser({
            id: 'myDateColumn',
            is: function (s) {
                return false;
            },
            format: function (s) {
                var t = s.split("/");
                var newdate = t[2] + t[0] + t[1];
                return newdate;
            },
            type: 'numeric'

Open in new window


I thought this should work but it's not also there are some cells which are empty or actually they have " ----------"in them how can I work round these when trying to sort.
Any help is HIGHLY Appreciated
Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany image

Hi,
please try this:
var newdate = parseInt('' + t[2] + '' + t[0] + '' + t[1]);

Open in new window


Otherwise please provide some sample data or try to repro on jsBin or jsFiddle.

Thanks and HTH
Rainer
Sorry, one change to return a 0 if the string is empty / does not contain 2 slashes:
    $.tablesorter.addparser({
            id: 'myDateColumn',
            is: function (s) {
                return false;
            },
            format: function (s) {
                var t = s.split("/");
				if t.length == 3 {
					return parseInt(''+ t[2] + '' + t[0] + '' + t[1],10);
				} else {
					return 0;
				}
            },
            type: 'numeric'

Open in new window

If you want to sort the empty entries at the end, then return 99999999 instead of the 0.
HTH
Rainer
Avatar of Niall Gallagher

ASKER

Thank you Rainer for your help and it seems to make sense but there are 2 dates in the table which seem to stick together and are usually nearly half way down.
About 95% or more of the dates are in 2015 but there are 2 (one is 12/10/2014 and the other 02/12/2013) and they just keep showing up in the wrong places
How can I see if the parse is working
Hi,
hm hard to say without the raw HTML source. Could you perhaps attach / paste the HTML source / data?
Perhaps its just a space / special character / inline HTML tags ...
Thanks.
Rainer
The very best way would be to reproduce your situation e.g. at http://jsbin.com (and share the link inside this question).
Just take a sample of 4-8 rows from you existing set. Then we have something to work with and we can start to dig deeper into the current issue.
HTH
Rainer
Thanks,
I have never used it before so don't hold your breath waiting. but again thanks for your help
Hi,
you are welcome.
jsBin is nice in regards to have some kind of repro running on a web server, being able to reference scripts / plugins from CDN networks (HTTP and HTTPS) and share the issue with the world/experts - as most issues occur on private / internal sites where we experts have no access. This cannot solve every issue but in most cases some sample data is enough to get an idea how to solve / where the issue comes from.
As its late in Germany I will have a look tomorrow.
KR
Rainer
Rainer,
Something I didn't mention yesterday, I built the table to be editable so its coming up in JSBin with a label and a textbox for each field but on the actual site , it has the label only until you click on it then the label disappears and the textbox becomes visible.
Thanks for your help and excuse the mess. Let me know what else you need or what I can do thanks

I'm not sure where to put the patch to the js files or the tablesorter file.

http://jsbin.com/yanulovuwu/1/edit?html,js,output
Rainer,
I am searching to see why sorting is not working at all in JSBin.
I have tidied it up a lot since the last message but just can't figure out what I have forgot
Even in JSBin those 2 dates still show up in the wrong place look in App Date
ASKER CERTIFIED SOLUTION
Avatar of Rob
Rob
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you, and I will try it in the next few days and let you know how it goes.
I have not put this into practice yet but I do believe it would be a better solution as while reading about tablesorter a lot of users seem to have the same problem as I do