解决方案1:使用可观察
此解决方案是通过使用
BehaviorSubject
async
管道,并删除要隐藏在数组中的行项目。每次更改主题中的值时,整个列表都会更新。
import {BehaviorSubject} from "rxjs/BehaviorSubject";
...
public items$: BehaviorSubject<Array<any>> = new BehaviorSubject([]);
constructor() {
this.items$.next(this.allFeedItems);
}
public hideSomeRow() {
for (let i = 0; i < this.allFeedItems.length; i++) {
let barCategory = StatusBar.categoryArr[i];
let dataCategory = this.allFeedItems[i].category;
if (barCategory === dataCategory) {
this.allFeedItems.splice(i, 1);
}
}
this.items$.next([...this.allFeedItems]);
}
您的观点:
<ListView [items]="items$ | async" class="list-group">
<ng-template let-item="item" let-index="index">
<StackLayout class="card-view" margin="10">
<StackLayout>
<StackLayout orientation="horizontal">
<Image src={{item.iconName}} stretch="none" class="item-icon"></Image>
<Label class="item-category" [text]="item.category"></Label>
</StackLayout>
<StackLayout orientation="horizontal">
<Label class="item-time" text="4 hours ago"></Label>
</StackLayout>
<StackLayout orientation="vertical">
<Image src={{item.imageUrl}} stretch="aspectFill" width="100%" height="50%" class="item-image"></Image>
<Label class="item-title" [text]="item.title" textWrap="true"></Label>
</StackLayout>
</StackLayout>
</StackLayout>
</ng-template>
</ListView>
<Image (tap)="hideSomeRow()" ... class="item-image"></Image>
(在操作后隐藏某些行/删除某些项)
如果项目的语句应该隐藏或可见,则需要将其存储在数组变量中。
public isVisible: Array<boolean> = [];
public hideSomeRow() {
for (let i = 0; i < this.allFeedItems.length; i++) {
let barCategory = StatusBar.categoryArr[i];
let dataCategory = this.allFeedItems[i].category;
if (barCategory === dataCategory) {
this.allFeedItems.splice(i, 1);
}
}
this._changeDetectorRef.markForCheck();
}
现在在您的html中,没有要更改的内容:
<ListView [items]="allFeedItems" class="list-group">
<ng-template let-item="item" let-index="index">
<StackLayout class="card-view" margin="10">
<StackLayout>
<StackLayout orientation="horizontal">
<Image src={{item.iconName}} stretch="none" class="item-icon"></Image>
<Label class="item-category" [text]="item.category"></Label>
</StackLayout>
<StackLayout orientation="horizontal">
<Label class="item-time" text="4 hours ago"></Label>
</StackLayout>
<StackLayout orientation="vertical">
<Image src={{item.imageUrl}} stretch="aspectFill" width="100%" height="50%" class="item-image"></Image>
<Label class="item-title" [text]="item.title" textWrap="true"></Label>
</StackLayout>
</StackLayout>
</StackLayout>
</ng-template>
</ListView>
<Image (tap)="hideSomeRow()" ... class="item-image"></Image>