提问者:小点点

如何动态设置数据表的Ajax URL?


我正在使用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_id").on("change", function(){
    // set the ajax option value of the dataTable here according to the select's value
});

如何设置的ajax选项的值的dataTable中的on_change事件的选择元素基于选择的项目?


共3个答案

匿名用户

使用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();
});

使用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();
            });

匿名用户

数据表版本:1.10。0-β。1使用fnDraw重新绘制表格。

使用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();
} );

来源

$(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();
   });

});