I’m a newbie in Alpine JS. I want to design my table with detailed rows like this:
I wrote a simple HTML table like this:
JavaScript
x
20
20
1
2
<table>
3
<tr>
4
<th>Id</th>
5
<th>Name</th>
6
<th>Username</th>
7
</tr>
8
9
<tr>
10
<td>1</td>
11
<td>Leanne Graham</td>
12
<td>Bret</td>
13
</tr>
14
15
<tr>
16
<td colspan="3">User Email : Sincere@april.biz</td>
17
</tr>
18
</table>
19
20
I tried to bind my JSON to this table. At that point, it did not work as expected. Here is what I tried:
JavaScript
1
23
23
1
<table>
2
<tr>
3
<th>Id</th>
4
<th>Name</th>
5
<th>Username</th>
6
</tr>
7
8
<template x-for="u in users" :key="u.id">
9
10
<tr>
11
<td x-text="u.id"></td>
12
<td x-text="u.name"></td>
13
<td x-text="u.username"></td>
14
</tr>
15
<tr>
16
<td x-text="u.email" colspan="3"></td>
17
</tr>
18
19
</template>
20
21
</table>
22
23
With this code, the output will look like this:
User detail fields are building after the total of the list. And there is no data like user email in there. What am I missing? How can I fix this code?
You can access the Codepen project from here.
Any help would be appreciated!
Advertisement
Answer
I’ve been tried to change a few about HTML TABLES, Finally, I reached your the expected result. Here’s the codepen link: codepen
JavaScript
1
22
22
1
2
<table>
3
<thead>
4
<tr>
5
<th>Id</th>
6
<th>Name</th>
7
<th>Username</th>
8
</tr>
9
</thead>
10
<template x-for="(user, index) in users" :key="index">
11
<tbody>
12
<tr>
13
<td x-text="user.id"></td>
14
<td x-text="user.name"></td>
15
<td x-text="user.username"></td>
16
</tr>
17
<td x-text="user.email" colspan="3"></td>
18
</tbody>
19
</template>
20
</table>
21
22