2017-04-06 141 views
0

我正在使用DataTables將數據加載到表中 - 數據量很大,因此我使用的服務器端處理效果很好。但是,我不確定如何將自定義參數傳遞給我的ajax調用。將自定義參數傳遞到datatables.js

如何做到這一點和多個參數(即不同的參數按鈕)?

這裏是我的代碼,迄今:

<script> 
$.fn.dataTable.ext.buttons.reload = { 
text: 'Reload', 
action: function (e, dt, node, config) { 
    dt.ajax.reload(); 
} 
}; 


    $(document).ready(function() { 
     $('#landing_pages').DataTable({ 
      dom: 'Blfrtip', 
      keys: true, 
      deferRender: true, 
      responsive: true, 
      "searching": false, 
      "lengthMenu": [[10, 25, 50, 500], [10, 25, 50, 500]], 
      buttons: [ 
       'copy', 'csv', 'excel', 'pdf', 'print', 'reload' 
      ], 
      "processing": true, 
      "serverSide": true, 
      "ajax":{'url': "/data-source/landing-pages/{{profile_id}}{{page_dim}}", "data": function (d) { 
      d.myKey = "myValue"; 
      // d.custom = $('#myInput').val(); 
      // etc 
     }} 

     }); 
    }); 
    </script> 

這是從這個例子基於: https://datatables.net/examples/server_side/custom_vars.html

回答

0

一種方法是摧毀數據表,然後用你想要的參數重新初始化。類似這樣的:

$.fn.dataTable.ext.buttons.reload = { 
text: 'Reload', 
action: function (e, dt, node, config) { 

    // Get the profile_id and page_dim values to pass in to the initializer 

    // Destroy the datatable 
    $("#MyTable").dataTable().fnDestroy(); 
    // reinitialize the datatable with the new call with the variables 
    InitializeTable(profile_id, page_dim); 
} 
}; 

function InitializeTable(profile_id, page_dim) { 

    var initTable = { 
     dom: 'Blfrtip', 
      keys: true, 
      deferRender: true, 
      responsive: true, 
      "searching": false, 
      "lengthMenu": [[10, 25, 50, 500], [10, 25, 50, 500]], 
      buttons: [ 
       'copy', 'csv', 'excel', 'pdf', 'print', 'reload' 
      ], 
      "processing": true, 
      "serverSide": true, 
      "ajax":{'url': "/data-source/landing-pages/" + profile_id + "/" + page_dim, "data": function (d) { 
      d.myKey = "myValue"; 
    }; 

    // Initialize the data table with the parameters above 
    $("#MyTable").dataTable(initTable); 
} 
+0

感謝您的幫助。那麼我如何爲多個人做這個? – Adders

+0

多重是什麼?表?對於表格,爲每個要初始化的表格創建一個函數,每個表格都帶有自己的ajax調用和參數 – Simon

+0

沒有參數,對不起,如果我遺漏了一些明顯的東西。我希望能夠通過動態參數 – Adders

相關問題