I have an Angular Component that is taking an @Input
(result of a search) which I want to use as the datasource of a Angular Material table.
Unfortunately, the table doesn't seem to recognize when the variable in @Input
changes and always stays empty.
My workflow is this:
- main view consists of 2 components: search and search-result.
- search defines the search parameters and gets the result from a searchservice.
- search notifies main that it has a new result and main stores result in a variable.
- search-result gets the variable as @Input and updates the table with the results.
Everything works up until console.log(this.passengerSearchReadModels);
which prints the correct result, but the last step (updating the table) doesn't :(
main.html
<div class="mx-5">
<app-search (notify)="onSearch($event)"></app-search>
<app-search-result [passengerSearchReadModels]="passengerSearchReadModels"></app-search-result>
</div>
main.ts
@Component({
selector: 'app-main',
templateUrl: './main.component.html',
styleUrls: ['./main.component.scss']
})
export class MainComponent implements OnInit {
passengerSearchReadModels: PassengerSearchReadModel[] = [];
constructor() { }
ngOnInit(): void {
}
onSearch(passengerSearchReadModels: PassengerSearchReadModel[]): void {
console.log('in onSearch');
this.passengerSearchReadModels = passengerSearchReadModels;
console.log(this.passengerSearchReadModels); // here the correct result is displayed in the console
}
}
search.ts
@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.scss']
})
export class SearchComponent implements OnInit {
@Output() notify: EventEmitter<PassengerSearchReadModel[]> = new EventEmitter<PassengerSearchReadModel[]>();
searchQuery: SearchQuery;
passengerSearchReadModels: PassengerSearchReadModel[] = [];
constructor(private searchService: SearchService) {
this.searchQuery = {
lastName: null
};
this.init();
}
ngOnInit(): void {
}
init(): void {
}
search(): void {
this.searchService.getPassengers(this.searchQuery)
.subscribe(result => {
console.log(result);
this.passengerSearchReadModels.push(result);
}, error => console.error(error));
this.notify.emit(this.passengerSearchReadModels);
}
}
search-result.html:
<table mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="fullName">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Name, Vorname</th>
<td mat-cell *matCellDef="let element">{{element.lastName}}, {{element.firstName}}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
search-result.ts
@Component({
selector: 'app-search-result',
templateUrl: './search-result.component.html',
styleUrls: ['./search-result.component.scss']
})
export class SearchResultComponent implements AfterViewInit {
@Input() passengerSearchReadModels: PassengerSearchReadModel[] = [];
@ViewChild(MatSort, { static: true }) sort: MatSort;
displayedColumns: string[] = [ColumnNames.FullName];
dataSource = new MatTableDataSource<PassengerSearchReadModel>(this.passengerSearchReadModels);
selection = new SelectionModel<PassengerSearchReadModel>(true, []);
constructor() {
}
ngAfterViewInit(): void {
this.dataSource.sortingDataAccessor = this.sortingDataAccessor;
this.dataSource.sort = this.sort;
}
// sorting deleted bc irrelevant
}
#Edit search.html
<div class="shadow">
<div class="row">
<div class="col-2">
<mat-form-field>
<mat-label>{{ "search.lastname" | translate }}</mat-label>
<input matInput type="text" max="100" [(ngModel)]="searchQuery.lastName" />
</mat-form-field>
</div>
</div>
<div class="row">
<div class="col-3">
<button mat-stroked-button color="primary" (click)="search()">
<span class="material-icons mr-2">search</span>
{{ "search.searchButton" | translate }}
</button>
</div>
<div class="col-3"></div>
</div>
</div>
searchService.ts
@Injectable({
providedIn: 'root'
})
export class SearchService {
private ENDPOINT_GET = 'Passenger';
constructor(private http: HttpClient) { }
public getPassengers(searchQuery: SearchQuery): Observable<PassengerSearchReadModel> {
let params = new HttpParams();
if (searchQuery.lastName) { params = params.append('lastName', searchQuery.lastName); }
return this.http.get<PassengerSearchReadModel>(environment.API_URL + this.ENDPOINT_GET, { params });
}
}
this.sortingDataAccessor
sortingDataAccessor(item: any, property: string): any {
const columnSortingDataAccessors = ColumnSortingDataAccessor.get();
let sortingDataAccessor: string;
switch (property) {
// cases omitted for readability
default: {
sortingDataAccessor = columnSortingDataAccessors[property];
break;
}
}
return item[sortingDataAccessor];
}
What do I have to change to get the table to update with the new values?