jquery datatables server side - filter column on t

2019-01-20 04:37发布

Hello I need to move on the top the filter column on my JQUERY DATATABLES 1.10.10 I have the filter column on the bottom:

$("dtabledID thead th").each( function () {
        var title = $(this).text();
        $(this).html( "<input type=\"text\" placeholder=\"Search "+title+"\" />" );
    } );

And a classic:

// Apply the search column filters
    table.columns().eq( 0 ).each( function ( colIdx ) {
        $( 'input', table.column( colIdx ).footer() ).on( 'keyup change', function () {
            table
                .column( colIdx )
                .search( this.value )
                .draw();
        } );
    } );

My DataTables use scrollX and scroolY function...and the content is generate server-side, and all work correctly ..the filter too.

I have need to move the filter on top (after or berfore) the Title (TH and THEAD)

I have try many solutions without success, for example :

Add TD columns in THEAD dont work

<thead>
<tr><th>col1</th><th>col2</th></tr>
<tr><td>col1</td><td>col2<</td></tr>
</thead>



 $(document).ready(function() {
$('#mytable thead td').each( function () {
        var title = $('#mytable thead th').eq( $(this).index() ).text();
        $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
});
$("#mytable thead input").on( 'keyup change', function () {
        table
            .column( $(this).parent().index()+':visible' )
            .search( this.value )
            .draw();
});
});

CSS solution: don't work

 tfoot {
    display: table-header-group;
}

any suggestion?

1条回答
神经病院院长
2楼-- · 2019-01-20 05:23

SOLUTION

  • Add extra row in thead for search filters with the same amount of columns.
  • Use orderCellsTop to instruct plugin to use top row for sorting.
  • Use the code below to create filters and attach event handler.
// Setup - add a text input to each header cell
$('#example thead tr:eq(1) th').each( function () {
    var title = $('#example thead tr:eq(0) th').eq( $(this).index() ).text();
    $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
} ); 

var table = $('#example').DataTable({
    orderCellsTop: true
});

// Apply the search
table.columns().every(function (index) {
    $('#example thead tr:eq(1) th:eq(' + index + ') input').on('keyup change', function () {
        table.column($(this).parent().index() + ':visible')
            .search(this.value)
            .draw();
    });
});

DEMO

See this jsFiddle for code and demonstration.

查看更多
登录 后发表回答