2010-11-02 69 views
4

可能是一个新手的问​​题,但通过“网看后,仍然无法找到答案......我有一个这样的XML对象:如何将xml节点值绑定到Flex中的下拉数据字段?

<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属性为“数据”的值的源的定制组件? (我也尝试添加节点的思维,这可能有助于可惜就是不工作,要么...)

感谢 彼得

回答

4

通行证整个对象,后来取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([email protected]); 
      } 
     ]]> 
    </fx:Script> 

    <s:DropDownList id="s_poolnumber" 
     dataProvider="{poolMenu}" 
     labelField="name" 
     change="onDropDownListChange(event)"/> 
</s:Application> 
+0

谢谢,它的工作原理! (现在我只需要找到一种方法来过滤XMLListCollection来设置下拉值 - 相同的东西,反之亦然) – Peter 2010-11-03 19:38:03

相关问题