Showing posts with label JSON. Show all posts
Showing posts with label JSON. Show all posts

Tuesday, March 9, 2010

Dynamic table creation using JSON objects.

Hi,
Here is an example for creating dyanmic table using JSON objects.

script type="text/javascript"

// Adding the rows to tbody. From the html we will call the below function.
function displayRows(jsonString){
var tableBody = document.getElementById("QuestionBody");
// Clears the table row contents initally.
while (tableBody.firstChild) {
tableBody.removeChild(tableBody.firstChild);
}
// Fetches the size of the JSON object and creates that many rows.
for(i=0; ivar row = document.createElement('tr');
createTableData(row, jsonString[i]);
tableBody.appendChild(row);
}
document.getElementById("QuestionBody").appendChild(tableBody);
jQuery('#QuestionTable').show();
jQuery('table').Scrollable(100, 800);
}

function createTableData(rowObject, data){
createTableRowContent(rowObject, data, 'td');
}
// Adding the actuall data content to the td.
function createTableRowContent(rowObject, data, cellType){
var rowContent = document.createElement(cellType);
var cell = document.createTextNode(data);
rowContent.appendChild(cell);
rowObject.appendChild(rowContent);
}
/script