2012-12-17 75 views
1

問題: 我有一個帶有3個索引的手風琴,每個手風琴都包含一個組件。該組件總是相同的(一個普通的數據網格)。這個數據網格通過php和JSON接收他的數據。Flex Datagrid沒有更新

現在,當我的accordeon的SelectedIndex發生更改時,我的數據網格中的數據應該會更改。

這是PHP查詢:

$query = "SELECT * FROM gerecht where typeID = " . $typeId; 

其中$typeId是將selectedIndex

if(isset($_POST['accIndex'])){ 
    $typeId = mysql_real_escape_string($_POST['accIndex']); 
} else { 
    $typeId = 1; 
} 

現在,每當我改變手風琴指數,該數據保持不變。這裏是我的Flex代碼:

<?xml version="1.0" encoding="utf-8"?> 
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
       xmlns:s="library://ns.adobe.com/flex/spark" 
       xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:components="components.*" initialize="getData.send();"> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
     <mx:HTTPService id="getData" url="http://localhost/P006_Project/Query.php" 
         useProxy="false" method="POST" resultFormat="text" result="getPHPData(event)" /> 
     <mx:HTTPService id="sendData" url="http://localhost/P006_Project/Query.php" 
         useProxy="false" method="POST" result="sendData_resultHandler(event)"> 
      <mx:request xmlns=""> 
       <accIndex> 
        {accItems.selectedIndex + 1} 
       </accIndex> 
      </mx:request> 
     </mx:HTTPService> 

     <s:ArrayCollection id="acItems" source="{dataArray.source}" /> 
    </fx:Declarations> 
    <fx:Script> 
     <![CDATA[ 
      import mx.collections.ArrayCollection; 
      import mx.controls.Alert; 
      import mx.controls.Text; 
      import mx.events.FlexEvent; 
      import mx.events.IndexChangedEvent; 
      import mx.rpc.events.ResultEvent; 

      [Bindable]private var dataArray:ArrayCollection = new ArrayCollection(); 

      private function initDataGrid():void 
      { 
       getData.send(); 
      } 

      private function getPHPData(event:ResultEvent):void 
      { 
       var rawArray:Array; 
       var rawData:String = String(event.result); 
       rawArray = JSON.parse(rawData) as Array; 
       dataArray = new ArrayCollection(rawArray); 
      } 

      protected function accItems_changeHandler(event:IndexChangedEvent):void 
      { 
       // TODO Auto-generated method stub 
       sendData.send(); 
       trace(acItems); 
      } 

      protected function sendData_resultHandler(event:ResultEvent):void 
      { 
       // TODO Auto-generated method stub 
       Alert.show(event.result.toString()); 
      } 

     ]]> 
    </fx:Script> 

    <mx:Accordion id="accItems" creationPolicy="auto" change="accItems_changeHandler(event)"> 
     <s:NavigatorContent label="Frisdranken"> 
      <components:FULLTESTCOMP acItems="{acItems}" creationComplete="{initDataGrid()}"/> 
     </s:NavigatorContent> 
     <s:NavigatorContent label="Bieren (vat)"> 
      <components:FULLTESTCOMP acItems="{acItems}" creationComplete="{initDataGrid()}"/> 
     </s:NavigatorContent> 
    </mx:Accordion> 
</s:Application> 

我猜事情錯了無論是在: - PHP接受的selectedIndex? - 或者flex無法使用新數據更新數據網格?

+0

您是否看到期望值達到'getPHPData'fn? IE'dataArray'包含它應該的內容? – ethrbunny

+0

我'dataArray'的內容似乎一直保持不變,所以我猜selectedIndex沒有到達PHP文件? – ZackWhite

+0

聽起來像。因此,下一步是確定價值是否正在發送。我沒有看到任何params通過。不知道這是否重要。 – ethrbunny

回答

1

K我終於找到了解決方案。我正在使用2個httpServices,我從第一個接收數據,但這個沒有包含參數。所以我把它們合併成一個。導致下面的代碼:(我也添加了我所有的其他組件,所以不注意它們)

<?xml version="1.0" encoding="utf-8"?> 
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
       xmlns:s="library://ns.adobe.com/flex/spark" 
       xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:components="components.*"> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
     <mx:HTTPService id="sendData" url="http://localhost/P006_Project/Query.php" 
         useProxy="false" method="POST" result="sendData_resultHandler(event)"> 
      <mx:request xmlns=""> 
       <accIndex> 
        {accItems.selectedIndex + 1} 
       </accIndex> 
      </mx:request> 
     </mx:HTTPService> 

     <s:ArrayCollection id="acItems" source="{dataArray.source}" /> 
    </fx:Declarations> 
    <fx:Script> 
     <![CDATA[ 
      import mx.collections.ArrayCollection; 
      import mx.controls.Alert; 
      import mx.controls.Text; 
      import mx.events.FlexEvent; 
      import mx.events.IndexChangedEvent; 
      import mx.rpc.events.ResultEvent; 

      [Bindable]private var dataArray:ArrayCollection = new ArrayCollection(); 

      private function initDataGrid():void 
      { 
       sendData.send(); 
      } 

      protected function accItems_changeHandler(event:IndexChangedEvent):void 
      { 
       sendData.send(); 
      } 

      protected function sendData_resultHandler(event:ResultEvent):void 
      { 
       var rawArray:Array; 
       var rawData:String = String(event.result); 
       rawArray = JSON.parse(rawData) as Array; 
       dataArray = new ArrayCollection(rawArray); 
      } 

     ]]> 
    </fx:Script> 

    <mx:Accordion id="accItems" creationPolicy="auto" change="accItems_changeHandler(event)"> 
     <s:NavigatorContent label="Frisdranken"> 
      <components:FULLTESTCOMP acItems="{acItems}" creationComplete="{initDataGrid()}"/> 
     </s:NavigatorContent> 
     <s:NavigatorContent label="Bieren (vat)"> 
      <components:FULLTESTCOMP acItems="{acItems}" creationComplete="{initDataGrid()}"/> 
     </s:NavigatorContent> 
     <s:NavigatorContent label="Bieren"> 
      <components:FULLTESTCOMP acItems="{acItems}" creationComplete="{initDataGrid()}"/> 
     </s:NavigatorContent> 
     <s:NavigatorContent label="Warme dranken"> 
      <components:FULLTESTCOMP acItems="{acItems}" creationComplete="{initDataGrid()}"/> 
     </s:NavigatorContent> 
     <s:NavigatorContent label="Wijnen"> 
      <components:FULLTESTCOMP acItems="{acItems}" creationComplete="{initDataGrid()}"/> 
     </s:NavigatorContent> 
     <s:NavigatorContent label="Sterke dranken"> 
      <components:FULLTESTCOMP acItems="{acItems}" creationComplete="{initDataGrid()}"/> 
     </s:NavigatorContent> 
     <s:NavigatorContent label="Tapas"> 
      <components:FULLTESTCOMP acItems="{acItems}" creationComplete="{initDataGrid()}"/> 
     </s:NavigatorContent> 
     <s:NavigatorContent label="Platos"> 
      <components:FULLTESTCOMP acItems="{acItems}" creationComplete="{initDataGrid()}"/> 
     </s:NavigatorContent> 
     <s:NavigatorContent label="Especialidades"> 
      <components:FULLTESTCOMP acItems="{acItems}" creationComplete="{initDataGrid()}"/> 
     </s:NavigatorContent> 
     <s:NavigatorContent label="Bocadillos"> 
      <components:FULLTESTCOMP acItems="{acItems}" creationComplete="{initDataGrid()}"/> 
     </s:NavigatorContent> 
    </mx:Accordion> 
</s:Application>