【发布时间】:2019-05-02 21:48:54
【问题描述】:
我有一张表,想动态地向该表中添加一行。下面是我的代码!
<tr *ngIf="customer">
<td>4</td>
<td>
<input type="text" name="firstName" required minlength="2">
</td>
<td>
<input type="text" name="lastName" required minlength="2">
</td>
<td>
<input type="text" name="countryCode" required maxlength="2">
</td>
<td>
<input type="number" name="age" required minlength="2">
</td>
<td>
<i class="fas fa-times" (click)="cancel()"></i>
</td>
<td>
<i class="far fa-save" (click)="save()"></i>
</td>
</tr>
应在表格下方添加上面的行。是保存上述 html 的选择器(要添加的单个表格行)。目前,当单击按钮时,上面的行显示在页面的最底部,而不是按预期添加到表格中。
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Country</th>
<th scope="col">Gender</th>
<th scope="col">Age</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let customer of customerArray; let i = index">
<td>{{i + 1}}</td>
<td>{{customer.firstName}}</td>
<td>{{customer.lastName}}</td>
<td>{{customer.countryCode}}</td>
<td>{{customer.gender}}</td>
<td>{{customer.age}}</td>
<td><i class="fas fa-edit" (click)="editCustomer()"></i></td>
<td><i class="fas fa-trash-alt" (click)="deleteCustomer(customer)"></i></td>
</tr>
<add-edit-customer></add-edit-customer>
</tbody>
</table>
【问题讨论】:
-
只是将新客户推入
cutomerArray应该添加新行吗? -
如果您使用查看源代码功能或开发人员工具元素选项卡,您将看到由您的视图生成的 HTML。您会注意到嵌套元素 (add-edit-customer) 会干扰表格的布局。最好的办法是按照此处提供的建议添加到您的
customerArray。
标签: angular html-table