Datatables - Search Box outside datatable

前端 未结 11 1294
攒了一身酷
攒了一身酷 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:20

    As per @lvkz comment :

    if you are using datatable with uppercase d .DataTable() ( this will return a Datatable API object ) use this :

     oTable.search($(this).val()).draw() ;
    

    which is @netbrain answer.

    if you are using datatable with lowercase d .dataTable() ( this will return a jquery object ) use this :

     oTable.fnFilter($(this).val());
    
    0 讨论(0)
  • 2020-11-30 17:20

    More recent versions have a different syntax:

    var table = $('#example').DataTable();
    
    // #myInput is a <input type="text"> element
    $('#myInput').on('keyup change', function () {
        table.search(this.value).draw();
    });
    

    Note that this example uses the variable table assigned when datatables is first initialised. If you don't have this variable available, simply use:

    var table = $('#example').dataTable().api();
    
    // #myInput is a <input type="text"> element
    $('#myInput').on('keyup change', function () {
        table.search(this.value).draw();
    });
    

    Since: DataTables 1.10

    – Source: https://datatables.net/reference/api/search()

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

    This should be work for you:(DataTables 1.10.7)

    oTable = $('#myTable').dataTable();
    
    $('#myInputTextField').on('keyup change', function(){
      oTable.api().search($(this).val()).draw();
    })
    

    or

    oTable = $('#myTable').DataTable();
    
    $('#myInputTextField').on('keyup change', function(){
      oTable.search($(this).val()).draw();
    })
    
    0 讨论(0)
  • 2020-11-30 17:35
    $('#example').DataTable({
       "bProcessing": true,
       "bServerSide": true,
       "sAjaxSource": "../admin/ajax/loadtransajax.php",
       "fnServerParams": function (aoData) {
            // Initialize your variables here
            // I have assign the textbox value for "text_min_val"
            var min_val = $("#min").val();  //push to the aoData
            aoData.push({name: "text_min_val", value:min_val});
       },
       "fnCreatedRow": function (nRow, aData, iDataIndex) {
           $(nRow).attr('id', 'tr_' + aData[0]);
           $(nRow).attr('name', 'tr_' + aData[0]);
           $(nRow).attr('min', 'tr_' + aData[0]); 
           $(nRow).attr('max', 'tr_' + aData[0]); 
       }
    });
    

    In loadtransajax.php you may receive the get value:

    if ($_GET['text_min_val']){
        $sWhere = "WHERE ("; 
        $sWhere .= " t_group_no LIKE '%" . mysql_real_escape_string($_GET['text_min_val']) . "%' ";
        $sWhere .= ')';
    }
    
    0 讨论(0)
  • 2020-11-30 17:36

    This one helped me for DataTables Version 1.10.4, because its new API

    var oTable = $('#myTable').DataTable();    
    $('#myInputTextField').keyup(function(){
       oTable.search( $(this).val() ).draw();
    })
    
    0 讨论(0)
  • 2020-11-30 17:41

    I want to add one more thing to the @netbrain's answer relevant in case you use server-side processing (see serverSide option).

    Query throttling performed by default by datatables (see searchDelay option) does not apply to the .search() API call. You can get it back by using $.fn.dataTable.util.throttle() in the following way:

    var table = $('#myTable').DataTable();
    var search = $.fn.dataTable.util.throttle(
        function(val) {
            table.search(val).draw();
        },
        400  // Search delay in ms
    );
    
    $('#mySearchBox').keyup(function() {
        search(this.value);
    });
    
    0 讨论(0)
提交回复
热议问题