ป้ายกำกับ: Dynamic Tables

Tabulator: Fetching JSON Data with AJAX for Dynamic TablesTabulator: Fetching JSON Data with AJAX for Dynamic Tables

In this article, we explore how to use the Tabulator library to dynamically fetch and display data from a JSON API in a table format. This method, known as AJAX, allows for real-time data updates without needing to reload the page.

Setting Up Tabulator with AJAX

To get started, you need to include the Tabulator CSS and JS files in your HTML. Here’s a basic setup:

XML

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Tabulator: data.JSON</title><!-- Tabulator CSS --><link href="https://cdnjs.cloudflare.com/ajax/libs/tabulator/6.3.1/css/tabulator.min.css"integrity="sha512-RYFH4FFdhD/FdA+OVEbFVqd5ifR+Dnx2M7eWcmkcMexlIoxNgm89ieeVyHYb8xChuYBtbrasMTlo02cLnidjtQ=="crossorigin="anonymous" referrerpolicy="no-referrer" rel="stylesheet" /></head><body><div id="tabulator-table"></div><!-- Tabulator JS --><script integrity="sha512-8+qwMD/110YLl5T2bPupMbPMXlARhei2mSxerb/0UWZuvcg4NjG7FdxzuuvDs2rBr/KCNqhyBDe8W3ykKB1dzA=="crossorigin="anonymous" referrerpolicy="no-referrer"src="https://cdnjs.cloudflare.com/ajax/libs/tabulator/6.3.1/js/tabulator.min.js"></script><script>var table = new Tabulator("#tabulator-table", {ajaxURL: "../assets/ISO-4217.json",ajaxConfig: "GET",columns: [{field: "code",title: "Code",width: 80},{field: "currency",title: "Currency"},{field: "digits",title: "Digits",width: 80},{field: "numeric",title: "Numeric",width: 80},{field: "locations",formatter: function (cell) { return cell.getValue().join(", "); },title: "Locations"}],});</script></body></html>

Understanding the Code

The code snippet above sets up a Tabulator table that fetches data from a JSON file located at ../assets/ISO-4217.json. The ajaxConfig is set to GET, which is the standard method for fetching data. The columns array defines the structure of the table, specifying which fields from the JSON data to display and how to format them.

Conclusion

Using Tabulator with AJAX provides a powerful way to dynamically display data in a table format. This method is particularly useful for applications that require real-time data updates, such as dashboards or data visualization tools. By following the setup outlined in this article, you can easily integrate Tabulator into your projects to enhance data presentation and user experience.