嘗試編寫一個自定義排序函數,該函數可以檢索輸入的值,如果該行輸入了其他文本。見下文,
function getValue(x) {
if (x.indexOf('input') >= 0) {
return $(x).val();
}
return x;
}
現在,使用此功能來實現類似下面的自定義比較,
jQuery.fn.dataTableExt.oSort['cust-txt-asc'] = function (a, b) {
var x = getValue(a);
var y = getValue(b);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['cust-txt-desc'] = function (a, b) {
var x = getValue(a);
var y = getValue(b);
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
初始化數據表與上述搜索比較,
$('#example').dataTable({"aoColumns": [
{ "sType": "cust-txt" },
{ "sType": "cust-txt" },
{ "sType": "cust-txt" },
{ "sType": "cust-txt" }
]});
DEMO:http://jsfiddle.net/eLTUa/
**版本更新後的答案1.10 ** +: http://stackoverflow.com/questions/11376469/can-datatables-sort-a-column-with-an-input-field/29221907#29221907 –