2 回答

TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊
我在幫助下解決了這個(gè)問(wèn)題。我的數(shù)據(jù)表是服務(wù)器端的,具有動(dòng)態(tài)生成的列,因此我需要從每個(gè)輸入框獲取輸入值并搜索每個(gè)列。當(dāng)我了解更多時(shí),我會(huì)更新這個(gè)答案。
這是工作代碼:
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貢獻(xiàn)1891條經(jīng)驗(yàn) 獲得超3個(gè)贊
有兩種方法可以解決這個(gè)問(wèn)題:
用于
activeElement
查看哪些元素具有焦點(diǎn),并進(jìn)行相應(yīng)的搜索。在原始事件發(fā)生后設(shè)置一個(gè)計(jì)時(shí)器
keyup
,并在幾秒鐘后進(jìn)行搜索:
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
? ? }
? });
});
- 2 回答
- 0 關(guān)注
- 135 瀏覽
添加回答
舉報(bào)