简体   繁体   中英

How do I dynamically add an object to my Flex 3 Array Collection?

I have thoroughly searched, but have not found an answer to this question. Maybe my question is wrong. I have a total of 30 Children on each Canvas child of my tabNavigator. The code works well for counting and iterating through the children, but when I attempt to add an item to my ArrayCollection, it all falls apart. Here's the code:

    private function addrNewDB():void {
        var q:int = 0;
        var t:int = tabNavigator.numChildren;

    while (q<t){
            var TNG:Array = tabNavigator.getChildren();

            var qnn:Array = TNG[q].getChildren() as Array;
            var gat:int = 0;
            var pat:int = TNG[q].numChildren;
            var newItem:Object = new Object();

            while (gat<pat){

                if (UIComponent(qunn[gat]) is CheckBox){
                    if (qunn[gat].selected == true){
                        var game:String = "Y";
                    }
                    else {
                        gm = "N";
                }
            Alert.show("gat: "+String(gat)+" | pat: "+String(pat)+"\n"+qnn[gat].id+" - "+qnn[gat].label+": "+gm);


            }           
                gat++;
            }
        q++;
        }
    }

What's going on here is that I have tabs that are dynamically added at runtime with a button. Each tab has a canvas upon which are textboxes, labels, checkboxes, and a combobox. There are 30 items in total; seven of them are checkboxes.

I have set up this code to iterate through each child (component) of each Canvas child (pat) of each Tab(t) in my tabNavigator, determine if the component is a CheckBox, see if it is selected, and then Alert me for only the 7 CheckBoxes on each Canvas.

All of this works well. Where I run into a snag is when I attempt to add the new item to the HardwareItems ArrayCollection.

I think that I am just not getting the syntax right. When I try to place some code to add a new item to HardwareItems right after the Alert, it stops Alerting me after the first CheckBox, so I am assuming that it is running into an issue of some kind with the way I've been coding it.

What I would like is some help in correctly adding a new item to the HardwareItems array collection for each of the 7 checkboxes.

I have tried this:

HardwareItems.addItem({merch: lblMerchID.text, 
                               item: qnn(gat).label, 
                               manf: "", 
                               have: gm, 
                               requ: "", 
                               qual: "", 
                               location: "", 
                               id: qnn(gat).id});

and this:

                newItem['merch'] = lblMerchID.text;
                newItem['item'] = qnn(gat).label;
                newItem['qual' = "";
                newItem['loc'] = "";
                newItem['id'] = qnn(gat).id;

                        HardwareItems.addItem(newItem);
                        HardwareItems.refresh();

and this too:

                newItem.merch = lblMerchID.text;
                newItem.item = qnn(gat).label;
                newItem.qual = "";
                newItem.loc = "";
                newItem.id = qnn(gat).id;

                        HardwareItems.addItem(newItem);
                        HardwareItems.refresh();

It's obvious that these are all incorrect ways to accomplish what I want, but I'm just trying anything. Incidentally, none of these coding atrocities threw any errors. When I tried them, though, I got one alert... the first CheckBox which is at 23 out of 30 items.

I've read up on all the documentation about ArrayCollection and Array syntax, and I guess I just don't get it. Any help is appreciated. Thank you.

addItem() method should work anyway.

Please, make sure that you:

  1. initialize collection before filling it out:

    HardwareItems = new ArrayCollection();

  2. initialize objects before filling it out:

    newItem = {};

Anyway, remember, that ArrayCollection has a property 'source', which is actually an array. So, instead of addItem you may use the push() method as following:

var HardwareItems:ArrayCollection = new ArrayCollection();
var newItem:Object = {};
newItem['merch'] = lblMerchID.text;
newItem['item'] = qnn(gat).label;
newItem['qual' = "";
newItem['loc'] = "";
newItem['id'] = qnn(gat).id;
HardwareItems.source.push(newItem);
HardwareItems.refresh();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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