javascript - Alpine JS 表格数据绑定(bind)

标签 javascript html alpine.js

我是 Alpine JS 的新手。我想用这样的详细行设计我的表格:

enter image description here

我写了一个像这样的简单 HTML 表格:

       
<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 : Sincere@april.biz</td>    
  </tr>
</table>

我试图将我的 JSON 绑定(bind)到这个表。那时,它没有按预期工作。这是我尝试过的:


<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>

使用这段代码,输出将如下所示:

enter image description here

用户详细信息字段在列表总数之后构建。而且那里没有用户电子邮件之类的数据。我错过了什么?我该如何修复此代码?

您可以从 here 访问 Codepen 项目.

如有任何帮助,我们将不胜感激!

最佳答案

我已经尝试更改一些关于 HTML TABLES 的内容,最终,我达到了您预期的结果。这是代码笔链接: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>

关于javascript - Alpine JS 表格数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64826238/

相关文章:

html - 悬停子菜单

html - nth-of-type(2) 是针对第一个类型?

javascript - AlpineJs : Cannot read properties of undefined (reading 'focus' )

javascript - JQuery 点击事件仅在第一次工作

javascript - 更新云功能中的firestore

javascript - 延迟 React onMouseOver 事件

javascript - html5 Canvas 工具提示仅对最后绘制的对象可见,对以前的对象不可见

javascript - 为什么算法性能不同

javascript - Alpine.js - 嵌套组件

node-modules - 如何使用 mix 设置 Alpine.js 3