Skip to content

Instantly share code, notes, and snippets.

@Octagon-simon
Created June 4, 2022 13:10
Show Gist options
  • Select an option

  • Save Octagon-simon/a28fb4216bcbdaa0f95239323698c9d9 to your computer and use it in GitHub Desktop.

Select an option

Save Octagon-simon/a28fb4216bcbdaa0f95239323698c9d9 to your computer and use it in GitHub Desktop.
//function to build table
function buildTable(matchingIDs){
//reset table data
tableDataElem.innerHTML = "";
//check if parameter is provided
if(typeof matchingIDs === "undefined"){
//our loop index
let ind = 0;
//our table index
let tableInd = 1;
//loop through table object
while(ind < Object.keys(tableData).length){
//append data using a template literal
tableDataElem.innerHTML += `
<tr>
<td>${tableInd}</td>
<td>${tableData[tableInd][0]}</td>
<td>${tableData[tableInd][1]}</td>
<td>${tableData[tableInd][2]}</td>
<td>${tableData[tableInd][3]}</td>
</tr>
`;
//increment loop index
ind++;
//increment table index
tableInd++;
}
}else if(matchingIDs.length !== 0){ //check if matchingIDs array is not empty
//our loop index
let ind = 0;
//our table index
let tableInd = 1;
//loop through matching IDs provided
while(ind < matchingIDs.length){
//append data by getting ID of the record and using a template literal
tableDataElem.innerHTML+=`
<tr>
<td>${tableInd}</td>
<td>${tableData[matchingIDs[ind]][0]}</td>
<td>${tableData[matchingIDs[ind]][1]}</td>
<td>${tableData[matchingIDs[ind]][2]}</td>
<td>${tableData[matchingIDs[ind]][3]}</td>
</tr>
`;
//increment loop index
ind++;
//increment table index
tableInd++;
}
}else{
tableDataElem.innerHTML+=`
<tr>
<td colspan="5">
NO DATA FOUND
</td>
</tr>
`;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment