I'm trying to follow the material example and implement it on my own data, the table renders like it should but pagination, sorting, and filtering aren't working. You can write in the filter box, change amount per page, and press to sort and get an arrow pointing up or down, but nothing changes with the table. I think it has something to do with how I load my data, but I'm unsure exactly what's going on.
Here's my component:
import { Component, OnInit, ViewChild } from '@angular/core';
import { first } from 'rxjs/operators';
import { MatTableDataSource } from '@angular/material/table';
import { MatPaginator, MatSort } from '@angular/material';
import { Invoice } from '../_models';
import { InvoiceService } from '../_services';
@Component({
selector: 'app-invoices',
templateUrl: './invoices.component.html',
styleUrls: ['./invoices.component.css']
})
export class InvoicesComponent implements OnInit {
displayedColumns=['rating', 'amount', 'debtor', 'serial', 'dateout', 'expiration', 'daysleft', 'fid']
invoices: Invoice[] = [];
dataSource= new MatTableDataSource<Invoice>(this.invoices);
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
constructor(
private invoiceService: InvoiceService
) { }
ngOnInit() {
this.loadInvoices();
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
private loadInvoices(){
this.invoiceService.getUserInvoices().pipe(first()).subscribe(invoices => {
this.invoices=invoices;
});
}
applyFilter(filterValue: string) {
filterValue = filterValue.trim(); // Remove whitespace
filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
this.dataSource.filter = filterValue;
if (this.dataSource.paginator) {
this.dataSource.paginator.firstPage();
}
}
}
And here is the html:
<h3> All Uploaded invoices:</h3>
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>
<div class="mat-elevation-z8">
<mat-table #table [dataSource]="invoices" matSort class="mat-elevation-z8">
<ng-container matColumnDef="rating">
<mat-header-cell *matHeaderCellDef mat-sort-header> Rating </mat-header-cell>
<mat-cell *matCellDef="let invoice"> {{invoice.rating}} </mat-cell>
</ng-container>
<ng-container matColumnDef="amount">
<mat-header-cell *matHeaderCellDef mat-sort-header> Amount </mat-header-cell>
<mat-cell *matCellDef="let invoice"> {{invoice.amount}} </mat-cell>
</ng-container>
<ng-container matColumnDef="debtor">
<mat-header-cell *matHeaderCellDef> Debtor </mat-header-cell>
<mat-cell *matCellDef="let invoice"> {{invoice.debtor}} </mat-cell>
</ng-container>
<ng-container matColumnDef="serial">
<mat-header-cell *matHeaderCellDef> Serial </mat-header-cell>
<mat-cell *matCellDef="let invoice"> {{invoice.serial}} </mat-cell>
</ng-container>
<ng-container matColumnDef="dateout">
<mat-header-cell *matHeaderCellDef> Dateout </mat-header-cell>
<mat-cell *matCellDef="let invoice"> {{invoice.dateout}} </mat-cell>
</ng-container>
<ng-container matColumnDef="expiration">
<mat-header-cell *matHeaderCellDef> Expiration </mat-header-cell>
<mat-cell *matCellDef="let invoice"> {{invoice.expiration}} </mat-cell>
</ng-container>
<ng-container matColumnDef="daysleft">
<mat-header-cell *matHeaderCellDef mat-sort-header > Days left </mat-header-cell>
<mat-cell *matCellDef="let invoice"> {{invoice.daysleft}} </mat-cell>
</ng-container>
<ng-container matColumnDef="fid">
<mat-header-cell *matHeaderCellDef> Fid </mat-header-cell>
<mat-cell *matCellDef="let invoice"> {{invoice.fid}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
<mat-paginator [pageSizeOptions]="[3, 100, 200]" showFirstLastButtons></mat-paginator>
</div>
<p><a [routerLink]="['/login']">Logout</a></p>
Lastly the invoice model:
export class Invoice {
id: any;
rating: any;
serial: any;
amount: any;
debtor: any;
dateout: any;
expiration: any;
daysleft: any;
fid: any;
invoicefile: File;
}
Any insight into what might be going wrong?