像这样的东西?
$('table tr').hide();
$('input').keypress(function(e) {
$('table td:contains("'+String.fromCharCode(e.charCode)+'")').closest('tr').show();
});
笔记:
您需要在“回调”函数中传递事件(此处为“e”,但您可以将其命名为“事件”以获取按下了哪个键
只要按下其中一行中存在的字符,此特定示例就会显示整行。
编辑
如果您想使用数据表插件过滤,您可以在“数据表”表时执行此操作:
var yourDataTable = $('#yourDataTable').dataTable({...});
var dummySearchString = 'this.will.never.be.found.mwhahahahhaaa';
yourDataTable.fnFilter(dummySearchString);
$('.dataTables_filter input').css('color', 'white');
$('.dataTables_wrapper').delegate('.dataTables_filter input', 'focus', function () {
if (this.value === dummySearchString)
{
this.value='';
$(this).css('color', 'black');
yourDataTable.fnFilter(''); // only if you want the table to appear when you click the search field
}
});
基本上,在表格被“数据表化”之后,我们要求插件应用搜索(我们要求它搜索“不存在”的值)。因此插件“隐藏”了所有行。真的很好,文本的颜色设置为白色,所以它不会显示在输入框中。