2

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?

2

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.