2015-11-06 114 views
7

加載jQuery數據表時,我有如下所示的代碼。如何將其他參數傳遞給AJAX調用?下面的問題和答案中建議的fnServerParams回調不起作用。也就是說,天真地使用aodata.push()導致「push is undefined」(因爲aodata的確不是數組)。那麼做到這一點的正確方法是什麼?如何將其他參數傳遞給jQuery DataTable ajax調用?

相關問題:

代碼:

self.dataTable = self.dataTableContainer.DataTable({ 
      "autoWidth": false, 
      "bSort": false, 
      "displayStart": 0, 
      "paging": false, 
      "lengthChange": false, 
      "processing": true, 
      "serverSide": true, 
      "dom": "<'dataTables_header dashboard_alert_history__alertHeader'i>", 
      "ajax": { 
       url: getDataUri, 
       error: onError, 
       cache: false, 
       "fnDrawCallback": onTableDrawn, 
      }, 
      "fnDrawCallback": onTableDrawn, 
      "language": { 
       "info": resources.alarmHistory, 
       "infoEmpty": resources.alarmHistory, 
       "infoFiltered": '' 
      }, 
      "columns": [ 
       { 
        "data": "timestamp", 
        "mRender": function (data) { 
         return IoTApp.Helpers.Dates.localizeDate(data, 'L LTS'); 
        }, 
        "name": "timestamp" 
       }, 
       { 
        "data": "deviceId", 
        "mRender": function (data) { 
         return htmlEncode(data); 
        }, 
        "name": "deviceId" 
       }, 
       { 
        "data": "ruleOutput", 
        "mRender": function (data) { 
         return htmlEncode(data); 
        }, 
        "name": "ruleOutput" 
       }, 
       { 
        "data": "value", 
        "mRender": function (data) { 
         return htmlEncode(IoTApp.Helpers.Numbers.localizeFromInvariant(data)); 
        }, 
        "name": "value" 
       }, 
      ], 
      "columnDefs": [ 
       { 
        "targets": [0, 1, 2, 3], 
        "className": 'table_alertHistory_issueType', 
        "width": "20%" 

       } 
      ], 
     }); 

回答

13

我忽略RTF M.對於版本1.9和更早版本,fnServerParams回調現在爲legacy。在最新版本的DataTable中,您可以使用ajax數據參數as described in the DataTables documentation。在下面的例子中,附加mykeyd對象會做的伎倆:

$(document).ready(function() { 
    $('#example').DataTable({ 
     "processing": true, 
     "serverSide": true, 
     "ajax": { 
      "url": "scripts/server_processing.php", 
      "data": function (d) { 
       d.myKey = "myValue"; 
       // d.custom = $('#myInput').val(); 
       // etc 
      } 
     } 
    }); 
}); 
+0

謝謝你,幫我在正確的運動.. –

+1

@EdathadanChiefakaArun我相信你的意思是「時刻」,雖然一個很好的「運動「對憲法有幫助! – Brett

相關問題