angular - Angular 材料 5.2.4 - 无表格数据
问题描述
我有角材料表的问题。我不知道为什么它不起作用。插件工作,分页也是,我可以从 http 登录到控制台数据,但插件不显示行。表的数据是对象数组,它看起来与角度示例中的相同
.html
<mat-table #table [dataSource]="dataSource" class="" matSort matSortActive="name" matSortDisableClear matSortDirection="asc">
<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef>Nazwa</mat-header-cell>
<mat-cell *matCellDef="let row">{{ row.name }}</mat-cell>
</ng-container>
<ng-container matColumnDef="description">
<mat-header-cell *matHeaderCellDef>Opis</mat-header-cell>
<mat-cell *matCellDef="let row">{{ row.description }}</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
<mat-paginator [length]="resultsLength" [pageSize]="30">
</mat-paginator>
</div>
.ts
@Component({
selector: 'app-servicepointstablecomponent',
templateUrl: './servicepointstable.component.html'
})
@Injectable()
export class ServicepointstableComponent implements OnInit{
dataSource = new MatTableDataSource();
displayedColumns = ['name', 'description'];
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
@Output() forDisplayRow = new EventEmitter<ServicePoint>();
resultsLength = 0;
isLoadingResults = true;
isRateLimitReached = false;
public selectedRow: ServicePoint;
constructor(private tableService: TableService) {
}
ngOnInit() {
this.sort.sortChange.subscribe(() => this.paginator.pageIndex = 1);
merge(this.sort.sortChange, this.paginator.page)
.pipe(
startWith({}),
switchMap(() => {
this.isLoadingResults = true;
return this.tableService!.getTableDataNew2<ServicePoint>(
this.sort.active, this.sort.direction, this.paginator.pageIndex);
}),
map(data => {
this.isLoadingResults = false;
this.isRateLimitReached = false;
//this.resultsLength = data.total_count;
this.resultsLength = 100;
console.log(data);
return data.items;
}),
catchError(() => {
this.isLoadingResults = false;
this.isRateLimitReached = true;
return observableOf([]);
})
).subscribe(data => this.dataSource.data = data);
}
applyFilter(filterValue: string) {
filterValue = filterValue.trim();
filterValue = filterValue.toLowerCase();
}
onSelect(row: ServicePoint): void {
this.tableService.getRowNew<ServicePoint>(row.idServicePoint).subscribe(rr => {
this.selectedRow = rr; console.log(rr);
this.forDisplayRow.emit(rr);});
}
onDelete(row: ServicePoint): void {
console.log(`delete row ${row.description} ${row.name}`);
}
}
解决方案
dataSource = new MatTableDataSource();
哥们那里漏了点东西!
那里
.subscribe(data => this.dataSource.data = data)
这不是您创建新数据源的方式。
使用第一种语法:
.subscribe(data => this.dataSource = new MatTableDataSource(data))
推荐阅读
- amazon-web-services - AWS 主题创建零星失败
- css - 使用 CSS 模块和 React 的“composes”关键字的特异性问题
- java - 如何在应用程序未聚焦时记录数据?
- python - WordCloud 停用词无法正常工作
- macos - Apple Push Notification Service (APNS) 未显示按钮 (OSx App)?
- r - 如何模拟一个向量只包含 0、1、2 和给定的向量总和
- onem2m - 充电信息记录
- mysql - 错误 1452 MySQL Load Infile(但数据正确)
- sql - PostgreSQL 在查询返回空的情况下将 NULL 作为一行返回
- python - 根据行和列对矩阵中的所有元素进行排名