jquery datatables服务器端 - 顶部的过滤器列

Dan*_*llo 2 datatable datatables

您好我需要在我的JQUERY DATATABLES 1.10.10上的过滤器列顶部移动我底部有过滤器列:

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

一个经典:

// 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();
        } );
    } );
Run Code Online (Sandbox Code Playgroud)

我的DataTables使用scrollX和scroolY函数......内容生成服务器端,所有工作正常..过滤器也是如此.

我需要将过滤器移到顶部(之后或之前)标题(TH和THEAD)

我尝试了许多解决方案但没有成功,例如:

在THEAD中添加TD列 不起作用

<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();
});
});
Run Code Online (Sandbox Code Playgroud)

CSS解决方案:不起作用

 tfoot {
    display: table-header-group;
}
Run Code Online (Sandbox Code Playgroud)

有什么建议吗?

Gyr*_*com 14

  • thead为具有相同列数的搜索过滤器添加额外行.
  • 使用orderCellsTop指示插件使用顶行进行排序.
  • 使用下面的代码创建过滤器并附加事件处理程序.
// 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();
    });
});
Run Code Online (Sandbox Code Playgroud)

DEMO

有关代码和演示,请参阅此jsFiddle.