排序 DataTable jquery 排除文本字段中的空格
Sorting DataTable jquery excluding whitespace in the text fields
我的场景:
我正在尝试对使用 DataTable
jquery 插件的 table 的日期列进行排序,并且我已经对 table 进行了排序,但是文本字段具有白色-空间。因此,当我对“ASC”顺序进行排序时,空文本字段将首先占据。这不应该发生。我想对排除空文本框的 table 进行排序。
我尝试了以下代码:
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;
}
}
});
我在这段代码中发现的问题:
在上面的代码中,你可以看到 "customdatesort-pre": function (formElement)
。参数 formElement
仅采用空值,不采用具有某些日期值的文本框。
我需要的:
我需要对不包括空文本框的日期列进行排序。
使用自定义排序 pre
废弃了 asc
和 desc
。如果您定义了 pre
,那么 asc
和 desc
方法将永远不会被调用。 pre
是一种优化功能,它为每个单元格调用一次,然后内部排序器将使用该结果进行排序。参见 this thread on datatables.net。
相反,您可以将 pre
函数代码放在自定义排序文字之外,并从 asc
和 desc
方法中调用它:
customdatesortPre = function (formElement) {
//customdatesort-pre code
}
jQuery.extend(jQuery.fn.dataTableExt.oSort, {
"customdatesort-asc": function (a, b) {
a = customdatesortPre(a), b = customdatesortPre(b);
...
},
"customdatesort-desc": function (a, b) {
a = customdatesortPre(a), b = customdatesortPre(b);
...
}
});
我的场景:
我正在尝试对使用 DataTable
jquery 插件的 table 的日期列进行排序,并且我已经对 table 进行了排序,但是文本字段具有白色-空间。因此,当我对“ASC”顺序进行排序时,空文本字段将首先占据。这不应该发生。我想对排除空文本框的 table 进行排序。
我尝试了以下代码:
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;
}
}
});
我在这段代码中发现的问题:
在上面的代码中,你可以看到 "customdatesort-pre": function (formElement)
。参数 formElement
仅采用空值,不采用具有某些日期值的文本框。
我需要的:
我需要对不包括空文本框的日期列进行排序。
使用自定义排序 pre
废弃了 asc
和 desc
。如果您定义了 pre
,那么 asc
和 desc
方法将永远不会被调用。 pre
是一种优化功能,它为每个单元格调用一次,然后内部排序器将使用该结果进行排序。参见 this thread on datatables.net。
相反,您可以将 pre
函数代码放在自定义排序文字之外,并从 asc
和 desc
方法中调用它:
customdatesortPre = function (formElement) {
//customdatesort-pre code
}
jQuery.extend(jQuery.fn.dataTableExt.oSort, {
"customdatesort-asc": function (a, b) {
a = customdatesortPre(a), b = customdatesortPre(b);
...
},
"customdatesort-desc": function (a, b) {
a = customdatesortPre(a), b = customdatesortPre(b);
...
}
});