【问题标题】:How to append JSON response output into html table using Javascript如何使用 Javascript 将 JSON 响应输出附加到 html 表中
【发布时间】:2017-10-31 21:56:13
【问题描述】:

我有这个响应输出(JSON 类型)--> 从控制器传递

[["Subash Nisam",test,"Medix Care","2017.03.02","9.30 am 到 10.30 am"],["Subash Nisam",test,"Medix Care","2017.03.02" ,"3.30 到 5.30"]]

我想使用 Javascript 将它附加到表 tbody 之后-->

<table class="table table-bordered table-hover" id="doctorresultTable">
    <thead>
        <tr>
            <th>Doctor Name</th>
            <th>Speciality</th>
            <th>Hospital</th>
            <th>Date</th>
            <th>Time</th>
        </tr>
    </thead>
    <tbody>

    </tbody>
</table>

我该怎么做?

这是我的 JS 代码:

function searchbyNameandDate(name, avadate) {
    var ajaxConfig = {
        type: "GET",
        url: "searchbyNameandDate?name=" + name + "&date=" + avadate,
        async: true,
        dataType: "json"
    };
    $.ajax(ajaxConfig)
            .done(function (response) {

                for (var i = 0; i < response.length; i++) {
                    for (var j = 0; j < 4; j++) {

                        var html = "<tr> \
                <td>" + response[i][j] + "</td> \
                <td>" + response[i][j] + "</td>\
                <td>" + response[i][j] + "</td> \
                <td>" + response[i][j] + "</td> \
                <td>" + response[i][j] + "</td> \
            </tr>";
                        $("#doctorresultTable tbody").append(html);
                    }

                }


            })
            .fail(function (xhr, status, errorMessage) {
                alert("failed to load data");
                console.log("XML HTTP REQUEST : " + xhr);
                console.log("Status : " + status);
                console.log("Error message : " + errorMessage);
            });
    }

}

【问题讨论】:

  • 我可以看看你的代码吗?到目前为止你尝试了什么
  • 我已经提交了我的js代码

标签: javascript html ajax html-table response


【解决方案1】:

我将箭头函数替换为常用函数。 现在它可以正常工作了。 -------------------------@Armen Vardanyan ->感谢您的帮助。

这是用常用函数替换箭头函数后的代码。

const data = [["Subash Nisam",'test',"Medix Care","2017.03.02","9.30 am to 10.30 am"],["Subash Nisam",'test',"Medix Care","2017.03.02","3.30 to 5.30"]];

var tableContent = document.querySelector('#doctorresultTable > tbody');
data.map(function(instance) {
 const row = document.createElement('tr');
 tableContent.appendChild(row);
 instance.map(function(info) {
  const cell = document.createElement('td');
  cell.innerText = info;
  row.appendChild(cell);
 });
});

【讨论】:

    【解决方案2】:

    使用纯 javascript (ES6):

    const data = [["Subash Nisam",'test',"Medix Care","2017.03.02","9.30 am to 10.30 am"],["Subash Nisam",'test',"Medix Care","2017.03.02","3.30 to 5.30"]];
    
    var tableContent = document.querySelector('#doctorresultTable > tbody');
    data.map(instance => {
      const row = document.createElement('tr');
      tableContent.appendChild(row);
      instance.map(info => {
        const cell = document.createElement('td');
        cell.innerText = info;
        row.appendChild(cell);
      });
    });
    <table class="table table-bordered table-hover" id="doctorresultTable">
                <thead
                    <tr>
                        <th>Doctor Name</th>
                        <th>Speciality</th>
                        <th>Hospital</th>
                        <th>Date</th>
                        <th>Time</th>
                    </tr>
                </thead>
                <tbody>
    
                </tbody>
            </table>

    虽然使用 Angular 或 React 之类的框架来呈现视图会更容易(如果您不需要构建整个应用程序,React 更可取)。

    【讨论】:

    • 成功了。你能给我推荐一个首选的教程或参考来提高我的 js 知识吗?
    • 使用您的代码运行时遇到问题。当它运行控制台输出显示这个--->意外的令牌>它给出错误在这里-->" data.map(instance = > {"
    • 您可以查看this,因为您的问题是指 DOM 操作。你也可以看看jQuery和React是如何操作DOM的,网上有很多教程
    • 你用什么浏览器?顺便说一句,这是 ES6,所以你可能需要一个转译器(比如 Babel),或者只是用常用函数替换箭头函数
    猜你喜欢
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 2021-10-10
    • 2022-01-08
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多