如何使用DataTables中的Editor并从ajax调用数据

时间:2016-12-08 17:53:33

标签: javascript jquery ajax datatables jquery-datatables-editor

我有一个构建表格的大量信息数据库。我想要做的是通过ajax调用加载数据,然后能够编辑表中的数据。但我无法将数据显示在页面上。我在另一个界面中使用DataTables,加载,排序和其他很酷的功能都可以工作。我之前没有使用过编辑器而且我有点困惑。

function drawDataTable(){
   var len = allocationData.length;
     html = "<div id='dataTableDiv'><table id='dataTable' class='table table-bordered table-striped dataTable' role='grid'><thead><tr role='row'><th>System Name</th><th>Gatherer</th><th>Operator</th><th>County</th><th>Sample Date</th><th>Daily Avg</th></tr></thead><tbody>";
   for (i=0;i<len;i++){
     html += "<tr><td>" + allocationData['SystemName'][i] + "</td><td>" + allocationData['Gatherer'][i] + "</td><td>" + allocationData['Operator'][i] + "</td><td>" + allocationData['County'][i] + "</td><td>" + allocationData['SampleDate'][i] + "</td><td>" + allocationData['DailyAvg'][i] + "</td></tr>";

   }
   html += "</tbody></table></div>";

  $(".table-responsive").html(html);
}

$(function(){
  editor = new $.fn.dataTable.Editor( {
    "ajax": "qry/dataTable.php",
    "table": "#dataTable",
    "fields":[{
        "name": "SystemName"
    },{
        "name": "Gatherer"
    },{
        "name": "Operator"
    },{
        "name": "County"
    },{
        "name": "SampleDate"
    },{
        "name": "DailyAvg"
    }]
});

$('#dataTable').DataTable({
    dom: "Bfrtip",
    ajax: {
        type: "POST",
        url: "qry/dataTable.php",
        success: function(){
            drawDataTable();
        }
    },
    serverSide: true,
   columns: [
        {data: "SystemName"},
        {data: "Gatherer"},
        {data: "Operator"},
        {data: "County"},
        {data: "SampleDate"},
        {data: "DailyAvg"}
    ],
    select: true,
    buttons: [
        { extend: "create", editor: editor },
        { extend: "edit",   editor: editor },
        { extend: "remove", editor: editor }
    ]
  });
});

1 个答案:

答案 0 :(得分:2)

尝试编辑您的成功:功能。看看这是否有帮助。

        success: function(result){
            allocationData = JSON.parse(result);
            drawDataTable();
         }