观看这个使用 DataTable API 中的 columns.search() 的例子 http://www.datatables.net/examples/api/multi_filter_select.html,当用户选择“全部”或空的第一个选项时,如何'清除'搜索并再次显示所有结果?
$("#example tfoot th").each( function ( i ) {
var select = $('<select><option value="">All</option></select>')
.appendTo( $(this).empty() )
.on( 'change', function () {
var term = $(this).val()!='' ? '^'+$(this).val()+'$' : '';
table.column( i )
.search(term, true, false )
.draw();
} );
table.column( i ).data().unique().sort().each( function ( d, j ) {
select.append( '<option value="'+d+'">'+d+'</option>' )
});
});
修改后的单独列过滤演示 -> http://jsfiddle.net/CmMfJ/
我不确定这是否对该示例有所帮助,但我找到了一种清除单个列搜索文本字段的方法:
首先,这是我创建字段的方式:
$(document).ready(function(){
// Setup - add a text input to each footer cell
$('#myTable tfoot th').each( function () {
var title = $('#myTable thead th').eq( $(this).index() ).text();
$(this).html( '<input id="'+title.replace(/ /g,'')+'_search" type="text" placeholder="'+title+'" />' );
} );
oTable = $('#myTable').DataTable({ settings bla bal });
// Apply the search
oTable.columns().eq( 0 ).each( function ( colIdx ) {
$( 'input', oTable.column( colIdx ).footer() ).on( 'keyup change', function () {
oTable
.column( colIdx )
.search( this.value, true )
.draw();
} );
} );
}
这就是我清除它们的方式:
function fnResetAllFilters() {
// Apply blank string
oTable.columns().eq( 0 ).each( function ( colIdx ) {
$( 'input', oTable.column( colIdx ).footer() ).val(''); //clear column visible text
oTable.columns(colIdx).search( '', true ); //clear dataTable column search filters
}
)
oTable.search( '', true ).draw(); //clears global search filter then finally draw the table, all done..
}
作者提供了一种简单的方法。
只需在您的清除事件中这样调用:
$('#datatable tfoot input').val('').change();
我想到了一种非常简单的方法。
由于DataTables使用更改事件来执行过滤,因此您只需要触发一个事件即可重新绘制表格。
function clearAllInputs() {
// Will reset all input text values to nothing.
$("input:text").val('');
// Will trigger a "change" event in order for datatables to refresh the filtering. This is because DataTables uses the change event to perform filtering.
$("input:text").trigger("change");
}
这对于特定的筛选输入文本也应该有效。您只需要使用jQuery正确选择它即可!
希望能有所帮助!