DataTables 列过滤器因 FixedHeader 而失败
DataTables Column Filter Fails With FixedHeader
我有一个 DataTables table,它使用文本输入对单独的列进行过滤。过滤器工作得很好。但是,当与 FixedHeader 插件结合使用时,我遇到了问题。当我向下滚动并且 headers 变得固定时,过滤器不再起作用。您仍然可以看到它们并输入它们,它们什么也不做。不确定它是否有所作为,但我将过滤器附加到 header 以便您可以在 table.
的顶部看到它们
我希望我只是遗漏了一些明显的东西。如果需要其他代码供参考,请告诉我。任何帮助将不胜感激。
数据表脚本
$(document).ready(function() {
$("#HCView tfoot tr").clone().appendTo($("#HCView thead")).find("th").each( function (i) {
var title = $('#HCView thead th').eq( $(this).index() ).text();
$(this).html( '<input type="text" class="HCViewSearch" data-index="'+i+'" />' );
} );
// DataTable
var table = $('#HCView').DataTable( {
paging: false,
ordering: false,
scrollX: false,
sScrollX: false,
} );
new $.fn.dataTable.FixedHeader( table, {
// options
} );
// Filter event handler
$( table.table().container() ).on( 'keyup', 'thead input', function () {
table
.column( $(this).data('index') )
.search( this.value )
.draw();
} );
$("#HCView_info").appendTo("#tableControls");
} );
发生这种情况是因为固定的 header 元素位于 table().container()
API 方法引用的元素之外。
我会使用 Individual column searching (text inputs) 页上演示的方法。
// Setup - add a text input to each header cell
$('#example thead th').each( function () {
var title = $(this).text();
$(this).html( '<input type="text" placeholder="Search '+title+'" />' );
} );
var table = $('#example').DataTable({
ordering: false,
fixedHeader: true
});
// Apply the search
table.columns().every( function () {
var that = this;
$( 'input', this.header() ).on( 'keyup change', function () {
if ( that.search() !== this.value ) {
that
.search( this.value )
.draw();
}
} );
} );
有关代码和演示,请参阅 this example。
我有一个 DataTables table,它使用文本输入对单独的列进行过滤。过滤器工作得很好。但是,当与 FixedHeader 插件结合使用时,我遇到了问题。当我向下滚动并且 headers 变得固定时,过滤器不再起作用。您仍然可以看到它们并输入它们,它们什么也不做。不确定它是否有所作为,但我将过滤器附加到 header 以便您可以在 table.
的顶部看到它们我希望我只是遗漏了一些明显的东西。如果需要其他代码供参考,请告诉我。任何帮助将不胜感激。
数据表脚本
$(document).ready(function() {
$("#HCView tfoot tr").clone().appendTo($("#HCView thead")).find("th").each( function (i) {
var title = $('#HCView thead th').eq( $(this).index() ).text();
$(this).html( '<input type="text" class="HCViewSearch" data-index="'+i+'" />' );
} );
// DataTable
var table = $('#HCView').DataTable( {
paging: false,
ordering: false,
scrollX: false,
sScrollX: false,
} );
new $.fn.dataTable.FixedHeader( table, {
// options
} );
// Filter event handler
$( table.table().container() ).on( 'keyup', 'thead input', function () {
table
.column( $(this).data('index') )
.search( this.value )
.draw();
} );
$("#HCView_info").appendTo("#tableControls");
} );
发生这种情况是因为固定的 header 元素位于 table().container()
API 方法引用的元素之外。
我会使用 Individual column searching (text inputs) 页上演示的方法。
// Setup - add a text input to each header cell
$('#example thead th').each( function () {
var title = $(this).text();
$(this).html( '<input type="text" placeholder="Search '+title+'" />' );
} );
var table = $('#example').DataTable({
ordering: false,
fixedHeader: true
});
// Apply the search
table.columns().every( function () {
var that = this;
$( 'input', this.header() ).on( 'keyup change', function () {
if ( that.search() !== this.value ) {
that
.search( this.value )
.draw();
}
} );
} );
有关代码和演示,请参阅 this example。