如何在html和angular中创建动态表

如何在html和angular中创建动态表,html,angular,typescript,Html,Angular,Typescript,我需要在Html中显示一个动态表,我不知道每行的模板是什么: 有时它包含2列,有时是4列 我需要这样的东西: <div> <h1>Angular HTML Table Example</h1> <table> <thead> <tr> <th>#ID</th> <th>Name</th&

我需要在Html中显示一个动态表,我不知道每行的模板是什么: 有时它包含2列,有时是4列

我需要这样的东西:

    <div>
   <h1>Angular HTML Table Example</h1>
   <table>
      <thead>
         <tr>
            <th>#ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Address</th>
            <th>Action</th>
         </tr>
      </thead>
      <tbody>
         <tr *ngFor="let item of ItemsArray">
            <th>{{ item.id }}</th>
            <td>{{ item.name }}</td>
            <td>{{ item.email }}</td>
            <td>{{ item.phone}}</td>
            <td>{{ item.address }}</td>
           
         </tr>
      </tbody>
   </table>
</div

角度HTML表格示例
#身份证
名称
电子邮件
电话
地址
行动
{{item.id}
{{item.name}
{{item.email}
{{item.phone}
{{item.address}

您可以使用下面的代码和render header列获取列表项的键

Object.keys(list);
这是密码

您可以在产品组件文件中看到所需的代码。我希望它适用于您的场景

这是解决方案:

<tbody class="f">
             <ng-container *ngFor="let message of this.allMessages | filter:term | paginate: config  ;let i = index"> 
            <tr >
                <td>{{ i+1 }}</td>
                    <ng-container *ngFor = "let j of this.messageIndex">
                        <td>{{message.data[0][j]}}</td>
                    </ng-container>
             </tr> 
             </ng-container> 
        </tbody>

{{i+1}}
{{message.data[0][j]}

*ngFor
应位于
td
标记的内部,以便为每列创建一个单元格。此外,您应该迭代一个
对象.keys(item)
,这样您就可以执行
*ngFor=“let key of keys”
<代码>项[键]
。我应该用打字脚本写吗?或者HTML?只需将HTML代码更改为此,您应该在typescript中编写Objecy.keys(list)。看看这个对象,它看起来像没有键的json——类似于:[“12”,“firstItem”]
Object.keys(list);
<tbody class="f">
             <ng-container *ngFor="let message of this.allMessages | filter:term | paginate: config  ;let i = index"> 
            <tr >
                <td>{{ i+1 }}</td>
                    <ng-container *ngFor = "let j of this.messageIndex">
                        <td>{{message.data[0][j]}}</td>
                    </ng-container>
             </tr> 
             </ng-container> 
        </tbody>