Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Dynamically Generated Table

//Creating the table
function CreateTableFromJSON() {
	console.log('CreateTableFromJSON invoked');
	var ObjTableData = OBJECT_GLOBAL;
	console.log(ObjTableData);

	var col = [];//create an empty array
	//get the number of records, and then get and array of the field names in order to build the table header
	for (var i = 0; i < ObjTableData.length; i++) {
		for (var key in ObjTableData[i]) {
			if (col.indexOf(key) === -1) {
				col.push(key);
			}
		}
	}
	//create an element in which to put the table. This is done dynamically
	var table = document.createElement('table');
	var tr = table.insertRow(-1); // TABLE ROW.
	//actually build the table header
	for (var i = 0; i < col.length; i++) {
		var th = document.createElement('th'); // TABLE HEADER.
		th.innerHTML = col[i];
		tr.appendChild(th);
	}
	// ADD JSON DATA TO THE TABLE AS ROWS.
	for (var i = 0; i < ObjTableData.length; i++) {
		tr = table.insertRow(-1);
		for (var j = 0; j < col.length; j++) {
			var tabCell = tr.insertCell(-1);

			tabCell.innerHTML = ObjTableData[i][col[j]];
		}
	}
	// FINALLY ADD THE NEWLY CREATED TABLE WITH JSON DATA TO A CONTAINER.
	var divContainer = document.getElementById('showData');
	divContainer.innerHTML = '';
	divContainer.appendChild(table);
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: tour-app-api 
Javascript :: javascript unique id 
Javascript :: mongoose post new document 
Javascript :: Add Navbar to React Redux CRUD App 
Javascript :: javascript get each element count / occurrences / frequency from a list 
Javascript :: Working with substring 
Javascript :: change string to object in html 
Javascript :: iterating over an array 
Javascript :: empty donut chart chart js 
Javascript :: go back to screen with params react native 
Javascript :: fetch image from cloudinary in nodejs 
Javascript :: js import 
Javascript :: Creates an Express application 
Javascript :: mongoose schema aggregation lookup multiple collections 
Javascript :: GetNameOfZone 
Javascript :: Uncaught TypeError: $(...).steps is not a function 
Javascript :: Mandatory Parameter Shorthand javascript 
Javascript :: different way to for loop js 
Javascript :: simple promise 
Javascript :: apollo client with functional component 
Javascript :: Your task is to take every letter and its index and form a string out of them. javascript 
Javascript :: GET_FORM-VALUE 
Javascript :: complicated reduce 
Javascript :: remove all special characters online 
Javascript :: ES2022 - Top-level await modules 
Javascript :: upload image to server react next 
Javascript :: change teh value of a slider p5js 
Javascript :: console.dir vs console.log 
Javascript :: Using toLocaleString() to Print JavaScript Number Format with Commas 
Javascript :: javascript extrsct object 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =