Dynamic Table Generator


/ Published in: JavaScript
Save to your folder(s)

The use of dynamic data acquisition on modern web sites and in a lot of intranet applications, sometimes means we have to build structures dynamically as data comes in from a feed or AJAX operations. The problem is not with the data, but rather that it is unstructured, or not appropriately tagged. The solution is to dynamically build the necessary structures to make the data presentable in a web page


Copy this code and paste it in your HTML
  1. function buildTable()
  2. {
  3. tabobj = document.createElement("table");
  4. tabobj.width="80%";
  5. tabobj.style.backgroundColor = "khaki";
  6. tabobj.style="margin:auto";
  7. tabobj.style="border-radius:10px";
  8. thead = document.createElement("thead");
  9. tabobj.appendChild(thead);
  10. row = document.createElement("tr");
  11. thead.appendChild(row);
  12. for (i=0; i< 4; i++)
  13. {
  14. tHdg = document.createElement("th");
  15. HdgName = 'Heading' + (i+1);
  16. tHdg.innerHTML=HdgName;
  17. row.appendChild(tHdg);
  18. }
  19. tbody=document.createElement("tbody");
  20. tabobj.appendChild(tbody);
  21. for (i = 0; i < 5; i++)
  22. {
  23. row = document.createElement("tr");
  24. row.setAttribute("id", "tr" + i);
  25. for (j = 0; j < 4; j++)
  26. {
  27. td = document.createElement("td");
  28. td.className='cell';
  29. tContent = "row" +i + " cell" +j;
  30. td.innerHTML=tContent;
  31. row.appendChild(td);
  32. }
  33. tbody.appendChild(row);
  34. }
  35. main=document.getElementById('container');
  36. main.appendChild(tabobj);
  37. }

URL: http://coboldinosaur.com/pages/Dynamically_Generated_Tables.html

Report this snippet


Comments


You need to login to view comments.