如何将DataTables列过滤器置于顶部

时间:2022-02-12 14:27:01

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

我正在使用jQuery DataTables最新版本。我想在每个表上使用单独的列过滤器,所以我使用列过滤器插件,但我只在页脚中获取搜索框。我想放在标题中

    $(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 个解决方案

#1


33  

Method 1 (CSS)

You could change the CSS to

您可以将CSS更改为

tfoot {
    display: table-header-group;
}

Method 2 (Javascript)

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

将滤波器行的东西放入THEAD作为TD(而不是TH)并更改

$("tfoot input")

to

$("thead input")

#2


16  

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

您可以通过添加参数'sPlaceHolder'将其移动到表格的顶部

}).columnFilter({
    sPlaceHolder: "head:after",
    aoColumns: [ ...

#3


7  

Simply use the following javascript code ( here 'example' being the id of your table ):

只需使用以下javascript代码(此处'example'是您表的id):

$('#example tfoot tr').insertAfter($('#example thead tr'))

#4


7  

In CSS you can use

在CSS中你可以使用

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

display:table-header-group; //在tfoot上

and

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

display:table-row-group; //在thead上

You'll get them positioned like this:

你会得到这样的定位:

tfoot
thead
tbody

#5


6  

Use the sPlaceHolder option :

使用sPlaceHolder选项:

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/

看演示 - > http://jsfiddle.net/JNxj5/

#6


4  

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


1  

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

} );

});

#1


33  

Method 1 (CSS)

You could change the CSS to

您可以将CSS更改为

tfoot {
    display: table-header-group;
}

Method 2 (Javascript)

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

将滤波器行的东西放入THEAD作为TD(而不是TH)并更改

$("tfoot input")

to

$("thead input")

#2


16  

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

您可以通过添加参数'sPlaceHolder'将其移动到表格的顶部

}).columnFilter({
    sPlaceHolder: "head:after",
    aoColumns: [ ...

#3


7  

Simply use the following javascript code ( here 'example' being the id of your table ):

只需使用以下javascript代码(此处'example'是您表的id):

$('#example tfoot tr').insertAfter($('#example thead tr'))

#4


7  

In CSS you can use

在CSS中你可以使用

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

display:table-header-group; //在tfoot上

and

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

display:table-row-group; //在thead上

You'll get them positioned like this:

你会得到这样的定位:

tfoot
thead
tbody

#5


6  

Use the sPlaceHolder option :

使用sPlaceHolder选项:

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/

看演示 - > http://jsfiddle.net/JNxj5/

#6


4  

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


1  

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

} );

});