angular - 如何在过滤器管道中检查未找到数据?
问题描述
我编写了一个自定义表和管道,它也成功过滤了我的表。现在,如果没有可用数据,我想收到“未找到数据”消息。
我必须在我的代码中添加什么?
// Pipe
export class FormControlFilterPipe implements PipeTransform {
transform(items: any[], field: string, value: string): any[] {
if (!items || (!value || value === null || value.length < 1)) {
return items;
}
return items.filter((item) => {
const val = JSON.stringify(Object.values(item.value));
return (val && val.toLowerCase().indexOf(value.toLowerCase()) >= 0);
});
}
}
// Template
<table class="custom-table" formArrayName="rows" *ngIf="rows && rows !== null && !isLoading">
<thead>
<tr class="optimize-header-row">
<th *ngFor="let column of displayedColumns; let columnIndex = index;">
{{ column.name }}
</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let rowControl of rows.controls | filterBySearchText: 'value' :searchTerm; let rowIndex = index">
<td *ngFor="let column of displayedColumns; let columnIndex = index;">
<ng-container [formGroupName]="rowIndex">
<span>
<label class="change-label">
<input class="edit-input" type="text"[formControlName]="column.attribute">
</label>
</span>
</ng-container>
</td>
</tr>
</tbody>
</table>
之后我尝试了以下语句,</table>
<div *ngIf="rows.controls.length === 0">No Data in Table</div>
但不幸的是它不起作用。
解决方案
获取管道转换为变量后的值并使用它来检查空长度或零长度。
let rowControl of (rows.controls | filterBySearchText: 'value') as filteredData
完整示例(注意filteredData.length > 0
检查)
<table class="custom-table" formArrayName="rows" *ngIf="rows && rows !== null && !isLoading">
...
<tbody>
<tr *ngFor="let rowControl of (rows.controls | filterBySearchText: 'value' :searchTerm) as filteredData; let rowIndex = index">
<ng-container *ngIf="filteredData.length > 0; else emptySearch">
<td *ngFor="let column of displayedColumns; let columnIndex = index;">
<ng-container [formGroupName]="rowIndex">
<span>
<label class="change-label">
<input class="edit-input" type="text"[formControlName]="column.attribute">
</label>
</span>
</ng-container>
</td>
</ng-container>
<ng-template #emptySearch>
"No Data Found"
</ng-template>
</tr>
</tbody>
</table>
推荐阅读
- python - python中的日志文件
- javascript - 使用 MutationObserver 时,“参数 1 不是‘节点’类型”
- python - 如何在 python-escpos 中读取 ASB 状态
- google-cloud-platform - GKE cluster-autoscaler 无法根据 nodeaffinity 扩展 nodepool
- single-sign-on - Auth0 和 Intuit SSO
- flutter - 如何在颤动中修复 RadioListTile 列表?
- r - 您可以在多个级别进行过滤吗?
- office365 - Moodle 与 Microsoft Teams 的集成
- python - 使用 REGEX Python 删除特定字符后的行
- spring - 在我的 Spring Boot 中添加 facebook social 后无法使用任何控制器的 post 方法