简体   繁体   English

Angular2 +如何从QueryList <>获取正确的元素顺序

[英]Angular2+ how to get proper element order from QueryList<>

so I have a sortable list which are several elements in an *ngFor , which I drag and drop. 所以我有一个可排序的列表,它是*ngFor中的几个元素,我拖放。 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. 所以我添加了一个事件,但QueryList的顺序保持不变。

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 我将索引和id保存在我循环的对象中

在此输入图像描述

regards 问候

Sean 肖恩

You can use @ContentChildren() or @ViewChildren() and subscribe to changes: 您可以使用@ContentChildren()@ViewChildren()并订阅更改:

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

ngAfterViewInit() {
  this.records.changessubscribe(val => console.log(this.records.toArray()));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM