0

我有一个包含 3 个文档的材料数据表。在这些列中,我有一个需要获取 id ($key) 的编辑列,以便我可以路由到/object:id。

我怀疑这与异步加载有关,但在尽我所能进行彻底搜索后,我无法真正找到可靠的答案,或者诚然,我真的不知道如何将其合并到数据表中.

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

当我如上所述使用集合(可观察的)作为我的数据表源时,我能够获取键并显示它们,但是即使这会显示内容,它也会使数据表功能无用,例如过滤和排序。

这是我的 TS 和模板文件:

(我尝试将相同的值分配给多个属性 [uid, $key] 以确保它不是关键字问题)

import { Component, AfterViewInit, ViewChild } from '@angular/core';
import { MatTableDataSource, MatSort, MatDialog } from '@angular/material';
import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from '@angular/fire/firestore';
import { StatusDialogComponent } from './status-dialog/status-dialog.component';
import { Vehicle } from '../vehicle';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Component({
    moduleId: module.id,
    selector: 'app-list-vehicles',
    templateUrl: 'list-vehicles.component.html',
    styleUrls: ['list-vehicles.component.scss']
})
export class ListVehiclesComponent implements AfterViewInit {
    vehicleKey: {};
    vehicles$: AngularFirestoreCollection<Vehicle>;
    vehicles: Observable<Vehicle[]>;
    vehicle: AngularFirestoreDocument;
    displayedColumns = ['vin', 'make', 'model', 'status', '$key', 'details', 'edit'];
    dataSource: MatTableDataSource<any>;
    @ViewChild(MatSort) sort: MatSort;

    constructor(
        public afs: AngularFirestore,
        public dialog: MatDialog,
    ) {
        this.vehicles = this.afs.collection(`vehicles`).snapshotChanges().pipe(
            map(actions => {
                return actions.map(a => {
                    const data = a.payload.doc.data() as Vehicle;
                    const $key = a.payload.doc.id;
                    console.log('payload: ' + a.payload.doc.id);
                    data.$key = a.payload.doc.id;
                    data.uid = a.payload.doc.id;
                    if (data.$key === null || data.$key === undefined) {console.log('null.'); } else {console.log('full' + data.$key); }
                    return {$key, ...data};
                });
            })
        );
    }

    ngAfterViewInit(): void {
        this.afs.collection<Vehicle>('vehicles').valueChanges().subscribe(data => {
            this.dataSource = new MatTableDataSource(data);
            this.dataSource.sort = this.sort;
        });

    }

    applyFilter(filterValue: string) {
        filterValue = filterValue.trim();
        filterValue = filterValue.toLowerCase();
        this.dataSource.filter = filterValue;
    }

    openStatusDialog(data): void {
        const dialogRef = this.dialog.open(StatusDialogComponent, {
            width: '400px',
            data: data,
        });
    }
    trackByUid(index, item) {
        return item.uid;
    }
}
<div *ngFor="let veh of vehicles | async">
        {{veh.$key}} <!-- I can fetch the keys here. -->
        </div>
<div *ngIf="vehicles | async as vehicle1">
<button mat-raised-button color="accent"> A Useless Button </button>
<div class="example-header">
  <mat-form-field>
    <input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
  </mat-form-field>
</div>
<mat-table #table [dataSource]="dataSource" [trackBy]="trackByUid" matSort>
  <ng-container matColumnDef="vin">
    <mat-header-cell *matHeaderCellDef mat-sort-header>VIN</mat-header-cell>
    <mat-cell *matCellDef="let vehicle"> {{vehicle.vin}} </mat-cell>
  </ng-container>
  <ng-container matColumnDef="make">
    <mat-header-cell *matHeaderCellDef mat-sort-header>Make</mat-header-cell>
    <mat-cell *matCellDef="let vehicle"> {{vehicle.make}} </mat-cell>
  </ng-container>
  <ng-container matColumnDef="model">
    <mat-header-cell *matHeaderCellDef mat-sort-header>Model</mat-header-cell>
    <mat-cell *matCellDef="let vehicle"> {{vehicle.model}} </mat-cell>
  </ng-container>
  <ng-container matColumnDef="status">
    <mat-header-cell *matHeaderCellDef mat-sort-header>Status</mat-header-cell>
    <mat-cell *matCellDef="let vehicle"> {{vehicle.status}} </mat-cell>
  </ng-container>
  <ng-container matColumnDef="$key">
        <mat-header-cell *matHeaderCellDef mat-sort-header>Key</mat-header-cell>
        <mat-cell *matCellDef="let vehicle"> {{vehicle.$key}} </mat-cell>
      </ng-container>
  <ng-container matColumnDef="details">
    <mat-header-cell *matHeaderCellDef mat-sort-header>Details</mat-header-cell>
    <mat-cell *matCellDef="let vehicle"> <button mat-raised-button color="primary" [routerLink]="['/vehicles', vehicle.$key]">
        Details </button> </mat-cell>
  </ng-container>
  <ng-container matColumnDef="edit">
    <mat-header-cell *matHeaderCellDef mat-sort-header>Edit</mat-header-cell>
    <mat-cell *matCellDef="let vehicle"> <button mat-raised-button color="warn" (click)="openStatusDialog(vehicle)">
        Edit </button> </mat-cell>
  </ng-container>
  <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
  <mat-row *matRowDef="let row; columns: displayedColumns;" class="animate"></mat-row>
</mat-table>
Json: {{(vehicle1 | json)}} <!-- I can also fetch them here. -->
</div>

这是我使用“dataSource”作为数据表源的时候: 在此处输入图像描述

这是我使用 observable 作为数据表源的时候: 在此处输入图像描述

由于我能够以某种方式使用可观察异步获取密钥,我如何将类似的方法合并到数据表中,以便我可以使用数据表获取密钥?是否有另一种方法可以使用我可能错过的材料数据表导航到集合中的文档?

4

1 回答 1

0

订阅 observable 来构建 dataSource 而不是订阅集合解决了这个问题。

所以这:

this.afs.collection<Vehicle>('vehicles').valueChanges().subscribe(data => {
        this.dataSource = new MatTableDataSource(data);
        this.dataSource.sort = this.sort;
    });

替换为:

this.vehicles.subscribe((d) => this.dataSource = new MatTableDataSource(d));
于 2019-02-01T21:32:09.560 回答