Creating a view cursor on an ArrayCollection in Flex

ปกติเวลาจะหา data จาก ArrayCollection ก็จะวนลูปแล้ว if เอา วันนี้เล่นเน็ตไปมาเลยไปเจอว่าเค้าทำกันแบบนี้ได้

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
< ?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/04/15/creating-a-view-cursor-on-an-arraycollection-in-flex/ -->
<mx:application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();">
 
    <mx:array id="arr">
        <mx:string>One</mx:string>
        <mx:string>Two</mx:string>
        <mx:string>Three</mx:string>
        <mx:string>Four</mx:string>
        <mx:string>Five</mx:string>
    </mx:array>
 
    <mx:arraycollection id="arrColl" source="{arr}" />
 
    <mx:script>
        < ![CDATA[
            import mx.collections.IViewCursor;
            import mx.collections.Sort;
            import mx.collections.SortField;
 
            [Embed("assets/accept.png")]
            public var acceptIcon:Class;
 
            [Embed("assets/exclamation.png")]
            public var exclamationIcon:Class;
 
            private var cursor:IViewCursor;
 
            private function init():void {
                var sortField:SortField = new SortField(null, true);
                var sort:Sort = new Sort();
                sort.fields = [sortField];
 
                arrColl.sort = sort;
                arrColl.refresh();
 
                cursor = arrColl.createCursor();
            }
 
            private function button_click(evt:MouseEvent):void {
                var found:Boolean = cursor.findAny(textInput.text);
                if (found) {
                    img.source = acceptIcon;
                    list.selectedItem = cursor.current;
                } else {
                    img.source = exclamationIcon;
                    list.selectedItem = null;
                }
            }
        ]]>
    </mx:script>
 
    <mx:applicationcontrolbar dock="true">
        <mx:canvas>
            <mx:textinput id="textInput" />
            <mx:image id="img" right="3" verticalCenter="0" />
        </mx:canvas>
        <mx:button id="button"
                label="Find"
                click="button_click(event);" />
    </mx:applicationcontrolbar>
 
    <mx:list id="list"
            dataProvider="{arrColl}"
            width="100"
            rowCount="{arrColl.length}" />
 
</mx:application>

ผลการรันก็ได้อย่างนี้คับ
Continue reading “Creating a view cursor on an ArrayCollection in Flex” »

Cloning ArrayCollection

There are plenty of occasions where you might want to be able to create an exact clone of a complex/nested data objects, without your new object simply referencing the old one’s values. To do this, you need to force the creation of new values and references. Depending on the complexity of the data stored, this will be achieved through shallow or deep copying:

Continue reading “Cloning ArrayCollection” »