开发者

How to loop through children in MXML View?

开发者 https://www.devze.com 2023-02-14 19:42 出处:网络
I have mxml component and want to go through all its children, that I\'ve created in it and not to go deeper. For example I have this view:

I have mxml component and want to go through all its children, that I've created in it and not to go deeper. For example I have this view:

<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx"
         xmlns:chat="org.mirapolis.virtualroom.chat.*"
         height="100%" width="100%" 
         initialize="init()" >
    <fx:Script>
        <![CDATA[                   
            import org.mirapolis.virtualroom.chat.ChatModule;

            private function init() : void{
                setDisplayStrategy(this);
            }
            private function setDisplayStrategy(element :  UIComponent) : void{
                for(var i : int = 0; i < element.numChildren; i++){
                    var child : UIComponent = element.getChildAt(i) as UIComponent;
                    if (child != null && child.descriptor != null){
                        trace(child);
                        setDisplayStrategy(child);
                    }
                }
            }

        ]]>
    </fx:Script>
    <mx:VBox>
        <mx:Form>
            <mx:FormItem label="email">
                <s:TextInput id="email" />
            </mx:FormItem>
            <mx:FormItem>
                <s:Button
                    label="submit" />
            </mx:FormItem>
        </mx:Form>
    </mx:VBox>
    <chat:ChatView id="id1" />
    <chat:ChatView id="id2" />
</mx:VBox>

And trace will give me this

videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem10
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.F开发者_Python百科orm9.FormItem10.email
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem16
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem16.Button17
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2

which is exactly what I need. But if I add any spark element to my view, child.descriptor will give null and the whole thing will break up. Without child.descriptor check this code will go deeper in all components which is absolutely not needed.

So my question is - is it possible to get all children that have been added to mxml view?

UPD: Now it works better:

public static function setDisplayStrategy(element : UIComponent) : void{
            trace(element);
            if (element is IVisualElementContainer){
                for(var i : int = 0; i < (element as IVisualElementContainer).numElements; i++){
                    var child : UIComponent = (element as IVisualElementContainer).getElementAt(i) as UIComponent;
                    if (child){
                        setDisplayStrategy(child);
                    }
                }
            }
        }

But still takes too many deeps:

videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem10
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem10.email
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem16
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.VBox8.Form9.FormItem16.Button17
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1.chatWrapper
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1.chatWrapper.ScrollerSkin28.Group24
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1.chatWrapper.ScrollerSkin28.Group24.chatArea
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1.userMessage
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id1.send
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2.chatWrapper
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2.chatWrapper.ScrollerSkin105.Group101
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2.chatWrapper.ScrollerSkin105.Group101.chatArea
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2.userMessage
videoconference.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.MainView7.id2.send

Is it possible to stop on MainView7.id1 and MainView7.id2 in my example?


I think you need to loop over the numElement property.

However, I suggest you read this excellent post here

0

精彩评论

暂无评论...
验证码 换一张
取 消