Helpful Information
 
 
Category: Apache Flex
Nested Loops for Dynamic ComboBox -- Newbie easy fix??

I have a dynamic xml driven form that I am trying to get the "label" and "value" using nested for loops for the combobox. I am using "droplabel" attribute. The xml schema is final, I cannot change this.

I am having problem targeting the child node attributes.

I keep running into error: "A term is undefined and has no properties." I am sure I am not properly formatting the for loop statement.

I thank anyone for helping me in advance.

---------------------------------------------------------------
XML:

<?xml version="1.0" encoding="utf-8?>

<formdata userid="davis" fname="David" lname="washingon" auth="1">

<element position="1" questionlabel="What is todays time ?">
<item id="1" inputtype="TextInput">Monday</item>
<item id="222" inputtype="TextInput">tonday</item>
</element>


<element position="5" questionlabel="What is your time of arrival ?">
<item id="5" inputtype="ComboBox">
<drop droplabel="Select Answer---" dropvalue="0"></drop>
<drop droplabel="What is this" dropvalue="1" selected="true"></drop>
<drop droplabel="What is that" dropvalue="2"></drop>
</item>
</element>


</formdata>

----------------------------------------------------------------------

Flex Code:


<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundGradientColors="white" backgroundDisabledColor="white" backgroundColor="white" layout="absolute" horizontalAlign="center" creationComplete="handleCreationComplete()" verticalScrollPolicy="auto" cachePolicy="off">

<mx:Script>
<![CDATA[
import mx.controls.Text;
import mx.containers.FormItem;
import mx.controls.Label;
import mx.controls.Button;
import mx.containers.ControlBar;
import mx.controls.TextInput;
import mx.controls.RadioButtonGroup;
import mx.controls.List;
import mx.controls.ComboBox;
import mx.controls.CheckBox;
import mx.controls.FormItemLabel;
import mx.controls.RadioButton;
import mx.controls.TextArea;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.controls.ComboBox;
import mx.collections.ArrayCollection;
import mx.controls.DateField;
import mx.core.IUIComponent;
import mx.core.IUITextField;
import mx.utils.ObjectUtil;
import flash.utils.*;


private function handleCreationComplete():void
{

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 buildForm(xml:XML):void
{
var lst:XMLList = xml.children();
var lsta:XMLList = xml.element.item.children();



for(var i:int = 0; i < lst.length(); i++)
{
var x:XMLList = lst[i].children();





for(var j:int = 0; j < x.length(); j++)
{
if(x[j][email protected] == 'TextInput')
{
var frmItem:FormItem = new FormItem();
frmItem.direction = "vertical";


var tb:TextInput = new TextInput();
tb.text = x[j];
tb.id = x[j][email protected];
tb.toolTip = 'Enter your Answer here';
frmItem.addChild(tb);

userInfoForm.addChild(frmItem);

}

else
{
// Support other form field types


if(x[j][email protected] == 'ComboBox')
{


// Having problem with this nested loop.


for(var d:int = 0; d < lsta.length(); d++)

var p:XMLList = lsta[d].children();

for(var r:int = 0; r < p.length(); r++)



var frmItemCombo:FormItem = new FormItem();



var tg:ComboBox = new ComboBox();


tg.alpha = .5;
tg.text = p[r][email protected];
tg.width = 230;
tg.id = x[j][email protected];
frmItemCombo.required = true;
tg.data = x[j][email protected];


frmItemCombo.addChild(tg);

userInfoForm.addChild(frmItemCombo);



}
}
}
}


}
]]>
</mx:Script>
<mx:HTTPService fault="errorHandler(event)" id="XMLService" resultFormat="e4x" url="test/formdata_final_a.xml" result="resultHandler(event)" method="POST" />

<mx:Panel title="XML Driven Form -- Send Receive Data from SOA _JSP" height="90%" width="70%"
paddingTop="50" paddingBottom="10" paddingLeft="50" paddingRight="10" layout="vertical" horizontalAlign="left" verticalAlign="top" backgroundColor="#EEEFFD" borderColor="#E2E2E2" x="72.5" y="10">
<mx:Form id="userInfoForm" borderThickness="1" borderColor="#FCFAFA" borderStyle="solid" width="90%"/>
</mx:Panel>

</mx:Application>

not exactly sure of how to fix this, but i can point you in the right direction.

the advantage of using XML in flex is the ease of formatting. there's no need to use for loops to iterate through every XML node -- this is a very inefficient way of working with XML and it is a waste of using e4x formatting. if you want to access a specific node in the XML structure, become familiar with the syntax for elements, attributes, and descendants (" . ", " [email protected] ", " .. ").










privacy (GDPR)