最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Alpine JS Table Data Binding - Stack Overflow

programmeradmin3浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 7

I'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:

发布评论

评论列表(0)

  1. 暂无评论