如何动态设置dataTable的Ajax URL?

phe*_*mix 12 ajax jquery datatables

我正在使用jQuery DataTables,我的JavaScript代码如下所示:

$(document).ready(function() {
   var tbl = $('#table_tabl').DataTable({
      responsive: true,
      "oLanguage": {
         "sUrl": "<?php  echo RP_LANG ?>fr_FR.txt",
      },
      "processing": true,
      "serverSide": true,
      ajax: "<?php  echo RP_SSP ?>server_processing_reservTables.php", // I want to add a parmeter to it dynamically when a select element is selected 
      "aoColumnDefs": [{
         "aTargets": [3],
         "mData": 3,
         "mRender": function(data, type, full) {
            return '<div style="text-align:center;"><a href="RestaurantReservation/reserverTable/' + data + '" title="R&eacute;server"><span class="mif-lock icon"></span></a></div>';
         }
      }],
      "aLengthMenu": [
         [10, 25, 50, 100, -1],
         [10, 25, 50, 100, "Tout"]
      ]
   });
});
Run Code Online (Sandbox Code Playgroud)

我想根据select元素的选定值过滤此dataTable:

$("#select_id").on("change", function(){
    // set the ajax option value of the dataTable here according to the select's value
});
Run Code Online (Sandbox Code Playgroud)

如何设置ajax的选项的值dataTableon_change的事件select基础上,选择的所选项目元素?

Gyr*_*com 14

解决方案1

使用ajax.url()API方法设置DataTables用于Ajax获取数据的URL.

$("#select_id").on("change", function(){
    // set the ajax option value of the dataTable here according to the select's value
    $('#table_tabl').DataTable()
       .ajax.url(
          "<?php  echo RP_SSP ?>server_processing_reservTables.php?param=" 
          + encodeURIComponent(this.value)
       )
       .load();
});
Run Code Online (Sandbox Code Playgroud)

解决方案2

使用ajax.data选项在Ajax请求时添加或修改提交给服务器的数据.

var tbl = $('#table_tabl').DataTable({
   // ... skipped other parameters ...
   ajax: {
      url: "<?php  echo RP_SSP ?>server_processing_reservTables.php",
      data: function(d){
         d.param = $('#select_id').val();
      }
   }
});
Run Code Online (Sandbox Code Playgroud)


phe*_*mix 6

我找到了 :

$("#salle_code").on("change", function(){
                tbl.ajax.url("<?php  echo RP_SSP ?>server_processing_reservTables.php?salle_code="+$(this).val()).load();
            });
Run Code Online (Sandbox Code Playgroud)