简体   繁体   中英

DataTables Column Filter Fails With FixedHeader

I have a DataTables table with individual column filtering using text input. The filters were working great. However, when combined with the FixedHeader plugin, I am having issues. When I scroll down and the headers become fixed, the filters no longer function. You can still see them and type into them, they just do nothing. Not sure if it makes a difference, but I have the filters appended to the header so that you can see them at the top of the table.

I'm hoping I am just missing something obvious here. If additional code is needed for reference, let me know please. Any help will be greatly appreciated.

DataTables Script

$(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");

} );

This happens because fixed header element is located outside of the element referred by table().container() API method.

I would use method demonstrated on Individual column searching (text inputs) page.

// 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();
        }
    } );
} );   

See this example for code and demonstration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM