Link to home
Start Free TrialLog in
Avatar of dojjol
dojjolFlag for Afghanistan

asked on

Dynamic Form Creation Flex

Hi Genusies,
I have XML file which have a format like
<name>robert</name>
<last name>jackson</last name>

Now I have to create a dynamic form in Flex which contains form items called name with value "robert"
lastname "jackson".
This form has to be created at runtime so that in case in future if xml xhanges so does the form.

I am still a newbie in flex :)

Thanks in advance for replying to my post.

Thanks,
dojjol


ASKER CERTIFIED SOLUTION
Avatar of bglodde
bglodde
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dojjol

ASKER

Thanks
This is awesome and is perfectly in line with something i was trying to accomplish!!!

i modified this to also allow for method names and dataproviders for form items like comboBox, etc.
NEW XML-------------
<?xml version="1.0"?>
<userinfoform>
        <user>
                <firstname inputtype="TextInput" formlabel="First Name">Robert</firstname>
                <lastname inputtype="TextInput" formlabel="Last Name">Jackson</lastname>
                <venueitems inputtype="ComboBox" formlabel="Venue" dataprovider="venueDataSet" methodname="comboBoxHandler"></venueitems>
                <submitbutton inputtype="Button" buttonlabel="Send Form" methodname="handleFormSubmit"></submitbutton>
        </user>
</userinfoform>


MXML----------------

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="handleCreationComplete()">
        <mx:Script>
        <![CDATA[
	    	import mx.controls.ComboBox;
	    	import mx.collections.ArrayCollection;
	    	import mx.controls.Button;
	        import mx.containers.FormItem;
	        import mx.controls.Label;
	        import mx.controls.TextInput;
	        
	        import mx.controls.Alert;
	        import mx.rpc.events.FaultEvent;
	        import mx.rpc.events.ResultEvent;
	        
	        public var venueDataSet:ArrayCollection = new ArrayCollection();
	        public var labelList:Array = new Array("New York", "Orlando", "Cincinnati");
	        public var dataList:Array = new Array("NY", "OL", "CN");
	        
	        private function initDataSet():void
	        {
				for (var i:int = 0; i < dataList.length; i++){
					var newItem:Object = new Object();
					newItem.data = dataList[i];
					newItem.label = labelList[i];
					  
					venueDataSet.addItem(newItem);
				}
			}
	
	        
	        private function handleCreationComplete():void
	        {
	                initDataSet();
	                XMLService.send();              
	        }
	        
	        private function errorHandler(evt:FaultEvent):void
	        {
	                Alert.show( "Error: " + evt.fault.message );
	        }
	        
	        private function resultHandler(evt:ResultEvent):void
	        {
	                buildForm(new XML(evt.result));
	        }
	        
	        private function comboBoxHandler( e:Event ):void
	        {
	        	var cBox:ComboBox = e.currentTarget as ComboBox;
	            trace( "You selected: " +  cBox.selectedItem.label );
	            trace( "Data: " +  cBox.selectedItem.data );
	        }
	                
	        private function handleFormSubmit( e:MouseEvent ):void
	        {
				trace( "You clicked the submit button!!!" );	
	        }
	        
	        private function buildForm(xml:XML):void
	        {
	            var lst:XMLList = xml.children();
	            var frmItem:FormItem;
	            var txtInput:TextInput;
	            var btItem:Button;
	            var cBox:ComboBox;
	            var itemMethod:Function;
	                    
	            for(var i:int = 0; i < lst.length(); i++){
	                var x:XMLList = lst[i].children();
	                
	                for(var a:int = 0; a < x.length(); a++){
	                    if(x[a].@inputtype == 'TextInput'){
	                        frmItem = new FormItem();
	                        frmItem.direction = "horizontal";
	                        frmItem.label = x[a].@formlabel;
	                        
	                        txtInput = new TextInput();
	                        txtInput.text = x[a];
	                        frmItem.addChild(txtInput);
	                        
	                    }else if(x[a].@inputtype == 'Button'){
							frmItem  = new FormItem();
	                        frmItem.direction = "horizontal";
	                        frmItem.label = x[a].@formlabel;
	                        
	                       	btItem = new Button();
	                        btItem.label = x[a].@buttonlabel;
	                        itemMethod = this[ x[a].@methodname ] as Function;
	                        btItem.addEventListener(MouseEvent.CLICK, itemMethod);
	                        frmItem.addChild(btItem);
	                                               
	                    }else if(x[a].@inputtype == 'ComboBox'){
	                    	frmItem  = new FormItem();
	                        frmItem.direction = "horizontal";
	                        frmItem.label = x[a].@formlabel;
	                        
	                        cBox = new ComboBox();
	                        itemMethod = this[ x[a].@methodname ] as Function;
	                        cBox.dataProvider = this[ x[a].@dataprovider ] as ArrayCollection;
	                        cBox.addEventListener(Event.CHANGE, itemMethod);
	                        frmItem.addChild(cBox);
	                    	
	                    }else{
	                    	// Support other form field types
	                    	                    	
	                    }
	                    
	                    userInfoForm.addChild(frmItem);
	                    
	                }
	            }
	        }
	                
	    ]]>
        </mx:Script>
        <mx:HTTPService fault="errorHandler(event)" id="XMLService" resultFormat="e4x" url="assets/form_1.xml" result="resultHandler(event)" />
        <mx:Form id="userInfoForm" />
</mx:Application>

Open in new window