[英]Angular nested *ngFor
I am having a table with two nested iterations, the first three columns where an array of object (item) iterates, and a fourth column is where an array of numbers should iterate ( total=[30,70,100]
)我有一个包含两个嵌套迭代的表,前三列是对象(项目)数组迭代的地方,第四列是数字数组应该迭代的地方(
total=[30,70,100]
)
<table class="table">
<thead class="thead-dark">
<tr>
<th>Item</th>
<th>Unit Price</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let i of item">
<td>{{i.product}}</td>
<td>{{i.price}}</td>
<td>{{i.quantity}}</td>
<ng-container *ngFor="let t of total">
<td>{{t}}</td>
</ng-container>
</tr>
</tbody>
</table>
The object array iterates fine, however, the problem is with the array of numbers ( total=[30,70,100]
), I tried to place the ( ng-container *ngFor="let t of total"
) on different levels, but it always getting populated in a wrong way, I would appreciate advising me how to solve it.对象数组迭代得很好,但是,问题在于数字数组(
total=[30,70,100]
),我试图将( ng-container *ngFor="let t of total"
)放在不同的级别上,但是它总是以错误的方式填充,我很感激建议我如何解决它。
Assuming your index
level for the arrays are same, you can always use the index
to track and repeat the element.假设数组的
index
级别相同,您始终可以使用index
来跟踪和重复元素。
<tr *ngFor="let i of item; let in=index"> <td>{{i.product}}</td> <td>{{i.price}}</td> <td>{{i.quantity}}</td> <ng-container *ngFor="let t of total[in]"> <td>{{t}}</td> </ng-container> </tr>
Edit: If the OP is multiplying quantity with price to come up with total then we don't need to maintain a separate array for that, we can just multiply it in the view and get it:编辑:如果 OP 将数量与价格相乘得出总数,那么我们不需要为此维护一个单独的数组,我们可以在视图中将其相乘并得到它:
<tr *ngFor="let i of item; let in=index"> <td>{{i.product}}</td> <td>{{i.price}}</td> <td>{{i.quantity}}</td> <td>{{i.price * i.quantity}}</td> </tr>
We may need more information on how the data should be populated, what I can see is that the td tag is repeating in a wrong way when iterates with the total, having more tds than th tags.我们可能需要更多关于如何填充数据的信息,我可以看到 td 标签在迭代总数时以错误的方式重复,tds 比 th 标签多。 Also the total variable is a property inside the item or is separated?.
此外,总变量是项目内的一个属性还是被分隔?。 For now I can give you examples for some scenarios:
现在我可以给你一些场景的例子:
If you want to have the total in a single td and the total is a property of i如果您想在单个 td 中获得总数,并且总数是 i 的属性
<tr *ngFor="let i of item">
<td>{{i.product}}</td>
<td>{{i.price}}</td>
<td>{{i.quantity}}</td>
<td>{{i.total.join(,)}}</td>
</tr>
If you want to have the total in a single td and the total is just a variable outside the item array.如果您想在单个 td 中获得总数,而总数只是项目数组之外的一个变量。
<tr *ngFor="let i of item let index = index">
<td>{{i.product}}</td>
<td>{{i.price}}</td>
<td>{{i.quantity}}</td>
<td>{{total[index]}}</td>
</tr>
If you want to have sum the total in a single td and the total is a property of i如果您想在单个 td 中求和总数,并且总数是 i 的属性
<tr *ngFor="let i of item">
<td>{{i.product}}</td>
<td>{{i.price}}</td>
<td>{{i.quantity}}</td>
<td>{{i.total.reduce((a, b) => a + b, 0)}}</td>
</tr>
If you want to have sum the total in a single td and the total is just a variable outside the item array.如果您想在单个 td 中求和总数,而总数只是项目数组之外的变量。
<tr *ngFor="let i of item let index = index">
<td>{{i.product}}</td>
<td>{{i.price}}</td>
<td>{{i.quantity}}</td>
<td>{{total[index].reduce((a, b) => a + b, 0)}}</td>
</tr>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.