2010-09-08 42 views
1

我正在使用Zend AMF從PHP應用程序中提取一些數據。不過,我無法將數據綁定到簡單的DropDownList控件。 PHP方法是:無法將數據綁定到Flex 4中的DropDownList控件

class Test 
{ 
    public function myMethod() 
    { 
     $res = array(); 
     $res[] = array('NAME' => 'ThisIsATest', 'ID' => 1); 
     return $res; 
    } 
} 

網絡監視器報告該方法正在返回結果。它返回以下爲數組:

​​

下面是Flex代碼:

<?xml version="1.0" encoding="utf-8"?> 
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx" 
         width="500" height="286" 
         creationComplete="initApp()"> 
    <fx:Script> 
     <![CDATA[ 
      import mx.collections.ArrayCollection; 
      import mx.rpc.events.FaultEvent; 
      import mx.rpc.events.ResultEvent; 

      private function myMethodResult(e:ResultEvent):void 
      { 
       searchType.dataProvider = e.result as ArrayCollection; 
      } 

      protected function initApp():void 
      { 
       service.myMethod(); 
      } 

      protected function faultHandler(event:FaultEvent):void 
      { 
       trace(event.fault.faultString); 
      } 
     ]]> 
    </fx:Script> 
    <fx:Declarations> 
     <s:RemoteObject id="service" 
         destination="zend" 
         source="Test" 
         showBusyCursor="true" 
         fault="faultHandler(event)"> 
      <s:method name="myMethod" result="myMethodResult(event)"/> 
     </s:RemoteObject> 
    </fx:Declarations> 
    <s:DropDownList id="searchType" labelField="NAME"/> 
</s:WindowedApplication> 

任何幫助將不勝感激。提前致謝。

+0

當你把'var a:Array = e.result as Array; trace(a.length,a [0],a.join());'在行上面''propertyList.dataProvider = e.result as Array;' – Amarghosh 2010-09-08 13:27:20

+0

我在兩種方法下運行; myMethod = 1,另一個方法= 528。 – Reado 2010-09-08 13:41:35

回答

4

你問關於綁定,但我不認爲這就是你想知道的。我相信答案是在結果處理程序中的這一行:

searchType.dataProvider = e.result as ArrayCollection; 

我假設您從ColdFusion獲取數組。如果內存服務於我,則不能將數組轉換爲ArrayCollection。結果很可能是空的。你有沒有在調試模式下逐步驗證代碼?

相反試試這個:

searchType.dataProvider = new ArrayCollecection(e.result as Array); 

由於e.result是一個通用的對象,你需要將它轉換爲一個數組。

爲了解決答案的綁定部分。綁定有一個來源和一個價值。當源更改時,該值會自動更新。你有一個值(dropDownList.dataProvider),你想改變,但你沒有一個源。代碼中沒有任何內容使用綁定。當結果返回時,您只需手動設置值。爲了結合我可能會改變這樣的代碼中使用:

<?xml version="1.0" encoding="utf-8"?> 
    <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
          xmlns:s="library://ns.adobe.com/flex/spark" 
          xmlns:mx="library://ns.adobe.com/flex/mx" 
          width="500" height="286" 
          creationComplete="initApp()"> 
     <fx:Script> 
      <![CDATA[ 
       import mx.collections.ArrayCollection; 
       import mx.rpc.events.FaultEvent; 
       import mx.rpc.events.ResultEvent; 

// create a variable taht can be used as the source for a binding operation 
[Bindable] 
public var mySource : ArrayCollection; 

       private function myMethodResult(e:ResultEvent):void 
       { 
//     searchType.dataProvider = e.result as ArrayCollection; 
// change the value of your binding source 
mySource = new ArrayCollection(e.result); 
       } 

       protected function initApp():void 
       { 
        service.myMethod(); 
       } 

       protected function faultHandler(event:FaultEvent):void 
       { 
        trace(event.fault.faultString); 
       } 
      ]]> 
     </fx:Script> 
     <fx:Declarations> 
      <s:RemoteObject id="service" 
          destination="zend" 
          source="Test" 
          showBusyCursor="true" 
          fault="faultHandler(event)"> 
       <s:method name="myMethod" result="myMethodResult(event)"/> 
      </s:RemoteObject> 
     </fx:Declarations> 
<!-- and finally, specify your dataProvider as the target for binding --> 
     <s:DropDownList id="searchType" labelField="NAME" dataProvider="{this.mySource }"/> 
    </s:WindowedApplication> 

我寫在瀏覽器的所有代碼,它可能不是「完美編譯」

0

@Flextras

searchType.dataProvider = new ArrayCollecection(e.result); 

。 ..resulted在...

1118: Implicit coercion of a value with static type Object to a possibly unrelated type Array. 

相反,我試過......

searchType = ArrayCollection(e.result); 

,但是這導致...

Error #1034: Type Coercion failed: cannot convert []@812a1c9 to mx.collections.ArrayCollection 

然後我試圖...

typeArray.source = e.result as Array; 

......還有......

<s:DropDownList labelField="NAME"> 
    <s:ArrayCollection id="typeArray"/> 
</s:DropDownList> 

這工作! \ o/

+0

正如我在回答中所說:「可能你需要將e.result(一個通用對象)作爲一個數組,但我不這麼認爲。」我敢打賭,這將工作:searchType.dataProvider = new ArrayCollecection(e.result as Array);.如果您認爲我的回答對您有幫助,請務必將其標記爲正確,並/或給予優惠。 – JeffryHouser 2010-09-08 17:15:24

+0

我已經將它標記爲正確並且投了票。謝謝你的幫助。 – Reado 2010-09-09 14:34:30

+0

我很欣賞Karma的積分;謝謝! – JeffryHouser 2010-09-09 15:04:34

相關問題