2012-01-02 94 views
1

我有一個從XML文件中獲取值的datagrid(從flex數據庫使用PHP和HTTP請求獲取此xml文件)。我在數據網格的每一行都創建了一個複選框。這裏是我的要求: 我想選擇兩個或三個複選框,並希望以某種形式獲取特定行的所有值,首選arraycollection(這樣我可以直接將此數組傳遞給條形圖)。有人能幫助我,因爲我是flex的新手。如何從DataGrid flex中獲取選定的值(使用checkBox)

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="siteData.send()"> 
<mx:Script> 
    <![CDATA[ 
     import mx.collections.XMLListCollection; 
     import mx.controls.*; 
     import mx.events.ListEvent; 
     import mx.rpc.events.ResultEvent; 
     import mx.controls.Alert; 
     [Bindable] private var fullXML:XMLList; 
     private function contentHandler(evt:ResultEvent):void{ 
      fullXML = evt.result.values; 
     } 

    ]]> 
</mx:Script> 
<mx:VBox> 
    <mx:Label text="This Data Grid is loading the full XML file"/> 
    <mx:DataGrid width="600" id="datagrid" dataProvider="{fullXML}"> 

     <mx:columns> 
      <mx:DataGridColumn headerText="Select"> 
       <mx:itemRenderer> 
        <mx:Component> 
         <mx:HBox horizontalAlign="center"> 

          <mx:CheckBox id="check"/> 
         </mx:HBox> 
        </mx:Component> 
       </mx:itemRenderer> 
      </mx:DataGridColumn> 
      <mx:DataGridColumn dataField="release_version" headerText="Release"/> 
      <mx:DataGridColumn dataField="build" headerText="build"/> 
      <mx:DataGridColumn dataField="time_login" headerText="time_login"/> 
      <mx:DataGridColumn dataField="time_tunnel" headerText="time_tunnel"/> 
      <mx:DataGridColumn dataField="rate_login" headerText="time_tunnel"/> 
      <mx:DataGridColumn dataField="rate_tunnel" headerText="rate_tunnel"/> 
     </mx:columns> 
    </mx:DataGrid> 
</mx:VBox> 
<mx:HTTPService url="http://localhost/php_genxml.php" id="siteData" result="contentHandler(event)" resultFormat="e4x"/> 
</mx:Applicaton> 

我想選擇一些複選框,並希望得到在數據網格中的所有字段的值對應於複選框,可以有一個人幫助我如何得到選擇的值(所選擇的值複選框)在flex和動作腳本中。

回答

1
<mx:itemRenderer> 
        <mx:Component> 
         <mx:HBox horizontalAlign="center" verticalAlign="middle"> 
          <mx:Script> 
           <![CDATA[ 
            var objTemp:Object = new Object(); 

            override public function set data(value:Object):void 
            { 
             if(value != null) 
             { 
              var xml:XML = XML(value); 
              super.data = value; 
              objTemp = outerDocument.xmlToObject(xml.toString()); 
              if(objTemp.story['quiz_score'] != null) 
              { 
               chkAssignment.visible = false; 
              } 
              else 
              { 
               chkAssignment.visible = true; 
              } 
              if(objTemp.story.is_selected == false) 
              { 
               chkAssignment.selected = false; 
              } 
              else 
              { 
               chkAssignment.selected = true; 
              } 

             } 
            } 

            private function deleteAssignment():void 
            { 


             if(chkAssignment.selected) 
             { 
              outerDocument.isChanged = true; 

              objTemp.story.is_selected = true; 
              var xml:XML = outerDocument.objectToXML(objTemp,"record"); 

              var xmlList:XMLList = xml.children(); 
              xml = xmlList[0] as XML; 

              outerDocument.dgListeningLog.dataProvider[outerDocument.dgListeningLog.selectedIndex] = xml; 

              outerDocument.arrAssignment.push({"story_name": XML(outerDocument.dgListeningLog.selectedItem).story_title.toString() ,"student_assignmentId": XML(outerDocument.dgListeningLog.selectedItem).assignment_id.toString(),"session_key": XML(outerDocument.dgListeningLog.selectedItem).session_key.toString(),"selectedIndex": outerDocument.dgListeningLog.selectedIndex.toString()}); 
             } 
             else 
             { 
              outerDocument.isChanged = true; 

              objTemp.story.is_selected = false; 
              var xml:XML = outerDocument.objectToXML(objTemp,"record"); 

              var xmlList:XMLList = xml.children(); 
              xml = xmlList[0] as XML; 

              outerDocument.dgListeningLog.dataProvider[outerDocument.dgListeningLog.selectedIndex] = xml; 

              for(var i:int =0; i < outerDocument.arrAssignment.length; i++) 
              { 
               if(outerDocument.arrAssignment[i].selectedIndex == outerDocument.dgListeningLog.selectedIndex) 
               { 
                outerDocument.arrAssignment.splice(i,1); 
                break; 
               } 
              } 
             } 

            } 

           ]]> 
          </mx:Script> 
          <mx:CheckBox id="chkAssignment" change="{deleteAssignment();}"/> 
         </mx:HBox> 
        </mx:Component> 
       </mx:itemRenderer> 

我在這裏存儲在另一個陣列的選定值或陣列和刪除按鈕點擊時,它會檢查並刪除從主數組,它是數據網格的數據提供者的值。

如果你正面臨滾動DataGrid中的CheckBox問題時顯示錯誤的值比代碼複製下面的方法:

覆蓋公共職能組數據(值:對象):無效

主要有兩個功能使用...

public function objectToXML(obj:Object, qname:String):XML 
     { 
      var qName:QName = new QName(qname); 
      var xmlDocument:XMLDocument = new XMLDocument(); 
      var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument); 
      var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(obj, qName, xmlDocument); 
      var xml:XML = new XML(xmlDocument.toString()); 
      return xml; 
     } 

     public function xmlToObject(value:String):Object 
     { 
      var xmlStr:String = value.toString(); 
      var xmlDoc:XMLDocument = new XMLDocument(xmlStr); 
      var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true); 
      var resultObj:Object = decoder.decodeXML(xmlDoc); 
      return resultObj; 
     } 
+1

嗨薩格爾拉瓦爾, 感謝簡單和很好的解決方案.. – 2012-01-03 04:36:01

+1

感謝薩加爾對於上述工作。 – 2012-01-03 06:17:46

+0

感謝薩加爾,但請你讓我知道如何做到這一點我的腳本,我附加。我試着用你的腳本看起來像有一些函數像stringtoxml的缺陷等等。這些都不在代碼中。 – tanuj 2012-01-03 10:21:33

相關問題