【发布时间】:2018-10-11 06:37:06
【问题描述】:
我在理解和使用 RxJS Observables 和 ngrx Store 时遇到问题。
我尝试了combineLatest、过滤器、连接数组等,但似乎无法找到一个有效的无错误解决方案。
我将不胜感激任何有关哪种技术最适合实现此结果的 cmets / 反馈
要求
- 从 ngrx 存储中获取 2 个对象,并通过 a 过滤第一个对象
第二个对象中的属性
- 人物 - 人物的第一个对象列表
- 公司 - 第二个目标公司列表
- PeopleFromSelectedCompanies - 过滤第一个对象以仅显示 在第二个对象 d 中与公司 ID 匹配的人。如果不 公司存在于第 2 个对象中,然后我想显示第 01 个对象中的所有人
- 将 PeopleFromSelectedCompanies 分配给 Angular 材料数据表的数据源
- 接受字符串过滤器以过滤 PeopleFromSelectedCompanies 中包含该字符串的任何属性
ngOnInit 的所有代码都可以正常工作,我可以访问所有所需的列表,并且每次我选择另一个客户时,getSelectedCustomersPeople 都会运行。
当前的意大利面条代码????如果你能理解我想要做什么
组件
ngOnInit() {
this.peopleStore.dispatch(new fromPeopleStore.LoadPeople());
this.people$ = this.peopleStore.select(fromPeopleStore.getAllPeople);
this.selectedCustomers$ = this.toolbarStore
.select(fromToolbarStore.getSelectedCustomers);
this.selectedCustomers$.subscribe(selected => {
this.selectedCustomersPeople$ = this.getSelectedCustomersPeople();
});
}
getSelectedCustomersPeople(): Observable<Person[]> {
return combineLatest(this.selectedCustomers$, this.people$, (customers, people) => {
const allSelectedPeople = customers.map(
customer => Object.assign(people.filter(
person => person.company === customer.id
))
);
const flattenSelectedPeople = [].concat.apply([], allSelectedPeople);
return flattenSelectedPeople;
});
}
applyFilter(filterValue = ' ') {
filterValue = filterValue.trim();
filterValue = filterValue.toLowerCase();
this.selectedCustomersPeople$ = filterValue;
// Would like to filter here not sure how
}
模板
<mat-table #table [dataSource]="selectedCustomersPeople$ | async"
matSort
[@animateStagger]="{ value: '50' }">
<!-- Name Column -->
<ng-container cdkColumnDef="firstName">
<mat-header-cell *cdkHeaderCellDef mat-sort-header>First Name</mat-header-cell>
<mat-cell *cdkCellDef="let person">
<p class="text-truncate font-weight-600">
{{ person.firstName }} {{ person.familyName }}
</p>
</mat-cell>
</ng-container>
<mat-header-row *cdkHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *cdkRowDef="let person; columns: displayedColumns;"
class="person"
(click)="editPerson(person)"
[ngClass]="{'mat-light-blue-50-bg': checkboxes[person.id]}"
matRipple
[@animate]="{ value: '*', params: { y: '100%' } }">
</mat-row>
</mat-table>
【问题讨论】: