使用 MatTableDataSource 的 mat-table 中未显示远程数据源
Remote dataSource not displayed in mat-table with MatTableDataSource
我正在尝试从远程服务器使用 mat-table
和 dataSource
实现一个组件。但数据在 table.
中不可见
html
<div class="mat-elevation-z8">
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>
<table mat-table [dataSource]="dataSource" matSort style="width: 100%;">
<ng-container matColumnDef="accountType">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Account Type </th>
<td mat-cell *matCellDef="let e"> {{e.accountType}} </td>
</ng-container>
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef mat-sort-header> ID </th>
<td mat-cell *matCellDef="let e"> {{e.id}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
<tr mat-row *matRowDef="let row; columns: columnsToDisplay;"></tr>
</table>
<mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
</div>
ts
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator, MatTableDataSource, MatSort } from '@angular/material';
import { HeadAccount } from '../../Data/HeadAccount';
import { HeadAccountsService } from '../../Services/head-accounts.service';
import { AfterViewInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-head-accounts',
templateUrl: './head-accounts.component.html',
styleUrls: ['./head-accounts.component.css']
})
export class HeadAccountsComponent implements OnInit {
constructor(private haser: HeadAccountsService) { }
ngOnInit() {
this.gets();
}
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
has: HeadAccount[];
dataSource = new MatTableDataSource(this.has);
columnsToDisplay: string[] = ['accountType', 'id'];
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
gets(): void {
this.haser.gets()
.subscribe(has => this.has = has);
}
applyFilter(filterValue: string) {
this.dataSource.filter = filterValue.trim().toLowerCase();
}
}
但是,如果我将 dataSource
直接更改为 has
,例如 [dataSource]="has"
数据是可见的,但其他功能如 pagination, sort, filter
。
在应用程序模块中,MatTableModule, MatPaginatorModule, MatSortModule
这些是导入和导出
我导入了 lodash 库并做了一个 _.castArray,我想 [...data] 也可以工作,但我选择了 lodash 路线。我还必须将我的排序和分页器包装在 setTimeout 中......不过这可能不适用于你。
this.dataSource = new MatTableDataSource(_.castArray(data));
setTimeout(()=> this.dataSource.sort = this.matSort);
setTimeout(()=> this.dataSource.paginator = this.paginator);
将代码从 ngAfterViewInit()
移动到服务的 .subscribe
对我有用。
在代码段下方工作。
this.haser.gets()
.subscribe(has => {
this.dataSource = new MatTableDataSource(has);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
});
我正在尝试从远程服务器使用 mat-table
和 dataSource
实现一个组件。但数据在 table.
html
<div class="mat-elevation-z8">
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>
<table mat-table [dataSource]="dataSource" matSort style="width: 100%;">
<ng-container matColumnDef="accountType">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Account Type </th>
<td mat-cell *matCellDef="let e"> {{e.accountType}} </td>
</ng-container>
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef mat-sort-header> ID </th>
<td mat-cell *matCellDef="let e"> {{e.id}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
<tr mat-row *matRowDef="let row; columns: columnsToDisplay;"></tr>
</table>
<mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
</div>
ts
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator, MatTableDataSource, MatSort } from '@angular/material';
import { HeadAccount } from '../../Data/HeadAccount';
import { HeadAccountsService } from '../../Services/head-accounts.service';
import { AfterViewInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-head-accounts',
templateUrl: './head-accounts.component.html',
styleUrls: ['./head-accounts.component.css']
})
export class HeadAccountsComponent implements OnInit {
constructor(private haser: HeadAccountsService) { }
ngOnInit() {
this.gets();
}
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
has: HeadAccount[];
dataSource = new MatTableDataSource(this.has);
columnsToDisplay: string[] = ['accountType', 'id'];
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
gets(): void {
this.haser.gets()
.subscribe(has => this.has = has);
}
applyFilter(filterValue: string) {
this.dataSource.filter = filterValue.trim().toLowerCase();
}
}
但是,如果我将 dataSource
直接更改为 has
,例如 [dataSource]="has"
数据是可见的,但其他功能如 pagination, sort, filter
。
在应用程序模块中,MatTableModule, MatPaginatorModule, MatSortModule
这些是导入和导出
我导入了 lodash 库并做了一个 _.castArray,我想 [...data] 也可以工作,但我选择了 lodash 路线。我还必须将我的排序和分页器包装在 setTimeout 中......不过这可能不适用于你。
this.dataSource = new MatTableDataSource(_.castArray(data));
setTimeout(()=> this.dataSource.sort = this.matSort);
setTimeout(()=> this.dataSource.paginator = this.paginator);
将代码从 ngAfterViewInit()
移动到服务的 .subscribe
对我有用。
在代码段下方工作。
this.haser.gets()
.subscribe(has => {
this.dataSource = new MatTableDataSource(has);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
});