admin管理员组文章数量:1356585
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:
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Username</th>
</tr>
<tr>
<td>1</td>
<td>Leanne Graham</td>
<td>Bret</td>
</tr>
<tr>
<td colspan="3">User Email : [email protected]</td>
</tr>
</table>
I tried to bind my JSON to this table. At that point, it did not work as expected. Here is what I tried:
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Username</th>
</tr>
<template x-for="u in users" :key="u.id">
<tr>
<td x-text="u.id"></td>
<td x-text="u.name"></td>
<td x-text="u.username"></td>
</tr>
<tr>
<td x-text="u.email" colspan="3"></td>
</tr>
</template>
</table>
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!
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:
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Username</th>
</tr>
<tr>
<td>1</td>
<td>Leanne Graham</td>
<td>Bret</td>
</tr>
<tr>
<td colspan="3">User Email : [email protected]</td>
</tr>
</table>
I tried to bind my JSON to this table. At that point, it did not work as expected. Here is what I tried:
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Username</th>
</tr>
<template x-for="u in users" :key="u.id">
<tr>
<td x-text="u.id"></td>
<td x-text="u.name"></td>
<td x-text="u.username"></td>
</tr>
<tr>
<td x-text="u.email" colspan="3"></td>
</tr>
</template>
</table>
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!
Share Improve this question asked Nov 13, 2020 at 18:33 AkifAkif 7,6707 gold badges32 silver badges61 bronze badges2 Answers
Reset to default 7I've been tried to change a few about HTML TABLES, Finally, I reached your the expected result. Here's the codepen link: codepen
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Username</th>
</tr>
</thead>
<template x-for="(user, index) in users" :key="index">
<tbody>
<tr>
<td x-text="user.id"></td>
<td x-text="user.name"></td>
<td x-text="user.username"></td>
</tr>
<td x-text="user.email" colspan="3"></td>
</tbody>
</template>
</table>
Thanks to @Serkan Eken. Defining the template
outside of the tbody
solved the problem. It should look like this:
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Username</th>
</tr>
</thead>
<template x-for="(user, index) in users" :key="index">
<tbody>
<tr>
<td x-text="user.id"></td>
<td x-text="user.name"></td>
<td x-text="user.username"></td>
</tr>
<td x-text="user.email" colspan="3"></td>
</tbody>
</template>
</table>
And the expected output:
本文标签: javascriptAlpine JS Table Data BindingStack Overflow
版权声明:本文标题:javascript - Alpine JS Table Data Binding - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744044204a2581221.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论