jQuery DataTables:隐藏搜索栏

use*_*743 2 javascript jquery datatables

我似乎无法隐藏DataTables的默认搜索栏。到目前为止,我已经尝试过使用线程的解决方案,但是设置bFilter:false完全禁用了过滤功能,因此页脚中的搜索框不再起作用。

我已经贴上jsfiddle

我的HTML:

<thead>
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
    <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
</thead>
<tbody>
    <table id="mytable">
        <thead>
            <tr>
                <th>name</th>
                <th>type</th>
                <th>color</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th></th>
                <th></th>
                <th></th>
            </tr>
        </tfoot>
        <tbody>
            <tr>
                <td>apple</td>
                <td>fruit</td>
                <td>red</td>
            </tr>
            <tr>
                <td>banana</td>
                <td>fruit</td>
                <td>yellow</td>
            </tr>
            <tr>
                <td>carrot</td>
                <td>vegie</td>
                <td>red</td>
            </tr>
            <tr>
                <td>potato</td>
                <td>vegie</td>
                <td>brown</td>
            </tr>
        </tbody>
    </table>
</tbody>
Run Code Online (Sandbox Code Playgroud)

和jQuery代码:

$(document).ready(function(){
    let table = $('#mytable').DataTable();
  $('#mytable tfoot th').each( function (i) {
        let title = $('#mytable thead th').eq( $(this).index() ).text();
        $(this).html( '<input type="text" placeholder="Search '+title+'" data-index="'+i+'" />' );
    } );
  $( table.table().container() ).on( 'keyup', 'tfoot input', function () {
    table
      .column( $(this).data('index') )
      .search( this.value )
      .draw();
  } );
});
Run Code Online (Sandbox Code Playgroud)

任何帮助深表感谢。

小智 11

您需要相应地调整DataTable的sDom属性: let table = $('#mytable').DataTable({sDom: 'lrtip'}); 应该隐藏搜索框而不禁用过滤功能。另外,您可能想查看有关该主题的官方参考文档。


Alo*_*ngh 9

数据表提供自定义选项来显示和隐藏元素以及自定义元素。我们可以使用dom值来自定义:

 l - length changing input control
    **f - filtering input**
    t - The table
    i - Table information summary
    p - pagination control
    r - processing display element

    **f is for filter , so we can remove it.**

        $('#example').dataTable( {
          "dom": 'lrtip'
        } );
Run Code Online (Sandbox Code Playgroud)