Bind Table Using jQuery

The code snippet in this article binds a table with data on client-side using jQuery.

Introduction

 
jQuery has some predefined functions. Using predefined functions, we can perform our client-side tasks. jQuery append function is used to bind a table on client-side.
 
Why we will bind on client side 
  • Any operation we will perform in the client-side is more likely to be faster than the server-side because the operation is happening in the client browser.
  • We can give an attractive look to the UI during client-side operation.
  • Reduce the server side coding.
Example
 
Here we have an array named bykeList, which contains three brand names.
 
var bykeList=["Hero","Honda","Suzuki"]
 
Now we will bind this array to a Table using jQuery append function.
  1. <!DOCTYPE html>    
  2. <html lang="en">    
  3. <head>    
  4.   <title>Tooltip Example</title>    
  5.   <meta charset="utf-8">    
  6.   <meta name="viewport" content="width=device-width, initial-scale=1">    
  7.   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">    
  8.   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>    
  9.   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>    
  10. </head>    
  11. <body>    
  12.     
  13. <div class="container">    
  14.  <table class="table table-striped">    
  15. <thead>    
  16. <tr>    
  17. <th>Sl.No</th>    
  18. <th>Byke Name</th>    
  19. <tr>    
  20. </thead>    
  21. <tbody id="tblBykeLists"><tbody>    
  22. </table>    
  23.      
  24. </div>    
  25.     
  26. <script>    
  27. $(document).ready(function(){    
  28. var bykeList=["Hero","Honda","Suzuki"];    
  29. if(bykeList.length>0){  
  30. for(i=0;i<bykeList.length;i++){    
  31. var html="<tr><td>"+Number(1+i)+"</td><td>"+bykeList[i]+"</td></tr>";    
  32. $("#tblBykeLists").append(html);    
  33. }   
  34. }    
  35. });    
  36. </script>    
  37.     
  38. </body>    
  39. </html>   
In the above code, first, I checked the bykeList length. If the length is greater than 0 then there is some data, iterate through the data and bind it in a table.
 
Summary 
 
In this blog, I discussed how we can use jQuery append function. I hope it was useful for you.