I would like to build dynamic table using JQuery and then append the table to DOM. Usually I used string concatenation method to build the table. This time I would like to do this with JQuery. One thing that I'm struggling to get is opening/closing tr
element while looping over multiple rows. Here is example if my code:
var data = {
"1": {
"fname": "Jon",
"lname": "Wayne",
"dob": "05/14/1987",
"status": "Active",
"color": "Red",
"team": "Miami"
},
"2": {
"fname": "Tim",
"lname": "Ryan",
"dob": "01/23/1967",
"status": "Inactive",
"color": "Blue",
"team": "Chicago"
},
"3": {
"fname": "Jim",
"lname": "Carey",
"dob": "11/02/1997",
"status": "Suspended",
"color": "Yellow",
"team": "Denver"
},
"4": {
"fname": "Chuck",
"lname": "Norris",
"dob": "09/06/1945",
"status": "Active",
"color": "Green",
"team": "Boston"
}
}
$('#start').on('click', showRecords);
function showRecords() {
displayData(1,data);
}
function displayData(fnid,data) {
var tbl = $('<table>').addClass('display').prop('id','data_tbl'),
thead = $('<thead>'),
tbody = $('<tbody>'),
tr = $('<tr>');
title = ['First Name','Last Name','DOB','Status','Color','Team'];
/*** Start: Table Header ***/
thead.append('<tr>');
for(var i=0; i < title.length; i++) {
if(fnid == 1){
if(i <= 3) {
thead.append($('<th>').text(title[i]));
}
}else{
thead.append($('<th>').text(title[i]));
}
}
thead.append('</tr>');
/*** End: Table Header ***/
/*** Start: Table Body ***/
for(key in data) {
tbody.append('<tr>');
tbody.append($('<td>').text(data[key].fname));
tbody.append($('<td>').text(data[key].lname));
tbody.append($('<td>').text(data[key].dob));
tbody.append($('<td>').text(data[key].status));
if(fnid !== 1) {
tbody.append($('<td>').text(data[key].color));
tbody.append($('<td>').text(data[key].team));
}
tbody.append('</tr>');
}
/*** End: Table Body ***/
tbl.append(thead); // Append header section to table.
tbl.append(tbody); // Append body section to table.
$("#container").empty().append(tbl);
}
.display {
width: 500px;
background-color: red;
}
.display,
.display th,
.display td{
border: 1px solid blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="button" name="start" id="start" value="Start" />
<div id="container"></div>
At first look you would think that everything is fine with the code, but if you open your dev tools and inspect the table elements you will find the issues. There are tr
elements in thead
and tbody
that are empty. These elements should not be there. Can anyone tell me how I can open/close the tr element in JQuery and build the table properly? Thank you.