I use angular 8 in my front application but I have this error :
Cannot read property 'sortChange' of undefined
I try add pagination in my datatable(pagination for communication with server)
code component:
// Angular
import { Component, ElementRef, OnInit, ViewEncapsulation, ViewChild, AfterViewInit } from '@angular/core';
import { MatPaginator, MatSort } from '@angular/material';
import { MatTableDataSource } from '@angular/material/table';
import { PartnerService } from '../../../../core/partner/services/partner.service';
import { PartnerDataSource } from '../../../../core/partner/services/partner.datasource.service';
import { merge, Subscription } from 'rxjs';
import { tap } from 'rxjs/operators';
import { SelectionModel } from '@angular/cdk/collections';
@Component({
selector: 'kt-partner-list',
templateUrl: './partner-list.component.html',
encapsulation: ViewEncapsulation.None
})
export class PartnerListComponent implements AfterViewInit, OnInit {
title = 'mte-test';
displayedColumns = ['label', 'email', 'personalPhone', 'nature', 'type', 'organization'];
@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
@ViewChild(MatSort,{static: false}) sort: MatSort;
dataSource: PartnerDataSource;
@ViewChild('input',{static: true}) input: ElementRef;
selection = new SelectionModel<Element>(true, []);
listPartners: Element[] = [];
/**
*
* @param partnerService: PartnerService
*/
constructor(
private partnerService: PartnerService
){}
ngOnInit(): void {
this.dataSource = new PartnerDataSource(this.partnerService);
this.dataSource.loadPartners(1);
}
ngAfterViewInit(){
this.sort.sortChange.subscribe(() => this.paginator.pageIndex = 0);
merge(this.sort.sortChange, this.paginator.page)
.pipe(
tap(() => {
this.loadPartnerList();
}
)
)
.subscribe();
}
loadPartnerList() {
this.dataSource.loadPartners(this.paginator.pageIndex);
}
}
export interface Element {
label: string;
email: string;
personalPhone: string;
nature: string;
type: string;
organization: string;
}
code html:
<div class=" mat-table__wrapper">
<mat-table class="lmat-elevation-z8" matTableExporter [dataSource]="dataSource"
matSort>
<ng-container matColumnDef="select">
<mat-header-cell *matHeaderCellDef class="mat-column-checkbox">
<mat-checkbox (change)="$event " [indeterminate]="selection.hasValue() && !isAllSelected()">
</mat-checkbox>
</mat-header-cell>
<mat-cell *matCellDef="let element" class="mat-column-checkbox" (click)="$event" (change)="$event">
<mat-checkbox>
</mat-checkbox>
</mat-cell>
</ng-container>
<ng-container matColumnDef="label">
<mat-header-cell *matHeaderCellDef> {{ 'PARTNER.RAISON_SOCIAL' | translate }} </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.label}} </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 [length]="11" [pageSize]="3"
[pageSizeOptions]="[3, 5, 10]"></mat-paginator>
</div>
when page is loaded I have error sort and also when I click in next page pagination dosen't work