2016-11-17 195 views
0

jquery.dataTables.min.js:數據表1.10.12數據表:JSON是在initComplete未定義()

我需要的數據已經被加載後,可以訪問表中的行(deferred)。而我不能因爲jsonundefined對於我initComplete裏面的功能。儘管我的表已加載,並且我看到了所有數據。此外,函數內部還有settings數據。

這是爲什麼?我忘了一些選擇嗎?

我的代碼:

var data_table = task_submit_table.DataTable({ 
    "initComplete": function (settings, json) { 
     console.log(json); 
    }, 
    "processing": true, 
    "serverSide": true, 
    "deferRender": true, 
    "deferLoading": 0, 
    "ordering": true, 
    "order": [[ 0, "desc" ]], 
    "ajax": { 
     "url": "get_task_list/", 
     "type": "POST", 
     "dataType": "json" 
    }, 
    "columns": [ 
     {"title": "Id", "data": "id"}, 
     {"title": "Date", "data": "date"}, 
     {"title": "Project Id", "data": "project_id"}, 
     {"title": "Project Name", "data": "project_name"}, 
     {"title": "project", "data": "biobank_project"}, 
     {"title": "#Hashes", "data": "nhashes"}, 
     {"title": "#Success", "data": "nsuccess"}, 
     {"title": "#Fail", "data": "nfail"}, 
     {"title": "Status", "data": "status"}, 
     {"title": "Report", "data": null}, 
     {"title": "", "data": null}, 
     {"title": "", "data": null} 
    ], 
    "columnDefs": [ 
     { 
     "targets": [0], 
     "visible": false, 
     "searchable": true 
     }, 
     { 
     "targets": [2], 
     "visible": false, 
     "searchable": true 
     }, 
     { 
     "targets": -3, 
     "data": null, 
     "defaultContent": "<form id='tool-export' method='post' action='export/'>"+ 
      "<a href='#' id='export' class='btn btn-default export-link'>export</a></form>" 
     }, 
     { 
     "targets": -2, 
     "data": null, 
     "defaultContent": "<a href='#' id='task-delete' class='btn btn-default task-delete-link'"+ 
      "data-toggle='modal' data-target='#confirm_modal'>delete</a>" 
     }, 
     { 
     "targets": -1, 
     "data": null, 
     "defaultContent": "<a href='#' id='task-restart' class='btn btn-default task-restart-link'"+ 
      "data-toggle='modal' data-target='#confirm_modal'>restart</a>" 
     } 
    ], 
    "dom": "<\"dt-btn-floatLeft\"l><\"dt-btn-floatRight\"B><\"dt-btn-clear\">rtip", 
    "buttons": [ 
     { 
     "title": "Refresh", 
     "text": "Refresh", 
     "action": function() { 
      data_table.draw(); 
     } 
     } 
    ] 
    }); 
    data_table.draw(); 

在前端的表看起來像這樣: enter image description here

+0

嘗試將''initComplete''改爲''fnInitComplete「' –

+0

沒有成功。有設置數據,但json仍未定義。 – trex

回答

0

您可能會希望使用drawCallback函數,該函數在您的表格每次完全繪製時都會執行。

因此,這將是這樣的:

"drawCallback": function(settings) { 
    // You use this API object to get the data from the displayed data set. 
    var api = this.api(); 
} 

這drawCallback將執行每個網格重繪等時間:排序,過濾,分頁,ajax.reload()等。當作爲initComplete功能我相信只會發生一次。 https://datatables.net/reference/option/drawCallback