【问题标题】:Angular Material Data Table with sorting, pagination, and filtering using Firestore使用 Firestore 进行排序、分页和过滤的 Angular 材质数据表
【发布时间】:2018-04-24 17:03:07
【问题描述】:

我目前有一个表格,其中填充了来自 Firestore 的数据。我在实现排序、分页和过滤时遇到了麻烦,我希望有人能为我解释一下。下面是我的服务、组件和 html。我在 material.angular.io 上看到了不同的示例,但是他们使用的示例数据库让我很反感。

服务:

import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from 'angularfire2/firestore';
import { Account } from './../models/account.model';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class AccountService {
  accountsCollection: AngularFirestoreCollection<Account>;
  accounts: Observable<Account[]>;

  constructor(public afs: AngularFirestore) {
    this.accountsCollection = afs.collection('accounts');

    this.accounts = this.accountsCollection.snapshotChanges().map(changes => {
      return changes.map(a => {
        const data = a.payload.doc.data() as Account;
        data.id = a.payload.doc.id;
        return data;
      });
    });
  }

  getAccounts() {
    return this.accounts;
  }

 }

组件:

import { Account } from './../../../models/account.model';
import { Component, ViewChild, OnInit } from '@angular/core';
import { DataSource } from '@angular/cdk/collections';
import { MatPaginator, MatSort } from '@angular/material';
import { Observable } from 'rxjs/Observable';
import { AccountService } from '../../../services/account.service';

@Component({
  selector: 'app-account-table',
  templateUrl: './account-table.component.html',
  styleUrls: ['./account-table.component.css']
})
export class AccountTableComponent implements OnInit {
  dataSource = new AccountDataSource(this.accountService);
  displayedColumns = [
    'salesStep',
    'status',
    'idn',
    'hospital',
    'state',
    'regionalManager',
    'accountExecutive',
    'clientLiaison',
    'gpo'
  ];

  constructor(private accountService: AccountService) {}

  ngOnInit() {

  }

}

export class AccountDataSource extends DataSource<any> {

  constructor(private accountService: AccountService) {
    super();
  }

  connect(): Observable<Account[]> {
    return this.accountService.getAccounts();
  }

  disconnect() {}

}

html:

<div class="example-header">
  <mat-form-field>
    <input matInput #filter placeholder="Search">
  </mat-form-field>
</div>

<mat-card class="example-container">

  <mat-table #table [dataSource]="dataSource" matSort>

    <!--- Note that these columns can be defined in any order.
          The actual rendered columns are set as a property on the row definition" -->

    <!-- Sales Step Column -->
    <ng-container matColumnDef="salesStep">
      <mat-header-cell *matHeaderCellDef mat-sort-header> Sales Step </mat-header-cell>
      <mat-cell *matCellDef="let row"> {{row.salesStep}} </mat-cell>
    </ng-container>

    <!-- Status Column -->
    <ng-container matColumnDef="status">
      <mat-header-cell *matHeaderCellDef mat-sort-header> Status </mat-header-cell>
      <mat-cell *matCellDef="let row"> {{row.status}} </mat-cell>
    </ng-container>

    <!-- IDN Column -->
    <ng-container matColumnDef="idn">
      <mat-header-cell *matHeaderCellDef mat-sort-header> IDN </mat-header-cell>
      <mat-cell *matCellDef="let row"> {{row.idn}} </mat-cell>
    </ng-container>

    <!-- Hospital Column -->
    <ng-container matColumnDef="hospital">
      <mat-header-cell *matHeaderCellDef mat-sort-header> Hospital </mat-header-cell>
      <mat-cell *matCellDef="let row"> {{row.hospital}} </mat-cell>
    </ng-container>

    <!-- State Column -->
    <ng-container matColumnDef="state">
      <mat-header-cell *matHeaderCellDef mat-sort-header> State </mat-header-cell>
      <mat-cell *matCellDef="let row"> {{row.state}} </mat-cell>
    </ng-container>

    <!-- Regional Manager Column -->
    <ng-container matColumnDef="regionalManager">
      <mat-header-cell *matHeaderCellDef mat-sort-header> RM </mat-header-cell>
      <mat-cell *matCellDef="let row"> {{row.regionalManager}} </mat-cell>
    </ng-container>

    <!-- Account Executive Column -->
    <ng-container matColumnDef="accountExecutive">
      <mat-header-cell *matHeaderCellDef mat-sort-header> AE </mat-header-cell>
      <mat-cell *matCellDef="let row"> {{row.accountExecutive}} </mat-cell>
    </ng-container>

    <!-- Client Liaison Column -->
    <ng-container matColumnDef="clientLiaison">
      <mat-header-cell *matHeaderCellDef mat-sort-header> CL </mat-header-cell>
      <mat-cell *matCellDef="let row"> {{row.clientLiaison}} </mat-cell>
    </ng-container>

    <!-- GPO Column -->
    <ng-container matColumnDef="gpo">
      <mat-header-cell *matHeaderCellDef mat-sort-header> GPO </mat-header-cell>
      <mat-cell *matCellDef="let row"> {{row.gpo}} </mat-cell>
    </ng-container>



    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns;">
    </mat-row>
  </mat-table>

  <!-- <div class="example-no-results"
       [style.display]="dataSource.renderedData.length == 0 ? '' : 'none'">
    No accounts found matching filter.
  </div> -->

  <mat-paginator #paginator
                [length]="(accountService.accounts | async)?.length"
                [pageIndex]="0"
                [pageSize]="25"
                [pageSizeOptions]="[5, 10, 25, 100]">
  </mat-paginator>
</mat-card>

【问题讨论】:

  • 您可能希望将 DataSource 更改为新的 MatTableDataSource。 material2-docs-dev.firebaseapp.com/components/table/…。请务必升级到 Angular 5 和 Material RC。我会向您推荐我在 S.O. 上的回答。对于 Firebase,但它们现在大多已过时。我也在为新设置而苦苦挣扎,今天正在努力。当我找到解决方案时,它对您没有帮助,因为您的旧表设置似乎已被弃用。
  • 我目前正在使用 Angular 5 和“@angular/material”:“^2.0.0-beta.12”,但看起来我需要使用快照构建(npm install --save angular/material2-builds angular/cdk-builds) 来访问 MatTableDataSource 是否正确?
  • 我刚刚完成了快照构建的安装并使用 MatTableDataSource 实现了所有内容,它的工作原理就像一个魅力!非常感谢!
  • 是的,幸运的是,新的 Mat Table 设置更快、更容易。

标签: angular sorting datatable angular-material google-cloud-firestore


【解决方案1】:

我能够通过安装快照构建 (npm install --save angular/material2-builds angular/cdk-builds) 和使用 MatTableDataSource 来实现分页、排序和过滤。有关如何使用它的示例:https://material2-docs-dev.firebaseapp.com/components/table/examples

【讨论】:

    猜你喜欢
    • 2018-04-24
    • 2018-01-01
    • 2018-06-22
    • 2018-09-13
    • 1970-01-01
    • 2020-01-22
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    相关资源
    最近更新 更多