Oleg
Oleg

Reputation: 45

Angular 4. How to add item to an array according to sorting

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

Answers (1)

basarat
basarat

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.

Fix

Reassign the array e.g.

this.customer['events'] = this.customer['events'].concat(new_event);

Upvotes: 3

Related Questions