4

so I have a sortable list which are several elements in an *ngFor, which I drag and drop. On drop I would like to get the new order of the items. So I added an event but the order of QueryList remains the same.

Here are some things I have tried with no luck

public getOrder() {

        var records: QueryList<SimpleGridRecord> = this.simpleGridRecords.length > 0 || this.simpleGridDraggable.simpleGridRecords;
        records.notifyOnChanges()
        // this.cd.detectChanges();
        records.forEach((s:SimpleGridRecord) => {
            console.log(s.index);
            console.log(s.item.getKey('id'));
        });

    }

is there anyway to get the updated order list of elements?

I keep index and id in my objects which I loop over

enter image description here

regards

Sean

1
  • Can you create a plunker? Commented Feb 26, 2017 at 19:08

1 Answer 1

1

You can use @ContentChildren() or @ViewChildren() and subscribe to changes:

@ViewChildren(SimpleGridRecord) records: QueryList<SimpleGridRecord>;

ngAfterViewInit() {
  this.records.changessubscribe(val => console.log(this.records.toArray()));
}
Sign up to request clarification or add additional context in comments.

5 Comments

tx for the answer, that's exactly what I do, but the order of items is not the same order as it is in the HTML <li>
I added an image and as you can see, after a drop, X is first in the Query but Y is first in the UI :(
Do you update the order by direct DOM manipulation, or by changing the order of the array you use in *ngFor?
yes I update the order by direct DOM manipulation as I do a drag and drop to change the order of the items, which I have to touch the DOM
I thunk you should afterwards change the order in the array to let *ngFor redraw in the new order, them also the query will match.

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.