How to Limit Display Table Row Data in JavaScript

How to Limit Display Table Row Data in JavaScript

Introduction

In this tutorial we will create a How to Limit Display Table Row Data in JavaScript. This tutorial purpose is to teach you how to limit table row. This will cover all the important functionality that will limit the display row data in the table. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application that can limit the data of the table being shown. I will give my best to provide you the easiest way of creating this program Limit Table Row Data. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the html form and html table. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Limit Display Table Row Data</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-8">
  17. <div class="form-inline">
  18. <label>Max Entry</label>
  19. <input type="number" id="row" style="width:80px;" class="form-control"/>
  20. <button class="btn btn-success" onclick="limitRow();">Limit</button>
  21. </div>
  22. <br />
  23. <table class="table table-bordered">
  24. <thead class="alert-info">
  25. <tr>
  26. <th>Firstname</th>
  27. <th>Lastname</th>
  28. <th>Address</th>
  29. </tr>
  30. </thead>
  31. <tbody id= "result"></tbody>
  32. </table>
  33. </div>
  34. <div class="col-md-4">
  35. <div class="form-group">
  36. <label>Firstname</label>
  37. <input type="text" id="firstname" class="form-control"/>
  38. </div>
  39. <div class="form-group">
  40. <label>Lastname</label>
  41. <input type="text" id="lastname" class="form-control"/>
  42. </div>
  43. <div class="form-group">
  44. <label>Address</label>
  45. <input type="text" id="address" class="form-control"/>
  46. </div>
  47. <div class="form-group">
  48. <button type="button" onclick="saveData()" class="btn btn-primary form-control">Save Data</button>
  49. </div>
  50. </div>
  51. </div>
  52. </body>
  53. <script src="script.js"></script>
  54. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will allow you to limit the data row to be shown in the table. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. var data = [];
  2.  
  3. function saveData(){
  4. var firstname = document.getElementById("firstname");
  5. var lastname = document.getElementById("lastname");
  6. var address = document.getElementById("address");
  7.  
  8. if(firstname.value == "" || lastname.value == "" || address.value == ""){
  9. alert("Please complete the required field first!");
  10. }else{
  11. var member = {
  12. 'firstname': firstname.value,
  13. 'lastname': lastname.value,
  14. 'address': address.value,
  15. };
  16.  
  17. data.push(member);
  18. displayData(data.length);
  19. firstname.value = "";
  20. lastname.value = "";
  21. address.value = "";
  22.  
  23. }
  24. }
  25.  
  26.  
  27. function limitRow(){
  28. var row=document.getElementById('row').value;
  29.  
  30. if(row=="" || row<=0){
  31. alert("Please enter a valid number");
  32. }else{
  33. if(data.length == 0){
  34. alert("No data found!");
  35. }else{
  36. displayData(row);
  37. }
  38.  
  39. }
  40. }
  41.  
  42.  
  43. function displayData(length){
  44. var table = "" ;
  45.  
  46. for(var i=0; i<length; i++){
  47. table += "<tr>";
  48. table += "<td>"
  49. + data[i].firstname +"</td>"
  50. + "<td>" + data[i].lastname +"</td>"
  51. + "<td>" + data[i].address +"</td>" ;
  52. table += "</tr>";
  53. }
  54.  
  55. document.getElementById("result").innerHTML = table;
  56.  
  57. }

In the code above we just simply create one method called limitRow(). this function will limit the table row data that will be show in your html table. The function is compose only of a conditional statement that will limit the display data base on the entered number

Output:

The How to Limit Display Table Row Data in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Limit Display Table Row Data in JavaScript. 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment