Reputation: 45
I use Angular 4.4.6 in my project. Having a list of events I want this list to be sorted by date. I achieved this by adding ng2-order-pipe
// customers.html
<tr *ngFor="let event of customer.events | orderBy: order : reverse">
The sorting works but my problem is that if I add an event, it is placed at the end of the list, ignoring sorting.
// customers.ts
this.order = 'date';
this.reverse = true;
.........................................
addEvent(new_date, new_state, new_description) {
var new_event = {
date: new_date,
state: new_state,
description: new_description,
customer_id: this.customer['id']
};
this.customer['events'].push(new_event);
}
How to add an event so that it appears in its place according to sorting?
Upvotes: 2
Views: 687
Reputation: 275927
From : https://angular.io/guide/pipes#pure-and-impure-pipes
Angular ignores changes within (composite) objects. It won't call a pure pipe if you change an input month, add to an input array, or update an input object property.
Reassign the array e.g.
this.customer['events'] = this.customer['events'].concat(new_event);
Upvotes: 3