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

问题描述 投票:0回答:1

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

。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”数组的内容?即,可以在“帐户”内容和“ radioAccount”内容之间进行切换。我不认为用* ngif重复* ngfor代码是一种解决方案。

arrays angular ngfor
1个回答
0
投票

[您可以创建一个不会被过滤的数组,例如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()方法。

© www.soinside.com 2019 - 2024. All rights reserved.