2 回答

TA貢獻1815條經驗 獲得超10個贊
我能夠在幫助下解決它。我的數據表是服務器端的動態生成列,所以我需要從每個輸入框中獲取輸入值并搜索每一列。當我了解更多時,我會更新這個答案。
這是工作代碼:
table.columns().every(function () {
$('input', this.footer()).keypress(function (e) {
if (e.keyCode == 13) { //search only when Enter key is pressed to avoid wasteful calls
e.preventDefault(); //input is within <form> element which submits form when Enter key is pressed. e.preventDefault() prevents this
var header = table.table().header(); //header because I moved search boxes to header
var inputBoxes = header.getElementsByTagName('input');
$.each(data.columns, function (index) {
var inputBoxVal = inputBoxes[index].value;
table.column(index).search(inputBoxVal);
});
table.draw();
}
});
});

TA貢獻2051條經驗 獲得超10個贊
有兩種方法可以解決這個問題:
用于activeElement查看哪些元素具有焦點,并相應地進行搜索。
在您的原始事件之后設置一個計時器keyup,并在幾秒鐘后進行搜索:
var searchTimer;
table.columns().every( function () {
var that = this;
$( 'input', this.footer() ).on( 'keyup change clear', function () {
if ( that.search() !== this.value ) {
clearTimeout(searchTimer); // Reset timer
var value = this.value;
searchTimer = setTimeout(function() {
that.search(value).draw();
}, 3000); // Wait 3 seconds
}
});
});
添加回答
舉報