I have a Material Datatable that consists of 3 documents. In the columns, I have an edit column which needs to fetch the ids ($key) so I can route to /object:id.
I suspect this is related to async loading but after making a thorough search to the best of my capacity I couldn't really find a solid answer, or admitedly, I don't really know what to search for regarding how to incorporate this into datatable.
<mat-table #table [dataSource]="vehicle1" [trackBy]="trackByUid" matSort>
When I use the collection (observable) as my datatable source as above, I'm able to fetch the keys and display them, however even-though this displays content, it renders the datatable functions useless such as filtering & sorting.
This is my TS & template files:
(I tried assigning the same value to multiple properties [uid, $key] to make sure it's not a keyword issue)
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>
This is when I'm using the "dataSource" as the data table source:
This is when I'm using the observable as the data table source:
Since I'm able to somehow fetch the keys using the observable asynchronous, how do I incorporate a similar method to the data table so I can get the keys using data table? Is there another way of navigating to the documents within a collection using material data tables that I might have missed?
Subscribing to the observable to build dataSource instead of subscribing to the collection resolved the issue.
so this:
this.afs.collection<Vehicle>('vehicles').valueChanges().subscribe(data => {
this.dataSource = new MatTableDataSource(data);
this.dataSource.sort = this.sort;
});
is replaced with this:
this.vehicles.subscribe((d) => this.dataSource = new MatTableDataSource(d));