I have a students xml from a database. The student_state column can have a value of “passed”, among other values (hence the need for switch statement). When the list_changeHandler function is called, depending on the value of student_state, I want a form to display different fields. So I tried to dynamically create the form in actionscript, but it fails to show up when I run the flex application:
import mx.containers.Form;
import mx.containers.FormItem;
import mx.containers.HBox;
import mx.controls.Button;
import mx.controls.ComboBox;
import mx.controls.TextArea;
import mx.controls.TextInput;
[Bindable]
public var students:XML;
private const CONTEXT_URL:String = "http://localhost:3000";
protected function textInput_enterHandler():void
{
currentState='List';
}
protected function list_changeHandler():void
{
currentState='Detail';
for each (var element:XML in students)
{
switch (element.student_state) {
case "passed":
setPass("passing_number_id", "created_at");
break;
}
}
}
function setPass(label:String, contents:String):void
{
var form:Form;
var formItem:FormItem;
var textInput:TextInput;
var form = new Form();
var formItem = new FormItem();
var textInput = new TextInput();
form.addChild(formItem)
formItem.addChild(textInput)
addChild(form)
// form.includedIn = "Detail"
form.x = -12
form.y = 150
form.id = "detailView"
form.label = label;
formItem.label = label;
textInput.id = label + "TextInput";
textInput.text = "@{studentsGrid.selectedItem.label}";
}
Here’s the xml:
<students>
<student>
<student_state>passed</student_state>
<created_at>2010-02-19T17:44:34Z</created_at>
<passing_number_id>4</passing_number_id>
<site_id>1</site_id>
</student>
</students>
Thanks for any response.
Ok I think I see the problem now,
will only loop once, because the whole students XML is in students, which has no student_state property, you probably mean
then you are looping on the student node, which does have the child student_state. So that should work, even without the toString() method (not sure on that point, so you might want to try it both ways).
Familiarize yourself with trace() which you can use to output info to the console. if you had added a
trace(element.toXMLString())inside your for loop you would have seen this for yourself.