我用这个ng generate @angular/material:material-table命令创建了一个角材料数据表,它给了我以下文件结构:
这里的想法是在table-datasource.ts中执行所有的抓取、排序和分页。默认情况下,数据被放置在table-datasource.ts内部的数组中,但在我的示例中,数据来自ngxs存储,该存储公开了数组的可观察到的。Atm I有以下实现:
table-datasource.ts:
export class TokenTableDataSource extends DataSource<TokenTableItem> {
@Select(TokenTableState.getTokenTableItems) private tokenTableItems$:Observable<TokenTableItem[]>;
totalItems$ = new BehaviorSubject<TokenTableItem[]>([]);
constructor(private paginator: MatPaginator, private sort: MatSort) {
super();
}
/**
* Connect this data source to the table. The table will only update when
* the returned stream emits new items.
* @returns A stream of the items to be rendered.
*/
connect(): Observable<TokenTableItem[]> {
this.tokenTableItems$.subscribe(item => this.totalItems$.next(item));
// init on first connect
if (this.totalItems$.value === undefined) {
this.totalItems$.next([]);
this.paginator.length = this.totalItems$.value.length;
}
// Combine everything that affects the rendered data into one update
// stream for the data-table to consume.
const dataMutations = [
observableOf(this.totalItems$),
this.paginator.page,
this.sort.sortChange
];
return merge(...dataMutations).pipe(
map(() => this.totalItems$.next(this.getPagedData(this.getSortedData([...this.totalItems$.value])))),
mergeMap(() => this.totalItems$)
);
}
...generated paging and sorting methodstable-component.html:
<div class="mat-elevation-z8">
<table mat-table class="full-width-table" [dataSource]="dataSource" matSort aria-label="Elements">
...multiple columns
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator #paginator
[length]="this.dataSource.totalItems$.value?.length"
[pageIndex]="pageIndex"
[pageSize]="pageSize"
[pageSizeOptions]="pageSizeOptions"
[showFirstLastButtons]=true
(page)="handlePage($event)">
</mat-paginator>
</div>table.component.ts:
export class TokenTableComponent implements OnInit {
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
dataSource: TokenTableDataSource;
pageSizeOptions = [5, 10, 20, 40];
pageSize = this.pageSizeOptions[0];
pageIndex = 0;
tableLength = 0;
... colums definition
ngOnInit(): void {
this.dataSource = new TokenTableDataSource(this.paginator, this.sort);
}
public handlePage(pageEvent: PageEvent) {
// do what?
}
}什么在起作用:
不起作用的:
pageSize,并呈现此数量的行。对我来说奇怪的是,这只起下行作用(假设pageSize是10,我选择了5,结果是5行,但是一旦选择了5,就不可能再显示多于5行)Requirements:
TableDataSource.connect()之后的想法,因此不需要像这这样的解决方案,在这个解决方案中在共体中进行抓取。此外,这没有实现排序。handlePage()方法中。版本:
发布于 2019-06-15 09:29:03
我想出了如何为我的需求设置一个表。主要的改变是,我删除了从TableDataSource获取数据的可观察性,并引入了一个DataService。
export class DataService {
//the @Select is from ngxs but can be anything returning an Observable
@Select(TokenTableState.getTokenTableItems) private tokenTableItems$: Observable<TokenTableViewItem[]>;
private initValue$: BehaviorSubject<TokenTableViewItem[]> = new BehaviorSubject<TokenTableViewItem[]>([]);
getAllItems(): Observable<TokenTableViewItem[]> {
const sources = [this.tokenTableItems$, this.initValue$];
return merge(...sources);
}
}基本上,该服务从任何可观察的输入中获取数据,并将其与初始值合并到getAllItems方法中。
组件具有此服务的实例:
private _dataService: DataService | null;
它在load方法中将其交给TableDatasource:
private loadData(): any {
this._dataService = new DataService();
this.dataSource = new TokenTableDataSource(
this._dataService,
this.paginator,
this.sort
);
fromEvent(this.filter.nativeElement, 'keyup').subscribe(() => {
if (!this.dataSource) {
return;
}
this.dataSource.filter = this.filter.nativeElement.value;
});
}我在TableDataSource中没有引用DataService的原因是,组件中的分页器需要表的长度来呈现(见下文)。
TableDataSource像这样使用DataService:
filterChange在本地实例中定义。
_filterChange = new BehaviorSubject('');
而分页和排序是通过构造函数从外部触发的。
constructor(
public _internalService: DataService,
public _paginator: MatPaginator,
public _sort: MatSort
) {
super();
this._filterChange.subscribe(() => (this._paginator.pageIndex = 0));
}我还找到了一个在component.html中定义的分页解决方案,如下所示:
<mat-paginator #paginator
[length]="dataSource.filteredData.length"
[pageIndex]="pageIndex"
[pageSize]="pageSize"
[pageSizeOptions]="pageSizeOptions"
[showFirstLastButtons]=true>
</mat-paginator>和在component.ts中设置的变量
pageSizeOptions = [5, 10, 20, 40];
pageSize = this.pageSizeOptions[0];
pageIndex = 0;完整的代码可以在这项目中看到,在whatsmytoken.com中使用该表的一个活动版本。
发布于 2019-06-18 05:23:13
哇!
几乎在同一时间,我写了一篇关于我的反应性DataSource的文章,它可以很容易地扩展到多个数据列表!您可以添加可选的和必需的变异器,并附带getter函数来收集各自的参数并将它们合并到REQuest对象中。
我在这里解释了全部内容:
https://medium.com/@matheo/reactive-datasource-for-angular-1d869b0155f6
我也在StackBlitz上挂载了一个演示,它用简单的提交显示了Github,以一种干净的方式设置一个过滤/排序/分页列表是多么简单。
我希望你能给我一些关于图书馆的反馈,
如果你觉得它很有吸引力,我也可以用你的用例来支持你:)
编码愉快!
https://stackoverflow.com/questions/53759963
复制相似问题