开发者

actionscript switch statement not working in flex

开发者 https://www.devze.com 2022-12-22 05:34 出处:网络
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

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&g开发者_开发技巧t;
<passing_number_id>4</passing_number_id>
<site_id>1</site_id>
</student>
</students>

Thanks for any response.


switch (element.student_state.toString()) {...

You need the value of the xml element, not the element itself for the comparison.


You had a couple of errors stacked on top of one another - I believe your most recent one - the multiname reference error - has to do with more than one class with the same name (TextInput) being referenced in your code. Could be that flex is adding an import statement to one automatically at the head of your class.


Ok I think I see the problem now,

for each (var element:XML in students)

will only loop once, because the whole students XML is in students, which has no student_state property, you probably mean

for each (var element:XML in students.children())

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.

0

精彩评论

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