Simple Table Generator Using jQuery

In this tutorial we will try to create a simple table generator by using jquery. jQuery is a cross-platform javaScript library design to simplified the modern HTML client side scripting. jQuery syntax was made to make it easier to navigate a document, like select DOM elements, develop Ajax applications and etc. Throughout the tutorial you will learn how to apply an array inside a nested loop. So let's start coding Creating The Mark Up To start with the form, open any kind of text editor that your computer have(notepad, notepad++, etc). Then copy/paste the provided code below and name it 'index.html'.
  1. <!DOCTYPE html>
  2. <html lang = "en">
  3. <head>
  4. <title>Simple Dynamic Table</title>
  5. <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css"/>
  6. <meta charset = "UTF-8" name = "viewport" content = "width=device-width"/>
  7. </head>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <div class="navbar-header">
  11. <a class="navbar-brand" href="http://sourcecodester.com">Sourcecodester</a>
  12. </div>
  13. </div>
  14. </nav>
  15. <div class = "row">
  16. <div class = "col-md-3"></div>
  17. <div class = "col-md-6 well">
  18. <h3 style = "" class = "text-primary">Simple Table Generator</h3>
  19. <hr style = "border-top:1px dotted #000;"/>
  20. <div id = "tbl_data">
  21. <div class = "form-inline">
  22. <label style = "font-size:18px;" class = "text-info">Table header column(max = 5)</label>
  23. <input type = "number" min ="0" max = "5" id = "h_cols" class = "form-control"/>
  24. <button type = "button" id = "btn_tblhead" class = "btn btn-success form-control" ><span class = "glyphicon glyphicon-plus"></span></button>
  25. </div>
  26. </div>
  27. <br />
  28. <div id = "table table-responsive">
  29.  
  30. </div>
  31. </div>
  32. </div>
  33. </body>
  34. <script src = "js/jquery-3.1.1.js"></script>
  35. <script src = "js/script.js"></script>
  36. </html>
Creating The jQuery Function To make this simple application worked, we will need to add a client-side scripting language, To do that just copy/paste the provided code below then name it 'script.js'
  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 alert-warning');
  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. });
This code will generate the table that you will created. The first step is you have to provide a value on how many column will display a table. After giving the value it will generate an input textbox that you will need to provide for your title header for your table. Then you can now insert the data via the input textbox below the table. There you have it we created a simple table generator using jQuery. I hope that this tutorial help you to your on working projects. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Tags

Add new comment