如何将数据数组/对象推送到数据表中的Fnserverparams?

时间:2019-07-06 18:46:52

标签: javascript jquery filter datatables

我想在过滤后推送数据并替换现有数据。 该文档令人困惑: doc1 doc2

我尝试使用fnServerParams,但是它只推送数据并希望它是动态的。

这是我的代码

<form id="form_filter">
    <div class="col-lg-2 col-md-2 col-sm-12 col-xs-12"> 
        <div class="form-group ">
            <label>Start Date <span class="required">*</span></label>
            <input type="text" name="start_date" id="start_date" class="form-control datepicker_bottom" >
        </div>
    </div>
    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">  
        <div class="form-group ">
            <label>Layanan </label>
            <select name="service" id="service" class="form-control" >
              <option value="">All Service</option>
    <option value="Service_1">Service 1</option>
    <option value="Service_2">Service 2</option>
    <option value="Service_3">Service 3</option>
          </select>
        </div>
    </div> 
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"> 
        <p align="right">
        <button type="button" onclick="filter_data()"  class="btn btn-info"><i class="fa fa-search"></i> Search</button>
            &nbsp;
        <button type="reset"   class="btn btn-danger"><i class="fa fa-eraser"></i> Reset</button>
        </p>
    </div>
</form>

<table class="table datatable">
    <thead>
        <tr>
            <th><center>Date</center></th>
            <th><center>Service</center></th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
<script>
function filter_data(){
    var start_date  = $("#start_date").val();
    var end_date    =  $("#end_date").val();
    var service     =  $("#service").val();

    // i want to 3 variable push to aoData.push in fnServerParams datatables

}
dTable = $('.datatable').DataTable( {
"bProcessing": true,
"bServerSide": true,
"bJQueryUI": false,
"responsive": false, 
"sAjaxSource": "data.php", 
"sServerMethod": "POST",
"scrollX": true,
"fnServerParams": function ( aoData ) {
  aoData.push( 
                { "name": "test1", "value": "wow" } ,     // How to push data in here, dynamic
                { "name": "test2", "value": "wew" }   // How to push data in here, dynamic
    );
},
"columnDefs": [
{ "orderable": true,  "targets": 0, "searchable": true},
{ "orderable": true,   "targets": 1, "searchable": true }
]
} );
</script>

0 个答案:

没有答案