Dynamically Generate Table Data Using jQuery Tutorial
In this code we will tackle about Dynamically Generate Table using jQuery. The program will let you create your own table dynamically. The code uses a jQuery plugin that shortens your native scripting 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 the 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/.
I'll be using a GIF file that will displays as my loader for each process in this tutorial. Click here and download the file and save it inside a folder naming images.
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
.
Please ensure that the directory or location of included plugins/libraries for both external CSS Style Sheet and Script are correct according to your file locations.
- <!DOCTYPE html>
- <html lang = "en">
- <head>
- <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css"/>
- <meta charset = "UTF-8" name = "viewport" content = "width=device-width"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <div class="navbar-header">
- </div>
- </div>
- </nav>
- <div class="row">
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div id="tbl_data">
- <div class="form-inline">
- <input type = "number" min ="0" max = "5" id = "h_cols" class = "form-control"/>
- </div>
- </div>
- <br />
- <div id ="table table-responsive">
- </div>
- </div>
- </div>
- </body>
- </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. In my case, this file is located along with the jQuery file inside the js directory.
- $(document).ready(function(){
- $(document).on('click', '#btn_tblhead', function(){
- if( $('#h_cols').val() == ""){
- alert("Please enter something!");
- }else{
- if($('#h_cols').val() > 5){
- alert("Must not exceed 5");
- }else{
- if($('#h_cols').val() == 0){
- alert("Please enter a valid number");
- }else{
- $h_cols = $('#h_cols').val();
- $('#tbl_data').empty();
- $loader = $('<center>Generating <img src = "images/default.gif" height = "50"></center>');
- $loader.appendTo('#tbl_data');
- setTimeout(function(){
- $loader.remove();
- $('#tbl_data').attr('class', 'form-inline');
- $('<h4 class = "text-primary">Header Title</h4><br />').appendTo('#tbl_data');
- $col = [];
- for($a = 1; $a <= $h_cols; $a++){
- $col.push($a);
- }
- $.each($col, function(idx, va){
- $('<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');
- });
- $('<button id = "btn_hdata" class = "btn btn-success form-control"><span class = "glyphicon glyphicon-plus"></span></button>').appendTo('#tbl_data');
- }, 3000);
- }
- }
- }
- });
- $(document).on('click', '#btn_hdata', function(){
- if($('.th_data').val() == ""){
- alert("Please enter something");
- }else{
- $th_data = [];
- $('.th_data').each(function(){
- $th_data.push($(this).val());
- });
- $('#tbl_data').empty();
- $loader = $('<center>Creating table <img src = "images/default.gif" height = "50"></center>');
- $loader.appendTo('#tbl_data');
- setTimeout(function(){
- $loader.remove();
- $newTable = $('<table></table>').attr('class', 'table table-bordered');
- $thead = $('<thead></thead>').appendTo($newTable);
- $h_rows = $('<tr></tr>').appendTo($thead);
- $.each($th_data, function(index, value){
- $('<th style = "width:25%;">' + value + '</th>').appendTo($h_rows);
- });
- $tbody = $('<tbody id = "t_body"></tbody>').appendTo($newTable);
- $newTable.appendTo('#tbl_data');
- $.each($th_data, function(i, val){
- $('<input type = "text" class = "form-control tb_data" placeholder = "' + val + '" name = "tb_data[]" size = "5" style = "margin:0px 10px 0px 10px;"/>').appendTo('#tbl_data');
- });
- $('<button id = "btn_bdata" class = "btn btn-success form-control">Insert</button>').appendTo('#tbl_data');
- }, 3000);
- }
- });
- $(document).on('click', '#btn_bdata', function(){
- $tb_tr = $('<tr></tr>');
- $('.tb_data').each(function(){
- $('<td>' + $(this).val() + '</td>').appendTo($tb_tr);
- });
- $tb_tr.appendTo('#t_body');
- $('.tb_data').val('');
- })
- });
DEMO
There you have it we successfully created a Dynamically Generate Table using jQuery. I hope that this simple tutorial helps you with what you are looking for and you'll find this useful for your future web-based application projects. For more updates and tutorials just kindly visit this site.
Enjoy Coding!Add new comment
- 1261 views