My scenario:
I am trying to sort date column of the table which uses DataTable jquery plugin and I have sorted the table but with the text fields having white-spaces. Hence, when I sort "ASC" order, then the empty text fields will occupy at first. This should not happen. I want to sort the table excluding the empty text boxes.
I tried the following code:
jQuery.extend(jQuery.fn.dataTableExt.oSort, {
"customdatesort-pre": function (formElement) {
// returns the "weight" of a cell value
var r, x;
var a = $(formElement).val();
if (a === null || a === "") {
// for empty cells: weight is a "special" value which needs special handling
r = false;
} else {
// otherwise: weight is the "time value" of the date
x = a.split("/");
r = +new Date(+x[2], +x[1] - 1, +x[0]);
}
//console.log("[PRECALC] " + a + " becomes " + r);
return r;
},
"customdatesort-asc": function (a, b) {
// return values are explained in Array.prototype.sort documentation
if (a === false && b === false) {
// if both are empty cells then order does not matter
return 0;
} else if (a === false) {
// if a is an empty cell then consider a greater than b
return 1;
} else if (b === false) {
// if b is an empty cell then consider a less than b
return -1;
} else {
// common sense
return a - b;
}
},
"customdatesort-desc": function (a, b) {
if (a === false && b === false) {
return 0;
} else if (a === false) {
return 1;
} else if (b === false) {
return -1;
} else {
return b - a;
}
}
});
Problem I found with this code:
In the above code, you can see the "customdatesort-pre": function (formElement) . The parameter formElement is taking only the empty values and not the textboxes with some date values.
What I need:
I need to sort the date columns excluding the empty textboxes.