【问题标题】:How to set dynamically the Ajax URL of a dataTable?如何动态设置dataTable的Ajax URL?
【发布时间】:2015-11-10 00:49:07
【问题描述】:

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

如何根据select的选中项在select元素的on_change事件中设置dataTableajax选项值?

【问题讨论】:

    标签: jquery ajax datatables


    【解决方案1】:

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

    【讨论】:

    • 在解决方案2中:数据将是postget
    • @pheromix,默认为GET请求,但您可以使用type选项更改它,例如ajax: { url: 'script.php', type: 'POST' }
    【解决方案2】:

    我找到了:

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

    【讨论】:

      【解决方案3】:

      数据表版本:1.10.0-beta.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();
      } );
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-13
        • 2016-07-08
        • 2014-12-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多