【发布时间】:2021-05-18 20:32:11
【问题描述】:
我无法想出一种方法来显示我的“无结果”div 元素。基本上,我有一个包含订单时间线部分组件的列表组件,每个部分都包含订单组件。像这样:
我的 orders-list.component.html(检查底部 div):
<div class="list-container" [ngClass]="{section: isDeliverySlotsActive === false}">
<label class="list-header" *ngIf="isDeliverySlotsActive === true" style="margin-top: 1.625rem">DELIVERY SLOTS ORDERS</label>
<div [ngClass]="{section: isDeliverySlotsActive === true}" *ngFor="let date of timelines">
<app-orders-list-section
[orders]="orders"
[timeline]="date"
[isDeliverySlotsActive]="isDeliverySlotsActive"
[searchTerm]="searchTerm"
></app-orders-list-section>
</div>
</div>
/* I want to show the below div when there are no results for the search */
<div id="no-results">
<img src="../../../assets/my-orders/no-results.png" alt="No Results" style="margin-top: 6.063rem; margin-bottom: 2.837rem;">
<label class="no-results-text">COULDN'T FIND ANYTHING</label>
<label class="no-results-text weight-medium">Search by order number or customer</label>
</div>
对于每个部分,当用户使用搜索栏搜索订单时,都会应用一种过滤方法。如果搜索词与部分中的订单不对应,则不会显示该部分的订单。如果该部分没有结果,则也不会显示部分标题。
我的orders-list-section.component.html:
<div *ngIf="filteredSectionOrders.length > 0">
<label
*ngIf="isDeliverySlotsActive === true"
[ngClass]="{ slots: isDeliverySlotsActive === true }">
{{ timeline | addSectionDateFormat }}
</label>
</div>
<div *ngFor="let order of filteredSectionOrders">
<app-orders-list-item
[order]="order"
[timeline]="timeline"
></app-orders-list-item>
</div>
我在section组件中的过滤方法:
filterSectionOrders(searchString: string){
if(!searchString) return;
if(this.hasNumbers(searchString)){
this.filteredSectionOrders = this.filteredSectionOrders.filter(order => order.order_num.toString().indexOf(searchString) !== -1);
}
else{
this.filteredSectionOrders = this.filteredSectionOrders.filter(order => {
if(order.first_name && order.last_name){
let fullName = order.first_name + " " + order.last_name;
if(fullName.toLowerCase().indexOf(searchString.toLowerCase()) !== -1){
return order;
}
}
})
}
}
鉴于我将此过滤器应用于每个部分而不是整个列表,我如何找出总结果为 0 的时间,以便我可以只显示一个(不是每个部分) 带有“未找到结果”消息的 div 元素?
提前谢谢你。
【问题讨论】:
-
如果您在返回 JSX 之前在父组件中运行过滤代码,这将容易得多。您可以将每个部分放入一个有序数组中,并让每个数组都包含一个带有订单的子数组。最好在 React.useMemo() 函数中执行此操作以提高性能。
-
@Aadmaa 虽然这不是 React,但将过滤逻辑上移到父组件的方法仍然是正确的。如果您愿意,可以发布相应更新的答案,我会将其标记为正确答案。谢谢!
-
更正:您可以在父组件中运行过滤代码。您可以将每个部分放入一个有序数组中,并让每个数组都包含一个带有订单的子数组。如果可能的话,最好记住这一点以提高性能。
标签: html angular typescript