How to place DataTables column filter on top

2019-01-13 21:30发布

I am using jQuery DataTables latest version. I want to use individual column filter on every table so am using the column filter plugin but am getting search boxes in footer only. I want to place in the header

    $(document).ready(function () {
var  oTable=$("#example").dataTable({
       "bJQueryUI": true,
        "sScrollX": "100%",
        "aLengthMenu": [[5, 15, 50, 100], [5, 15, 50, "l00"]],
        "iDisplayLength": 10,
         "sPaginationType": "full_numbers",
        "sDom": '<"top"if>rt<"bottom"lp><"clear">'

    }).columnFilter({"sPlaceHolder":"head :before",
    "aoColumns": [{ "type": "text" }, { "type": "text" }, null, null, null, null, { "type": "text" }, null, { "type": "text" }, { "type": "text" }, { "type": "text" },

How can I place it on the top of my table?

7条回答
叼着烟拽天下
2楼-- · 2019-01-13 22:08

In CSS you can use

display: table-header-group; //on tfoot

and

display: table-row-group; //on thead

You'll get them positioned like this:

tfoot
thead
tbody
查看更多
男人必须洒脱
3楼-- · 2019-01-13 22:09
  CSS:  
 tfoot input {
    width: 100%;
    padding: 3px;
    box-sizing: border-box;
}
 tfoot {
display: table-header-group;}

 Script:
 $(document).ready(function() {
// Setup - add a text input to each footer cell
$('#example tfoot th').each( function () {
    var title = $(this).text();
    $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
} );

// DataTable
 var table = $('#example').DataTable();

// Apply the search
 table.columns().every( function () {
    var that = this;

    $( 'input', this.footer() ).on( 'keyup change', function () {
        if ( that.search() !== this.value ) {
            that
                .search( this.value )
                .draw();
        }
    } );
} );

} );

查看更多
对你真心纯属浪费
4楼-- · 2019-01-13 22:13

You can move it to the top of your table by adding the parameter 'sPlaceHolder'

}).columnFilter({
    sPlaceHolder: "head:after",
    aoColumns: [ ...
查看更多
女痞
5楼-- · 2019-01-13 22:15

Method 1 (CSS)

You could change the CSS to

tfoot {
    display: table-header-group;
}

Method 2 (Javascript)

Put the filter row stuff into the THEAD as TDs (not THs) and change

$("tfoot input")

to

$("thead input")
查看更多
成全新的幸福
6楼-- · 2019-01-13 22:16

Try this

$(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();
});
});
查看更多
放荡不羁爱自由
7楼-- · 2019-01-13 22:27

Use the sPlaceHolder option :

sPlaceHolder: "head:before"

example :

dataTable.columnFilter({
  sPlaceHolder: "head:before",
  aoColumns: [
      { type: "select" },  
      { type: "select" },        
      { type: "select" },  
      { type: "select" },  
      { type: "select" }
  ]
});

see demo -> http://jsfiddle.net/JNxj5/

查看更多
登录 后发表回答