This article describes how a Bootstrap Table is created using a given JSON data. The data can be retrieved by either importing it or representing it in JavaScript. The following are given two methods to do it.
Displaying JSON data without importing any file: The JSON file that has to be read can be represented using JavaScript. This can then be given to the Bootstrap Table to represent the data.
Example:
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title> load data from json file into a bootstrap table </title> <!-- Include Bootstrap for styling --> <link rel="stylesheet" href= <!-- Include the Bootstrap Table CSS for the table --> <link rel="stylesheet" href= </head> <body> <div class="container"> <h1 class="text text-success text-center "> GeeksforGeeks </h1> <h6 class="text text-success text-center"> A computer science portal for geeks </h6> <table class="table-striped border-success"> <thead> <tr> <th data-field="id"> <span class="text-success"> Employee ID </span> </th> <th data-field="name"> <span class="text-success"> Employee Name </span> </th> <th data-field="date"> <span class="text-success"> Joining Date </span> </th> </tr> </thead> </table> </div> <!-- Include jQuery and other required files for Bootstrap --> <script src= </script> <script src= </script> <script src= </script> <!-- Include the JavaScript file for Bootstrap table --> <script src= </script> <script type="text/javascript"> $(document).ready(function () { // Use the given data to create // the table and display it $('table').bootstrapTable({ data: mydata }); }); // Specify the JSON data to be displayed var mydata = [ { "id": "24323", "name": "Mark Smith", "date": "25/5/2020" }, { "id": "24564", "name": "Caitlin MacDonald", "date": "17/5/2020" }, { "id": "24345", "name": "Jessie Johnson ", "date": "1/5/2020" }, { "id": "24874", "name": "Alen Williams", "date": "14/5/2020" }, { "id": "24323", "name": "Maria Garcia ", "date": "13/5/2020" } ]; </script> </body> </html> |
Output:
Displaying JSON data after importing it from a file: The JSON data to be displayed is stored in a local folder in a JavaScript file that is imported. This imported data can then be given to the Bootstrap Table to represent the data. ES6 feature backticks (` `) can be used for the multi-line string value interpolation.
Example:
-
JSON Data: The JSON data stored in the below example:
Javascript
// Contents for test.js// Represents JSON of the datavarda = `[{"id":"24323","name":"Mark Smith","date":"25/5/2020"},{"id":"24564","name":"Caitlin MacDonald","date":"17/5/2020"}]`;chevron_rightfilter_none -
Program: To load data from JSON into a Bootstrap Table
HTML
<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width,initial-scale=1,shrink-to-fit=no"><title>load data from json fileinto a bootstrap table</title><!-- Include Bootstrap for styling --><linkrel="stylesheet"href=<!-- Include the Bootstrap TableCSS for the table --><linkrel="stylesheet"href=</head><body><divclass="container"><h1class="text text-success text-center ">GeeksforGeeks</h1><h6class="text text-success text-center">A computer science portal for geeks</h6><tableclass="table-striped border-success"><thead><tr><thdata-field="id"><spanclass="text-success">Employee ID</span></th><thdata-field="name"><spanclass="text-success">Employee Name</span></th><thdata-field="date"><spanclass="text-success">Joining Date</span></th></tr></thead></table></div><!-- Include jQuery and other required files for Bootstrap --><scriptsrc=</script><scriptsrc=</script><scriptsrc=</script><!-- Include the JavaScript file for Bootstrap table --><scriptsrc=</script><!-- Include the file where the JSON data is stored --><scripttype="text/javascript"src="test.js"></script><scripttype="text/javascript">$(document).ready(function () {// Use the given data to create// the table and display it$('table').bootstrapTable({data: mydata});});// Parse the imported data as JSON// and store itvar mydata = JSON.parse(da)</script></body></html>chevron_rightfilter_none -
Output:
Recommended Posts:
- How to make Bootstrap table with sticky table head?
- How to convert JSON data to a html table using JavaScript/jQuery ?
- How to fetch data from JSON file and display in HTML table using jQuery ?
- JSON | modify an array value of a JSON object
- How to convert JSON string to array of JSON objects using JavaScript ?
- How to load notification alert on top right corner without click of button in bootstrap ?
- How to place table text into center using Bootstrap?
- How to load the contents of a text file into a JavaScript variable?
- How to import data from .CSV file into MySQL table using Node.js ?
- Include Bootstrap in AngularJS using ng-bootstrap
- Difference between bootstrap.css and bootstrap-theme.css
- Difference Between Bootstrap 3 and Bootstrap 4
- Difference between Bootstrap 4 and Bootstrap 5
- How to make whole row in a table clickable as link in Bootstrap ?
- How to make the existing bootstrap 2 table responsive?
- How to show contents of selected row of a table in Bootstrap model using jQuery ?
- Deserializing a JSON into a JavaScript object
- How to convert JSON results into a date using JavaScript ?
- How to universally parse JSON into blocks using jQuery ?
- How to create table with 100% width, with vertical scroll inside table body in HTML ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

