I have this response output (type JSON)--> pass from controller
[["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"]]
I want to append it following table tbody using Javascript-->
<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>
How can I do it?
Here is my JS code:
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);
});
}
}
Using plain 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>
Though it would be easier to use some framework like Angular or React for rendering the view (React is preferable if you don't need to build an entire application).
I replace the arrow functions with usual functions. Now it's work properly. -------------------------@Armen Vardanyan ->Thanks for your help.
Here is the code after replace the arrow functions with usual functions.
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);
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.