How to Print Page Content as Table Format in JavaScript

How to Print Page Content as Table Format in JavaScript

Introduction

In this tutorial we will create a How to Print Page Content as Table Format in JavaScript. This tutorial purpose is to teach you how to print table function. This will cover all the important functionality that allow you to print a document in table format. 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 if you want print a content in your page. I will give my best to provide you the easiest way of creating this program Print Content as Table Format. 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 display the html table and button. 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. <body onload="displayData();">
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">How to Print Page Content as Table Format</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-9">
  18.                         <table class="table table-bordered" id="table">
  19.                                 <thead class="alert-info">
  20.                                         <tr>
  21.                                                 <th>Firstname</th>
  22.                                                 <th>Lastname</th>
  23.                                                 <th>Address</th>
  24.                                                 <th>Gender</th>
  25.                                         </tr>
  26.                                 </thead>
  27.                                 <tbody id="result"></tbody>
  28.                         </table>
  29.                 </div>
  30.                 <div class="col-md-3">
  31.                         <button class="btn btn-success" onclick="toPrint();"><span class="glyphicon glyphicon-print"></span> Print Table</button>
  32.                 </div>
  33.         </div>
  34. <script src="script.js"></script>      
  35. </body>
  36. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will immediately print the page content when you click the button. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function toPrint() {
  2.         var el=document.getElementById("table");
  3.         el.setAttribute('border', '1px');
  4.         el.setAttribute('cellpadding', '10');
  5.         el.setAttribute('class', 'table table-bordered');
  6.         el.style.borderCollapse='collapse';
  7.        
  8.         newPrint=window.open("");
  9.         newPrint.document.write(el.outerHTML);
  10.         newPrint.print();
  11.         newPrint.close();
  12. }
  13.  
  14.  
  15. function displayData(){
  16.         var data = [
  17.                 { 'firstname': 'John', 'lastname': 'Smith', 'address': 'New York', 'gender': 'Male'},
  18.                 { 'firstname': 'Claire', 'lastname': 'Temple', 'address': 'Racoon City', 'gender': 'Female'},
  19.         ];
  20.        
  21.         var table = "" ;
  22.  
  23.                         for(var i in data){
  24.                                 table += "<tr>";
  25.                                 table += "<td>" + data[i].firstname +"</td>"
  26.                                                 + "<td>" + data[i].lastname +"</td>"
  27.                                                 + "<td>" + data[i].address +"</td>"
  28.                                                 + "<td>" + data[i].gender +"</td>";
  29.                                 table += "</tr>";
  30.                         }
  31.  
  32.         document.getElementById("result").innerHTML = table;           
  33. }

In the code above we only created one method called toPrint(), this function will print the targeted content in table format when you clicked the print button. This code contains several styling attributes to format the content as table form. Then we use the print() to trigger an event that will launch the print window.

Output:


The How to Print Page Content as Table Format 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 Print Page Content as Table Format 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