角度材料5.2.4 - 没有表格数据

时间:2018-05-15 13:56:00

标签: angular angular-material

我有角度材料表的问题。我无法弄清楚它为什么不起作用.Plugin工作,分页,我可以从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}`);
}

}

1 个答案:

答案 0 :(得分:0)

dataSource = new MatTableDataSource();

在那里错过了什么好友!

然后

.subscribe(data => this.dataSource.data = data)

这不是您创建新数据源的方式 使用第一种语法:

.subscribe(data => this.dataSource = new MatTableDataSource(data))