如何使用 Javascript 将 JSON 响应输出附加到 html table
How to append JSON response output into html table using Javascript
我有这个响应输出(类型JSON)--> 从控制器传递
[["Subash Nisam",test,"Medix Care","2017.03.02","上午 9 点 30 分到 10 点 30 分"],["Subash Nisam",测试,"Medix Care" "2017.03.02","3.30 到 5.30"]]
我想使用 Javascript-->
将它附加到 table 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);
});
}
}
使用普通 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)。
我将箭头函数替换为常用函数。
现在可以正常工作了。
--------------------------@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);
});
});
我有这个响应输出(类型JSON)--> 从控制器传递
[["Subash Nisam",test,"Medix Care","2017.03.02","上午 9 点 30 分到 10 点 30 分"],["Subash Nisam",测试,"Medix Care" "2017.03.02","3.30 到 5.30"]]
我想使用 Javascript-->
将它附加到 table 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);
});
}
}
使用普通 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)。
我将箭头函数替换为常用函数。 现在可以正常工作了。 --------------------------@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);
});
});