2016-11-24 116 views
1

我正在使用以下函數從服務器中加載DataTables表中的數據。我想重新加載與不同的參數在一個點擊事件表,我不能解決如何做到這一點。如果我調用reload它只是重新加載原始參數,如果我重新初始化整個表,它會拋出一個錯誤,因爲表已經存在。dataTables更改ajax調用數據

我一直在尋找到fnServerParams,但不能工作了,如果這將有助於與否。

如果任何人都可以指出我的正確方向,那就太好了。

function LoadRiskProfileModalTable(userId, teamId, riskProfileClass) { 

var params = { 
    userId: userId, 
    teamId: teamId, 
    riskProfileClass: riskProfileClass 
}; 

var data = JSON.stringify(params); 
//if (!$.fn.DataTable.isDataTable("#riskProfileTable")) { 

    var table = $("#riskProfileTable").DataTable({ 
     "bProcessing": true, 
     "sAjaxSource": "WFMHome.aspx/GetRiskProfileDrillThroughDatatable", 
     "fnServerData": function (sSource, aoData, fnCallback) { 
      //aoData.push(JSON.stringify(params)); 
      $.ajax({ 
       "dataType": 'json', 
       "contentType": "application/json; charset=utf-8", 
       "type": "POST", 
       "url": sSource, 
       "data": data, 
       "success": function (msg) { 
        var json = jQuery.parseJSON(msg.d); 
        fnCallback(json); 
        $("#riskProfileTable").show("fast", function() { $('#riskProfileTableLoading').hide("fast") }); 
       }, 
       error: function (xhr, textStatus, error) { 
        if (typeof console == "object") { 
         appendAlert("errorAlertPlaceholder", xhr.responseText, 1, "danger"); 
         console.log(xhr.status + "," + xhr.responseText + "," + textStatus + "," + error); 
        } 
       } 
      }); 
     }, 
     "columns": [ 
      { "data": "CaseHandler" }, 
      { "data": "caseAreaText" }, 
      { "data": "RiskProfileText" }, 
      { "data": "PassChecks" }, 
      { "data": "F1Checks" }, 
      { "data": "F2Checks" }, 
      { "data": "F3Checks" }, 
      { "data": "CurrentChecks" } 
     ] 
    }); 
//} 
//else { 
//  $('#riskProfileTable').DataTable().ajax.reload(); 
// } 
}; 
+0

目前我已經解決了我的眼前問題,刪除和追加函數中的表格定義。我想要一個更優雅的解決方案。 – ZedZim

回答

1

如果你只是想取代你現在從服務器的數據有數據,試圖通過更換成功的方法:通過破壞

"success": function (msg) { 
    var json = jQuery.parseJSON(msg.d); //I assume it's the data set 
    var table = $("#riskProfileTable").DataTable(); 
    table.rows.clear(); //clear the current data 
    table.rows.add(json).draw(); 
    $("#riskProfileTable").show("fast", function() { $('#riskProfileTableLoading').hide("fast") }); 
},