2010-11-02 74 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

相關問題