【发布时间】:2017-10-15 13:11:46
【问题描述】:
我从 Angular Material 实现了 Angular datatable。
我从我的 API 中读取数据并用它填充我的表,使用以下代码:
@ViewChild('filter') filter: ElementRef;
dataSource: MyDataSource | null;
dataSubject = new BehaviorSubject<any[]>([]);
displayedColumns = ['name'];
constructor(private appService: ApplicationsService) {
this.appService.getAllApps().subscribe({
next: value => this.dataSubject.next(value)
});
}
ngOnInit() {
this.dataSource = new MyDataSource(this.dataSubject);
}
export class MyDataSource extends DataSource<any[]> {
constructor(private subject: BehaviorSubject<any[]>) {
super ();
}
connect (): Observable<any[]> {
return this.subject.asObservable();
}
disconnect ( ): void {
}
}
还有我的模板:
<md-table #table [dataSource]="dataSource">
<!--- Note that these columns can be defined in any order.
The actual rendered columns are set as a property on the row definition" -->
<!-- name Column -->
<ng-container mdColumnDef="name">
<md-header-cell *mdHeaderCellDef> Name </md-header-cell>
<md-cell *mdCellDef="let row" (click)="viewApp(row)"> {{row.name}} </md-cell>
</ng-container>
<md-header-row *mdHeaderRowDef="displayedColumns"></md-header-row>
<md-row *mdRowDef="let row; columns: displayedColumns;"></md-row>
</md-table>
当用户以这种方式单击按钮时,我正在尝试更新表格:
buttonClick() {
this.appService.geAnotherApps()
.subscribe({
next: value => this.dataSubject.next(value)
});
}
但表格没有更新。
- 为什么表没有更新?
- 如何根据输入过滤表?
【问题讨论】:
-
您的第二个问题已在您在问题中引用的页面中解决。
标签: angular rxjs angular-material