如何动态设置dataTable的Ajax URL?

How to set dynamically the Ajax URL of a dataTable?

我正在使用 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"]
      ]
   });
});

我想根据 select 元素的 selected 值过滤此数据表:

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

如何根据 select 的 select 元素的 on_change 事件中的 dataTable 设置 ajax 选项的值=26=]ed 项 ?

数据table版本:1.10.0-beta.1使用fnDraw重绘table。

使用 fndraw 的示例代码

$(document).ready(function() {
  var oTable = $('#example').dataTable();

  // Re-draw the table - you wouldn't want to do it here, but it's an example :-)
  oTable.fnDraw();
} );

Source

$(document).ready(function() {
   var tbl = $('#table_tabl').DataTable({
      responsive: true,
      "oLanguage": {
         "sUrl": "<?php  echo RP_LANG ?>fr_FR.txt",
      },
      "processing": true,
      "serverSide": true,
      "sAjaxSource": "<?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"]
      ]
   });

   $("#select_id").change(function () {
          var end = this.value;
          var NTypSource = '<?php  echo RP_SSP ?>server_processing_reservTables?type='+end+'';
          var oSettings = tbl.fnSettings();
          oSettings.sAjaxSource  = NTypSource;
          tbl.fnDraw();
   });

});

解决方案 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();
});

解决方案 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();
      }
   }
});

我找到了:

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