2017-04-06 3 views
0

私はDataTableを使用してテーブルにデータをロードしています。データが大量であるため、サーバ側の処理がうまく機能しています。しかし、私は自分の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

関連する問題