【问题标题】:How to use Editor from DataTables and call data from ajax如何使用 DataTables 中的编辑器并从 ajax 调用数据
【发布时间】:2016-12-08 17:53:32
【问题描述】:

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

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 }
    ]
  });
});

【问题讨论】:

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


    【解决方案1】:

    尝试编辑你的成功:函数。看看这是否有帮助。

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

    【讨论】:

    • 我已经尝试过了,但它仍然没有在页面上加载任何内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 2015-06-27
    • 2017-06-04
    相关资源
    最近更新 更多