Datatables - Search Box outside datatable

前端 未结 11 1295
攒了一身酷
攒了一身酷 2020-11-30 17:08

I\'m using DataTables (datatables.net) and I would like my search box to be outside of the table (for example in my header div).

Is this possible ?

相关标签:
11条回答
  • 2020-11-30 17:42

    You can use the sDom option for this.

    Default with search input in its own div:

    sDom: '<"search-box"r>lftip'
    

    If you use jQuery UI (bjQueryUI set to true):

    sDom: '<"search-box"r><"H"lf>t<"F"ip>'
    

    The above will put the search/filtering input element into it's own div with a class named search-box that is outside of the actual table.

    Even though it uses its special shorthand syntax it can actually take any HTML you throw at it.

    0 讨论(0)
  • 2020-11-30 17:44

    I had the same problem.

    I tried all alternatives posted, but no work, I used a way that is not right but it worked perfectly.

    Example search input

    <input id="serachInput" type="text"> 
    

    the jquery code

    $('#listingData').dataTable({
      responsive: true,
      "bFilter": true // show search input
    });
    $("#listingData_filter").addClass("hidden"); // hidden search input
    
    $("#serachInput").on("input", function (e) {
       e.preventDefault();
       $('#listingData').DataTable().search($(this).val()).draw();
    });
    
    0 讨论(0)
  • 2020-11-30 17:44

    You could move the div when the table is drawn using the fnDrawCallback function.

        $("#myTable").dataTable({
        "fnDrawCallback": function (oSettings) {
            $(".dataTables_filter").each(function () {
                $(this).appendTo($(this).parent().siblings(".panel-body"));
            });
        }
    });
    
    0 讨论(0)
  • 2020-11-30 17:47

    You can use the DataTables api to filter the table. So all you need is your own input field with a keyup event that triggers the filter function to DataTables. With css or jquery you can hide/remove the existing search input field. Or maybe DataTables has a setting to remove/not-include it.

    Checkout the Datatables API documentation on this.

    Example:

    HTML

    <input type="text" id="myInputTextField">
    

    JS

    oTable = $('#myTable').DataTable();   //pay attention to capital D, which is mandatory to retrieve "api" datatables' object, as @Lionel said
    $('#myInputTextField').keyup(function(){
          oTable.search($(this).val()).draw() ;
    })
    
    0 讨论(0)
  • 2020-11-30 17:47

    If you are using JQuery dataTable so you need to just add "bFilter":true. This will display default search box outside table and its works dynamically..as per expected

    $("#archivedAssignments").dataTable({
                    "sPaginationType": "full_numbers",
                    "bFilter":true,
                    "sPageFirst": false,
                    "sPageLast": false,
                    "oLanguage": {
                    "oPaginate": {
                        "sPrevious": "<< previous",
                        "sNext" : "Next >>",
                        "sFirst": "<<",
                        "sLast": ">>"
                        }
                    },
                "bJQueryUI": false,
                "bLengthChange": false,
                "bInfo":false,
                "bSortable":true
            });    
    
    0 讨论(0)
提交回复
热议问题