How to place DataTables column filter on top

前端 未结 7 2033
[愿得一人]
[愿得一人] 2020-12-08 16:29

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 foote

相关标签:
7条回答
  • 2020-12-08 16:41

    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/

    0 讨论(0)
  • 2020-12-08 16:44

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

    }).columnFilter({
        sPlaceHolder: "head:after",
        aoColumns: [ ...
    
    0 讨论(0)
  • 2020-12-08 16:49

    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();
    });
    });
    
    0 讨论(0)
  • 2020-12-08 16:50
      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();
            }
        } );
    } );
    

    } );

    0 讨论(0)
  • 2020-12-08 16:51

    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
    
    0 讨论(0)
  • 2020-12-08 16:55

    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")
    
    0 讨论(0)
提交回复
热议问题