熱門文章

Ⓔeui.ArrayCollection 用法

ArrayCollection 操作

namespace edwin {

    export class MainUI extends eui.Component {

        protected createChildren(): void {
            super.createChildren();
            this.percentWidth = this.percentHeight = 100;

            let ac = new eui.ArrayCollection();
            ac.source = [1, 2, 3];
            ac.addEventListener(eui.CollectionEvent.COLLECTION_CHANGE, this.onCollectionChange, this);
            ac.addItem(5);//add
            ac.addItemAt(6, 1);//add
            ac.source.sort();
            ac.refresh();//refersh
            ac.removeItemAt(2);//remove
            ac.removeAll();//remove
            ac.source = [1, 2, 3];//reset
            ac.replaceItemAt(7, 1);//replace
            ac.source[1] = 8;
            ac.itemUpdated(1);//update
            // ArrayCollection 比一般 number[], string[]..等 array 物件多了增查修改監控事件
            // 當元素 ArrayCollection 修改視圖也會跟這修改, 看 DataGroup, List 範例就能理解

            let ic: eui.ICollection = ac;
            console.log(ic.length); // 3
            console.log(ic.getItemAt(0)); // 1
            console.log(ic.getItemIndex(8)); // 1
        }

        private onCollectionChange(e: eui.CollectionEvent): void {
            switch (e.kind) {
                case eui.CollectionEventKind.ADD:
                    egret.log("add" + " " + e.currentTarget.source + " " + e.location);
                    break;
                case eui.CollectionEventKind.REFRESH:
                    egret.log("refersh" + " " + e.currentTarget.source + " " + e.location);
                    break;
                case eui.CollectionEventKind.REMOVE:
                    egret.log("remove" + " " + e.currentTarget.source + " " + e.location);
                    break;
                case eui.CollectionEventKind.REPLACE:
                    egret.log("replace" + " " + e.currentTarget.source + " " + e.location);
                    break;
                case eui.CollectionEventKind.RESET:
                    egret.log("reset" + " " + e.currentTarget.source + " " + e.location);
                    break;
                case eui.CollectionEventKind.UPDATE:
                    egret.log("update" + " " + e.currentTarget.source + " " + e.location);
                    break;
            }
        }

    }

}

沒有留言:

張貼留言