DEV Community

Michael Burrows
Michael Burrows

Posted on • Originally published at w3collective.com

Alpine.js: Displaying API data in a HTML table

In this tutorial we’ll be using Alpine.js to load data from an API and then display that data in a HTML table. We’ll be using the free SportsDB API to load a list of teams from the English Premier League along with some associated team data.

Let’s get started, for the purposes of this tutorial you can load Alpine via CDN:

<script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
Enter fullscreen mode Exit fullscreen mode

Now for the HTML markup starting with a wrapper <div> for the component:

<div
    x-cloak
    x-data="{teams: [], 'isLoading': true}"
    x-init="fetch('https://www.thesportsdb.com/api/v1/json/1/lookup_all_teams.php?id=4328')
    .then(response => response.json())
    .then(response => { teams = response.teams; isLoading = false; console.log(response.teams); })"
>
<!-- Table will go here -->
</div>
Enter fullscreen mode Exit fullscreen mode

x-cloak hides an element until Alpine has fully loaded, in this instance it will prevent the flash of the table header before the x-show has evaluated and hidden the element. For the x-cloak attribute to work you must also include the following CSS:

[x-cloak] {
  display: none !important;
}
Enter fullscreen mode Exit fullscreen mode

x-data defines a chunk of HTML as an Alpine component and provides the reactive data for that component to reference. In this case it’ll store the team data in an array and the loading state of the component.

x-init is used to fetch the data and store it in x-data before processing and rendering the component. The data itself is being fetched with the JavaScript fetch() method using the SportsDB API endpoint for the English Premiere League teams.

If successful you should the team data logged in the browser console as follows:

Image description

We can now output this data into a HTML table:

<h1 x-show="isLoading">Loading...</h1>
<table x-show="!isLoading">
    <tr>
        <th>Team</th>
        <th>Founded</th>
        <th>Stadium</th>
        <th>Capacity</th>
    </tr>
    <template x-for="team in teams" :key="team.idTeam">
        <tr>
        <td x-text="team.strTeam"></td>
        <td x-text="team.intFormedYear"></td>
        <td x-text="team.strStadium"></td>
        <td x-text="team.intStadiumCapacity"></td>
        </tr>
    </template>
</table>
Enter fullscreen mode Exit fullscreen mode

x-show toggles the visibility of the <h1> loading text and the <table> based on the value of isLoading. We then use the x-for directive to loop through each team and output that data using x-text into the individual table rows. The <template> element used here is a mechanism for holding HTML that is not to be rendered immediately when a page is loaded.

That’s all for this tutorial, in it we used 6 of the 15 directives available in V3 of Alpine.js. If your interested in exploring the framework further be sure to checkout some of our other Alipine.js tutorials.

Top comments (3)

Collapse
 
nasoma profile image
nasoma

Great stuff! Is there a way to cache the data so that we don't make a new request every time.

Collapse
 
jamesli2021 profile image
jamesli2021 • Edited

Session or Local storage are the mechanism for caching per session or long-term.

You can cache .json file within browser for a long-time if the origin source header have set how long it will be cache.

Or build your own local web server in your machine like a CDN way and mirror JSON or etc, it's simple.

Collapse
 
diomed profile image
May Kittens Devour Your Soul • Edited

I get nothing but ** loading...**
when I do all that