Null Values + Currency Sort
Null Values + Currency Sort
I'm using the great currency plugin to sort, but it breaks when there are blank/null cells. I've tried to integrate one of the blank/null quasi-plugins, but it still does not work. Would appreciate any help!
Here is what I'm starting with and what is live on the site now: http://www.lawschooltransparency.com/clearinghouse/?show=compare&sub=costs
[code] jQuery.fn.dataTableExt.oSort['currency-asc'] = function(a,b) {
/* Remove any formatting */
var x = a == "-" ? 0 : a.replace( /[^\d\-\.]/g, "" );
var y = b == "-" ? 0 : b.replace( /[^\d\-\.]/g, "" );
/* Parse and return */
x = parseFloat( x );
y = parseFloat( y );
return x - y;
};
jQuery.fn.dataTableExt.oSort['currency-desc'] = function(a,b) {
var x = a == "-" ? 0 : a.replace( /[^\d\-\.]/g, "" );
var y = b == "-" ? 0 : b.replace( /[^\d\-\.]/g, "" );
x = parseFloat( x );
y = parseFloat( y );
return y - x;
};[/code]
Sort works everywhere but the three columns beginning with "Resident" as those are the columns with blanks.
So then I tried to integrate this:
[code]jQuery.fn.dataTableExt.oSort['mystring-asc'] = function(x,y) {
var retVal;
x = $.trim(x);
y = $.trim(y);
if (x==y) retVal= 0;
else if (x == "" || x == " ") retVal= 1;
else if (y == "" || y == " ") retVal= -1;
else if (x > y) retVal= 1;
else retVal = -1; // <- this was missing in version 1
return retVal;
}
jQuery.fn.dataTableExt.oSort['mystring-desc'] = function(y,x) {
var retVal;
x = $.trim(x);
y = $.trim(y);
if (x==y) retVal= 0;
else if (x == "" || x == " ") retVal= -1;
else if (y == "" || y == " ") retVal= 1;
else if (x > y) retVal= 1;
else retVal = -1; // <- this was missing in version 1
return retVal;
}[/code]
I added everything from "if(x==y)" to "return retVal;" and defined the retVal variable at the beginning of the function.
At that point, everything went haywire and I had to take it down. The problem is that I don't understand how oSort works, so I don't really know what I should be trying to return in certain scenarios.
Thanks in advance for any help.
Here is what I'm starting with and what is live on the site now: http://www.lawschooltransparency.com/clearinghouse/?show=compare&sub=costs
[code] jQuery.fn.dataTableExt.oSort['currency-asc'] = function(a,b) {
/* Remove any formatting */
var x = a == "-" ? 0 : a.replace( /[^\d\-\.]/g, "" );
var y = b == "-" ? 0 : b.replace( /[^\d\-\.]/g, "" );
/* Parse and return */
x = parseFloat( x );
y = parseFloat( y );
return x - y;
};
jQuery.fn.dataTableExt.oSort['currency-desc'] = function(a,b) {
var x = a == "-" ? 0 : a.replace( /[^\d\-\.]/g, "" );
var y = b == "-" ? 0 : b.replace( /[^\d\-\.]/g, "" );
x = parseFloat( x );
y = parseFloat( y );
return y - x;
};[/code]
Sort works everywhere but the three columns beginning with "Resident" as those are the columns with blanks.
So then I tried to integrate this:
[code]jQuery.fn.dataTableExt.oSort['mystring-asc'] = function(x,y) {
var retVal;
x = $.trim(x);
y = $.trim(y);
if (x==y) retVal= 0;
else if (x == "" || x == " ") retVal= 1;
else if (y == "" || y == " ") retVal= -1;
else if (x > y) retVal= 1;
else retVal = -1; // <- this was missing in version 1
return retVal;
}
jQuery.fn.dataTableExt.oSort['mystring-desc'] = function(y,x) {
var retVal;
x = $.trim(x);
y = $.trim(y);
if (x==y) retVal= 0;
else if (x == "" || x == " ") retVal= -1;
else if (y == "" || y == " ") retVal= 1;
else if (x > y) retVal= 1;
else retVal = -1; // <- this was missing in version 1
return retVal;
}[/code]
I added everything from "if(x==y)" to "return retVal;" and defined the retVal variable at the beginning of the function.
At that point, everything went haywire and I had to take it down. The problem is that I don't understand how oSort works, so I don't really know what I should be trying to return in certain scenarios.
Thanks in advance for any help.
This discussion has been closed.
Replies
[code]
var x = (a==="-" || a==="") ? 0 : a.replace( /[^\d\-\.]/g, "" );
[/code]
You could add your trim in as well at the start of the function.
Allan