我试图将多选添加到this guide之后的Angular Material表中。它在masterToggle中引用了data
,但是我的UserDataSource中没有该属性。解决此问题的正确方法是什么?
我的组件
dataSource: UserDataSource;
ngOnInit() {
this.dataSource = new UserDataSource(this.svc);
this.dataSource.loadCompanies(this.lgid);
}
masterToggle() {
this.isAllSelected() ?
this.selection.clear() :
this.dataSource.data.forEach(row => this.selection.select(row));
}
用户数据源:
export class UserDataSource implements DataSource<Company> {
private CompanyModelsSubject = new BehaviorSubject<Company[]>([]);
private loadingSubject = new BehaviorSubject<boolean>(false);
public loading$ = this.loadingSubject.asObservable();
constructor(private svc: GreencardService) { }
connect(collectionViewer: CollectionViewer): Observable<Company[]> {
return this.CompanyModelsSubject.asObservable();
}
disconnect(collectionViewer: CollectionViewer): void {
this.CompanyModelsSubject.complete();
this.loadingSubject.complete();
}
loadCompanies(lgid)
{
this.loadingSubject.next(true);
this.svc.getCompanies(lgid).pipe(
catchError(() => of([])),
finalize(() => this.loadingSubject.next(false))
).subscribe(x => { this.CompanyModelsSubject.next(x);});
}
答案 0 :(得分:0)
我相信您所遵循的示例正在使用MatTableDataSource
类,该类具有公共获取器data
...
get data() { return this._data.value; }
私有变量_data
是带有初始化数据的BehaviorSubject
。
constructor(initialData: T[] = []) {
super();
this._data = new BehaviorSubject<T[]>(initialData);
this._updateChangeSubscription();
}
由于创建了自己的类,因此您将需要实现一个类似的公共获取器……可能如下所示。
get data() {return this.CompanyModelsSubject.value; }