首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >反应角材料数据表

反应角材料数据表
EN

Stack Overflow用户
提问于 2018-12-13 10:39:23
回答 2查看 2.2K关注 0票数 5

我用这个ng generate @angular/material:material-table命令创建了一个角材料数据表,它给了我以下文件结构:

  • table-datasource.ts
  • table.component.ts
  • table.component.html

这里的想法是在table-datasource.ts中执行所有的抓取、排序和分页。默认情况下,数据被放置在table-datasource.ts内部的数组中,但在我的示例中,数据来自ngxs存储,该存储公开了数组的可观察到的。Atm I有以下实现:

table-datasource.ts:

代码语言:javascript
复制
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 methods

table-component.html:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
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?
  }
}

什么在起作用:

  • 数据呈现正确(通过按钮并通过ngxs存储触发)
  • 我可以整理数据

不起作用的:

  • 在第一次数据加载时,pageSize将被完全忽略,所有行都将被拆分。
  • 单击排序或分页元素时,将获取当前选定的pageSize,并呈现此数量的行。对我来说奇怪的是,这只起下行作用(假设pageSize是10,我选择了5,结果是5行,但是一旦选择了5,就不可能再显示多于5行)

Requirements:

  • 我喜欢将所有的数据操作封装在TableDataSource.connect()之后的想法,因此不需要像这样的解决方案,在这个解决方案中在共体中进行抓取。此外,这没有实现排序。
  • 该应用程序使用的是ngxs商店,非常类似于ngrx,因此任何涉及此部分的解决方案都是受欢迎的。
  • 我还没有弄清楚如何处理pageEvents,所以我猜想解决方案在handlePage()方法中。

版本:

  • RxJS 6.3.x
  • 角7.x
  • ngxs 3.3.x
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-15 09:29:03

我想出了如何为我的需求设置一个表。主要的改变是,我删除了从TableDataSource获取数据的可观察性,并引入了一个DataService

代码语言:javascript
复制
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

代码语言:javascript
复制
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

  • 连接方法中,它持有一个数组,其中包含可能的数据突变: const dataMutations = this._dataChange,this._sort.sortChange,this._filterChange,this._paginator.page;
  • 数组的_dataChange成员通过订阅我们的getAllItems方法获得它的值。 this._internalService.getAllItems().subscribe(data => {this._dataChange.next(数据);};
  • dataMutations用于过滤、排序和返回应该显示的数据: 返回merge(...dataMutations).pipe( map()) => { // Filter data this.filteredData = this._dataChange.value .slice() .filter((item: TokenTableViewItem) => { const searchStr = (item.symbol + item.name).toLowerCase();返回searchStr.indexOf(this.filter.toLowerCase()) !== -1;});//排序过滤后的数据,const sortedData =sortedData //获取过滤后的排序数据的页面切片。this.renderedData = this.getPagedData(sortedData);返回this.renderedData;} );

filterChange在本地实例中定义。

_filterChange = new BehaviorSubject('');

分页排序是通过构造函数从外部触发的。

代码语言:javascript
复制
constructor(
  public _internalService: DataService,
  public _paginator: MatPaginator,
  public _sort: MatSort
) {
  super();
  this._filterChange.subscribe(() => (this._paginator.pageIndex = 0));
}

我还找到了一个在component.html中定义的分页解决方案,如下所示:

代码语言:javascript
复制
<mat-paginator #paginator
  [length]="dataSource.filteredData.length"
  [pageIndex]="pageIndex"
  [pageSize]="pageSize"
  [pageSizeOptions]="pageSizeOptions"
  [showFirstLastButtons]=true>
</mat-paginator>

和在component.ts中设置的变量

代码语言:javascript
复制
pageSizeOptions = [5, 10, 20, 40];
pageSize = this.pageSizeOptions[0];
pageIndex = 0;

完整的代码可以在项目中看到,在whatsmytoken.com中使用该表的一个活动版本。

票数 1
EN

Stack Overflow用户

发布于 2019-06-18 05:23:13

哇!

几乎在同一时间,我写了一篇关于我的反应性DataSource的文章,它可以很容易地扩展到多个数据列表!您可以添加可选的和必需的变异器,并附带getter函数来收集各自的参数并将它们合并到REQuest对象中。

我在这里解释了全部内容:

https://medium.com/@matheo/reactive-datasource-for-angular-1d869b0155f6

我也在StackBlitz上挂载了一个演示,它用简单的提交显示了Github,以一种干净的方式设置一个过滤/排序/分页列表是多么简单。

我希望你能给我一些关于图书馆的反馈,

如果你觉得它很有吸引力,我也可以用你的用例来支持你:)

编码愉快!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53759963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档