Flex removeChildById implementation

Filed under Experiments, Flex Components, Labs

So, on Flex2/3/4 SDK does not have any kind of implementation. I’ve search on web and found that only Sandy 3D engine has one typical method.

For who came or know java as well has a plethora a kind of method available and why not in Flex? Is a thing to think about it.

DisplayObject class is great but avoiding that only simple methods and a lot of workarrounds to handle that.

Any case you might wonder or required kind of method is simple here’s a example of that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:Script>
		<![CDATA[
 
 			public function removeChildById(id:String):void{
				for each( var item:Object in getChildren()){
						if(item['id'] == id){
							removeChild(item as DisplayObject);
						}
				}
 
			} 
		]]>
	</mx:Script>
	<mx:Button id="a" x="387" y="124"/>
	<mx:Button id="b" x="265" y="124"/>
	<mx:Button id="c" x="278" y="73"/>
	<mx:Button x="114" y="280" label="remove by Id" click="removeChildById(fieldId.text)"/>
	<mx:TextInput id="fieldId" x="197" y="280"/>
</mx:Application>

You just implement this method in any custom component that you created.

This method is very utility when you are creating a based tool mockup. Any future ideas or others developers has different approach?

[Updated]
Sencolar sent me a tip that do all stuff, I made a mistake, didn’t tought about that. Thanks to the tip. You can follow the Senocular tip bellow on comments.

6 Comments

  1. Posted July 24, 2008 at 12:53 pm | Permalink

    You should just be able to use

    removeChild(getChildByName(id));

  2. Posted July 24, 2008 at 3:52 pm | Permalink

    I sooooooo wish they would change that ‘id’ prop in MXML to ‘name’ as thats what it actually is as you can see by the comment above.

  3. Posted July 28, 2008 at 2:38 pm | Permalink

    Yeah, Thanks senocular :)

  4. Danilo Ascione
    Posted August 6, 2008 at 7:21 am | Permalink

    Using actionscript you have to set object.name to use getChildbyName.

    example (with image object):

    img.id=”image”;
    application.addChild(img);
    application.getChildByName(img.id); //return null
    application.getChildByName(”image”); //return null

    img1.name=”image1″;
    application.addChild(img1);
    application.getChildByName(img1.name); //return img1
    application.getChildByName(”image1″); //return img1

  5. Posted September 27, 2008 at 2:11 am | Permalink

    That’s not a bad idea, cause a component id is only the same as its name if you set it so. Though I think using a getChildById function –> removeChild(getChildById([id])) would be more flexible:

    public function getChildById(id:String):DisplayObject {
    for each( var item:Object in getChildren()){
    if(item['id'] == id){
    return item as DisplayObject;
    }
    }
    return null;
    }

    The alternative is to monkeypatch UIComponent to set the id as the same as the name (which is determined by mx.util.NameUtil.createUniqueName() ) in the constructor if no id has been set by default (which would be instantiated with an id prop through createComponentsFromDescriptors()).

    Then you would be able to use removeChild(getChildByName(id)) without creating a get/removeChildById utility function.

  6. Santanu.K
    Posted November 17, 2008 at 5:21 am | Permalink

    The ‘getChildByName’ is buggy in Flex environment. It does not return what is expected all the time. It returns null reference while using this. Dmn’

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*