Angular 角材料数据表更新
我用有角度的材料实现了有角度的 我从API中读取数据,并使用以下代码填充表:Angular 角材料数据表更新,angular,rxjs,angular-material,Angular,Rxjs,Angular Material,我用有角度的材料实现了有角度的 我从API中读取数据,并使用以下代码填充表: @ViewChild('filter') filter: ElementRef; dataSource: MyDataSource | null; dataSubject = new BehaviorSubject<any[]>([]); displayedColumns = ['name']; constructor(private appService: Applications
@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 {
}
}
但是表不会更新
一种解决方案是在HTML中定义一个操作列,如下所示:
<ng-container matColumnDef="actions">
<mat-cell *matCellDef="let model">
<button mat-icon-button matTooltip="Edit" (click)="edit(model)">
<mat-icon>edit</mat-icon>
</button>
</mat-cell>
</ng-container>
}
请记住,元素和响应必须是同一类型的一种解决方案是在HTML中定义一个操作列,如下所示:
<ng-container matColumnDef="actions">
<mat-cell *matCellDef="let model">
<button mat-icon-button matTooltip="Edit" (click)="edit(model)">
<mat-icon>edit</mat-icon>
</button>
</mat-cell>
</ng-container>
}
请记住,要素和回答必须是同一类型的您的第二个问题将在您在问题中引用的页面中解决。您的第二个问题将在您在问题中引用的页面中解决。
<ng-container matColumnDef="actions">
<mat-cell *matCellDef="let model">
<button mat-icon-button matTooltip="Edit" (click)="edit(model)">
<mat-icon>edit</mat-icon>
</button>
</mat-cell>
</ng-container>
private edit(element?: Model) {
// In this case i use Mat-Dialog from edition
let dialogRef = this.dialog.open(EditDialogComponent,
{
panelClass: 'mat-dialog-lg',
data: { element: element }
});
dialogRef.afterClosed().subscribe(response => {
if (response) {
// For change the data in the table list you have to assign the response to your element
element = response;
}
});