如何操作 ngfor table,在两个数组之间切换

How to manipulate ngfor table, to toggle between two arrays

我想知道是否有一种简单的方法可以将不同的数组解析为单个 *ngfor table。我有以下代码:

.html

<div class="mb-3">
        <div class="form-check">
            <input class="form-check-input" type="radio" name="date" (click)="valid()" />
            <label class="form-check-label">Valid</label>
        </div>
        <div class="form-check">
            <input class="form-check-input" type="radio" name="date" (click)="invalid()"/>
            <label class="form-check-label">Invalid</label>
        </div>
</div>

<tr *ngFor="let myAccount of Account | filterBy: accountFilter | paginate: { itemsPerPage: count, currentPage: p }; let i = index">
            <td>{{ (p - 1) * count + i + 1 }}</td>
            <td>{{myAccount.name}}</td>
            <td>{{myAccount.startDate}}</td>
            <td>{{myAccount.endDate}}</td>
</tr>

.ts

Account = [];
radioAccount = [];
currentDate = '';

ngOnInit() {
    showAll();
}
showAll() {
       return this.acctService.getAccount().subscribe(data => this.Account = data);
}

valid() {
        this.currentDate = this.datePipe.transform(new Date(),"yyyy-MM-dd");
        this.radioAccount = this.Account.filter(data => {
            return data.startDate < this.currentDate  && data.endDate > this.currentDate});
}

invalid() {
    this.currentDate = this.datePipe.transform(new Date(),"yyyy-MM-dd");
    this.radioAccount = this.Account.filter(data => {
        return data.startDate < this.currentDate  && data.endDate <= this.currentDate});
}

你可以看到我有两个数组,如何在单击单选按钮时显示 "radioAccount" 数组的内容?也就是说,可以在 "Account" 内容和 "radioAccount" 内容之间切换。我认为用 *ngif 重复 *ngfor 代码不是解决方案。

您可以创建一个不会被过滤的数组,例如 AccountSource 然后您可以将原始值 (AccountSource) 分配给 Account:

Account = [];
AccountSource= [];

showAll() {
   return this.acctService.getAccount()
       .subscribe(data => {
            this.Account = data;
            this.AccountSource = data;
       });
}

valid() {
        this.currentDate = this.datePipe.transform(new Date(),"yyyy-MM-dd");
        this.Account = this.Account.filter(data => {
            return data.startDate < this.currentDate  && data.endDate > this.currentDate});
}

invalid() {
    this.currentDate = this.datePipe.transform(new Date(),"yyyy-MM-dd");
    this.Account = this.Account.filter(data => {
        return data.startDate < this.currentDate  && data.endDate <= this.currentDate});
}

setSourceData() {
    this.Account = this.AccountSource;
}

并在您想查看原始值时调用 setSourceData() 方法。