jquery - 如何使用 jquery 和 ajax 从数据库中的数据创建 HTML 表格

标签 jquery html mysql ajax

我正在尝试使用数据库中的信息显示表格。我可以看到我的 console.log 中显示的所有数据,但我不确定如何将其放入表中。

这是我的更新版本。它现在至少显示一个运算符(operator),但只显示一个运算符(operator),而不是所有运算符(operator)。我怎样才能让它显示所有运营商?

function displayDataTable(index){

allocationDataAjax = $.ajax({
    type: "POST",
    url: "./qry/getAllocationData.php",
    async: true,
    data: {ModelId: control.settings.modelId},
    success: function(result){
        allocationData = JSON.parse(result);

        for (i=0;i<allocationData.length;i++){
            //console.log(allocationData["SystemName"][i], allocationData["Operator"][i]);
            var operator = allocationData["SystemName"][i];
            console.log(operator);
        }

        $("#dataTableDiv").html(allocationData);
        $("#pageOverlay").empty();
            html = "<div id='dataTableDiv'><h4>Data Table</h4><table id='dataTable' class='table table-bordered table-striped dataTable' role='grid'><thead><tr role='row'><th>header 1</th><th>header 2</th><th>header 3</th><th>header 4</th><th>header 5</th><th>header 6</th></thead></tr><tbody><tr><td>" + operator + "</td></tr></tbody></table><input type='button' value='Close' onclick='closeOverlay()'>&nbsp;&nbsp;<input type='button' id='exportDataTable' value='Export Table'></div>";
        $("#pageOverlay").html(html);
        }
   });

最佳答案

这是我的答案,以防将来其他人遇到这个问题

function displayDataTable(index){

   allocationDataAjax = $.ajax({
      type: "POST",
      url: "./qry/getAllocationData.php",
      async: true,
      data: {ModelId: control.settings.modelId},
      success: function(result){
        allocationData = JSON.parse(result);

        $("#pageOverlay").empty();

        html = "<div id='dataTableDiv'><h4>Data Table</h4><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></thead></tr><tbody>";

        for (i=0;i<allocationData.length;i++){
            //console.log(allocationData);
            //console.log(allocationData["SystemName"][i], allocationData["Operator"][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><input type='button' value='Close' onclick='closeOverlay()'>&nbsp;&nbsp;<input type='button' id='exportDataTable' value='Export Table'></div>";

        $("#pageOverlay").html(html);
}

关于jquery - 如何使用 jquery 和 ajax 从数据库中的数据创建 HTML 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40642267/

相关文章:

jQuery 淡入淡出

html - 将 h 标签和 div 在同一封闭 div 中的同一水平水平对齐

html - 如何在这样的按钮中添加阴影?

基于表单答案的 Javascript 计算

php - 如何用PHP连接MySQL(WAMP)数据库,并处理错误?

php - 非法字符串 PDO fetch() 与 fetchAll()

javascript - 使用 JQuery map 更新中心和缩放

javascript - td 到表格顶部的距离(在可滚动的 div 内)?

ajax - MVC3 和 JQuery 对话框打开对话框时不会提交表单

PHP MYSQL Foreach 日期列表