【问题标题】:How to bind xml node value to dropdown data field in Flex?如何将 xml 节点值绑定到 Flex 中的下拉数据字段?
【发布时间】:2010-11-02 20:26:18
【问题描述】:

可能是一个新手问题,但是在浏览了'网络后,仍然找不到答案......我有一个这样的 XML 对象:

<questionpools>
 <questionpool id="1">
  <name>Sample test bank</name>
  <description>This is a Sample test bank description</description>
  <createdate>2010.10.10</createdate>
  <moddate>2010.10.11</moddate>
  <createdby>testuser</createdby>
  <modby>testuser</modby>
</questionpool>
<questionpool id="2">
  <name>alme</name>
  <description>newpool</description>
  <createdate>2010.10.31</createdate>
  <moddate>2010.10.31</moddate>
  <createdby>testuser</createdby>
  <modby>testuser</modby>
</questionpool>
<questionpool id="9">
  <name>pool_new</name>
  <description>newpool</description>
  <createdate>2010.10.31</createdate>
  <moddate>2010.10.31</moddate>
  <createdby>testuser</createdby>
  <modby>testuser</modby>
</questionpool>

我将此文件加载到一个 XML 变量中:

var poolMenuXML:XMLList = questionpoolsXML.questionpools;
poolMenu = new XMLListCollection(poolMenuXML.children());

并将“名称”节点绑定到下拉列表的标签字段

<s:DropDownList id="s_poolnumber" dataProvider="{poolMenu}" labelField="name"></s:DropDownList>

但是如何将 id 属性添加为下拉列表的“数据”字段,以便在选择项目时返回该字段?

我应该创建一个使用@id 属性作为“数据”值来源的自定义组件吗? (我也尝试添加一个可能有帮助的节点,但不幸的是这也不起作用......)

谢谢 彼得

【问题讨论】:

    标签: apache-flex drop-down-menu dataprovider


    【解决方案1】:

    稍后传递整个对象并获取 id 属性。见 onDropDownListChange 方法

    <?xml version="1.0" encoding="utf-8"?>
    <s:Application 
        minHeight="600"
        minWidth="955"
        creationComplete="application1_creationCompleteHandler(event)"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
    
        <fx:Script>
            <![CDATA[
                import mx.collections.XMLListCollection;
                import mx.events.FlexEvent;
    
                import spark.events.IndexChangeEvent;
    
                [Bindable] private var poolMenu:XMLListCollection;
    
                private var questionpoolsXML:XML = <questionpools>
                        <questionpool id="1">
                            <name>Sample test bank</name>
                            <description>This is a Sample test bank description</description>
                            <createdate>2010.10.10</createdate>
                            <moddate>2010.10.11</moddate>
                            <createdby>testuser</createdby>
                            <modby>testuser</modby>
                        </questionpool>
                        <questionpool id="2">
                            <name>alme</name>
                            <description>newpool</description>
                            <createdate>2010.10.31</createdate>
                            <moddate>2010.10.31</moddate>
                            <createdby>testuser</createdby>
                            <modby>testuser</modby>
                        </questionpool>
                        <questionpool id="9">
                            <name>pool_new</name>
                            <description>newpool</description>
                            <createdate>2010.10.31</createdate>
                            <moddate>2010.10.31</moddate>
                            <createdby>testuser</createdby>
                            <modby>testuser</modby>
                        </questionpool>
                    </questionpools>;
    
                private function application1_creationCompleteHandler(event:FlexEvent):void
                {
                    poolMenu = new XMLListCollection(questionpoolsXML.children());
                }
    
                private function onDropDownListChange(event:IndexChangeEvent):void
                {
                    trace(s_poolnumber.selectedItem.@id);
                }
            ]]>
        </fx:Script>
    
        <s:DropDownList id="s_poolnumber"
            dataProvider="{poolMenu}"
            labelField="name"
            change="onDropDownListChange(event)"/>
    </s:Application>
    

    【讨论】:

    • 谢谢,它有效! (现在我只需要找到一种方法来过滤 XMLListCollection 以设置下拉值 - 相同的东西,反之亦然)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    相关资源
    最近更新 更多