How to display data from an API in a dynamic table using JavaScript and Bootstrap
Display data from an API in a dynamic table using JavaScript to fetch and populate the table.
Displaying data from an API in a dynamic table enhances user experience by providing real-time information in a structured format. This tutorial will guide you through the process of integrating an API with an HTML table, using JavaScript to fetch and display data.
Prerequisites
Basic Knowledge of HTML and CSS: Understanding of HTML elements and basic styling.
JavaScript Fundamentals: Familiarity with functions, asynchronous operations, and DOM manipulation.
An API Endpoint: Access to an API that returns data in JSON format. This could be your own API or a public one.
Tutorial Overview
We will:
Set up the HTML structure for the table.
Write JavaScript code to fetch data from an API.
Dynamically populate the table with the fetched data.
Ensure the table is responsive and styled appropriately.
Step 1: Set Up the HTML Structure
Create an HTML file and add the following code to set up the table structure.
Notes:
The table has an id of data-table, which we’ll use to select it in our JavaScript code.
Bootstrap CSS is included for basic styling. You can omit it or replace it with your own styles.
Step 2: Write the JavaScript to Fetch Data from the API
Below the existing code, before the closing </body> tag, add a <script> tag where we’ll write our JavaScript.
Explanation:
async function fetchData(): An asynchronous function that allows us to use await for promises.
fetch(‘YOUR_API_ENDPOINT’): Replace 'YOUR_API_ENDPOINT' with the URL of your API.
response.json(): Parses the JSON data returned by the API.
document.querySelector(‘#data-table tbody’): Selects the <tbody> of the table to insert rows.
data.forEach(user => { … }): Iterates over each item in the data array to create table rows.
Step 3: Replace the Placeholder API with Your Own
In the script, replace 'YOUR_API_ENDPOINT' with the actual URL of your API. For example:
Ensure that your API returns data in the following format or adjust the code accordingly:
Step 4: Test the Implementation
Run the HTML File: Open the HTML file in a web browser.
Check for Data: The table should populate with data fetched from your API.
Verify Console for Errors: Open the browser’s developer console to check for any errors.
Step 5: Handling CORS Issues (If Any)
If you encounter a Cross-Origin Resource Sharing (CORS) error, you might need to adjust your API settings to allow requests from your domain or use a proxy.
Complete Code Example
Here’s the full HTML code with the JavaScript included:
Styling: Feel free to customize the styling of the table and its contents to match your application’s theme.
Error Handling: Enhance the error handling in the fetchData function to provide user feedback if the data fails to load.
Pagination and Sorting: For large datasets, consider implementing pagination and sorting functionality.
Security: If your API requires authentication, ensure you handle API keys or tokens securely.
Conclusion
You have successfully created a dynamic table that fetches and displays data from an API using JavaScript. This approach can be extended to various types of data and integrated into larger applications to enhance data presentation and user interaction.