Skip to main content

Dynamically Generate Table Using jQuery


In this code we will tackle about Dynamically Generate Table using jQuery. The program will let you create your own table dynamically. The code use a jQuery plugin that shorten your native scriptin by allowing you to call a single line of code. To learn more about this, just follow the steps below.

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

And this is the link for the jquery that i used in this tutorial https://jquery.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.

  1. <!DOCTYPE html>
  2. <html lang = "en">
  3. <head>
  4. <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css"/>
  5. <meta charset = "UTF-8" name = "viewport" content = "width=device-width"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <div class="navbar-header">
  10. <a class="navbar-brand" href="http://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </div>
  13. </nav>
  14. <div class="row">
  15. <div class="col-md-3"></div>
  16. <div class="col-md-6 well">
  17. <h3 class="text-primary">Dynamically Generate Table Using jQuery</h3>
  18. <hr style="border-top:1px dotted #ccc;"/>
  19. <div id="tbl_data">
  20. <div class="form-inline">
  21. <label style = "font-size:18px;" class = "text-info">Table header column(max = 5)</label>
  22. <input type = "number" min ="0" max = "5" id = "h_cols" class = "form-control"/>
  23. <button type = "button" id = "btn_tblhead" class = "btn btn-success form-control" ><span class = "glyphicon glyphicon-plus"></span></button>
  24. </div>
  25. </div>
  26. <br />
  27. <div id ="table table-responsive">
  28.  
  29. </div>
  30. </div>
  31. </div>
  32. </body>
  33. <script src = "js/jquery-3.1.1.js"></script>
  34. <script src = "js/script.js"></script>
  35. </html>

Creating the Script

This code contains the script of the application. The code can generate a HTML table by submitting a form. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.

  1. $(document).ready(function(){
  2. $(document).on('click', '#btn_tblhead', function(){
  3. if( $('#h_cols').val() == ""){
  4. alert("Please enter something!");
  5. }else{
  6. if($('#h_cols').val() > 5){
  7. alert("Must not exceed 5");
  8. }else{
  9. if($('#h_cols').val() == 0){
  10. alert("Please enter a valid number");
  11. }else{
  12. $h_cols = $('#h_cols').val();
  13. $('#tbl_data').empty();
  14. $loader = $('<center>Generating <img src = "images/default.gif" height = "50"></center>');
  15. $loader.appendTo('#tbl_data');
  16. setTimeout(function(){
  17. $loader.remove();
  18. $('#tbl_data').attr('class', 'form-inline');
  19. $('<h4 class = "text-primary">Header Title</h4><br />').appendTo('#tbl_data');
  20. $col = [];
  21. for($a = 1; $a <= $h_cols; $a++){
  22. $col.push($a);
  23. }
  24. $.each($col, function(idx, va){
  25. $('<input type = "text" class = "form-control th_data" name = "th_data[]" placeholder = "Column' + va + '" size = "5" style = "margin:0px 10px 0px 10px;"/>').appendTo('#tbl_data');
  26. });
  27. $('<button id = "btn_hdata" class = "btn btn-success form-control"><span class = "glyphicon glyphicon-plus"></span></button>').appendTo('#tbl_data');
  28. }, 3000);
  29. }
  30. }
  31. }
  32. });
  33.  
  34. $(document).on('click', '#btn_hdata', function(){
  35. if($('.th_data').val() == ""){
  36. alert("Please enter something");
  37. }else{
  38. $th_data = [];
  39. $('.th_data').each(function(){
  40. $th_data.push($(this).val());
  41. });
  42. $('#tbl_data').empty();
  43. $loader = $('<center>Creating table <img src = "images/default.gif" height = "50"></center>');
  44. $loader.appendTo('#tbl_data');
  45. setTimeout(function(){
  46. $loader.remove();
  47. $newTable = $('<table></table>').attr('class', 'table table-bordered');
  48. $thead = $('<thead></thead>').appendTo($newTable);
  49. $h_rows = $('<tr></tr>').appendTo($thead);
  50. $.each($th_data, function(index, value){
  51. $('<th style = "width:25%;">' + value + '</th>').appendTo($h_rows);
  52. });
  53. $tbody = $('<tbody id = "t_body"></tbody>').appendTo($newTable);
  54. $newTable.appendTo('#tbl_data');
  55. $.each($th_data, function(i, val){
  56. $('<input type = "text" class = "form-control tb_data" placeholder = "' + val + '" name = "tb_data[]" size = "5" style = "margin:0px 10px 0px 10px;"/>').appendTo('#tbl_data');
  57. });
  58. $('<button id = "btn_bdata" class = "btn btn-success form-control">Insert</button>').appendTo('#tbl_data');
  59. }, 3000);
  60. }
  61. });
  62.  
  63. $(document).on('click', '#btn_bdata', function(){
  64. $tb_tr = $('<tr></tr>');
  65. $('.tb_data').each(function(){
  66. $('<td>' + $(this).val() + '</td>').appendTo($tb_tr);
  67. });
  68. $tb_tr.appendTo('#t_body');
  69. $('.tb_data').val('');
  70. })
  71.  
  72. });

There you have it we successfully created a Dynamically Generate Table using jQuery. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.