2012-01-24 36 views
1

我有一個mx:Datagrid女巫我想添加一個組合框作爲項目渲染器。Flex mx:DataGrid - 創建組合框ItemRenderer

<mx:DataGrid id="dgEnsActes" 
          horizontalScrollPolicy="on" 
          dataProvider="{arListeDevis}" 
          width="100%" height="100%" > 
        <mx:columns> 
         <mx:DataGridColumn dataField="dvIndex" headerText="" headerStyleName="dgHeader" fontWeight="normal" width="40"/> 
         <mx:DataGridColumn dataField="dvLibelle" headerText="Libellé" headerStyleName="dgHeader" wordWrap="true" fontWeight="normal" width="180"/> 
         <mx:DataGridColumn dataField="dvTotal" headerText="Total" headerStyleName="dgHeader" width="60" fontWeight="normal"/> 
         <mx:DataGridColumn dataField="dvStatut" headerText="Statut" 
              rendererIsEditor="true" editorDataField="result" itemRenderer="fr.inters.ui.itemRenderer.irComboEtatDevis" 
              wordWrap="true" headerStyleName="dgHeader" fontWeight="normal" width="70"/> 
         <mx:DataGridColumn dataField="dvAcceptDirect" headerText="Création" headerStyleName="dgHeader" width="60" fontWeight="normal"/> 

        </mx:columns> 

       </mx:DataGrid> 

我的自定義項目渲染是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<s:MXDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
          xmlns:s="library://ns.adobe.com/flex/spark" 
          xmlns:mx="library://ns.adobe.com/flex/mx" 
          focusEnabled="true"> 


    <fx:Script> 
     <![CDATA[ 
      public var result:String=""; 
      [Bindable] var dpValue:Array=[ 
       {label:"Accepté", data:"Accepté"}, 
       {label:"Refusé", data: "Refusé"}, 
       {label:"En attente", data: "En attente"}]; 


      override public function set data(value:Object):void 
      { 
       super.data = value; 
       if (value != null) 
       { 
        var currentValue:String = value.size; 
        var len:int = dpValue.length; 
        for (var i:int = 0; i < len; i++) 
        { 
         if (dpValue[i].data == currentValue) 
         { 
          editor.selectedIndex = i; 
          break; 
         } 
        } 
       } 
      } 

      public function onChange():void 
      { 
       var index:int = editor.selectedIndex; 
       result = dpValue[index].data; 
      } 


     ]]> 
    </fx:Script> 
    <mx:ComboBox id="editor" dataProvider="{dpValue}" width="130" change="onChange()"/> 

</s:MXDataGridItemRenderer> 

但是,當我嘗試調試一個錯誤出現,該消息是selectedIndex is undefined

是有人幫我嗎?

謝謝

回答

2

爲什麼不使用沒有柵格項呈示器的組合框?

看看這個樣品在http://blog.flexmp.com/2008/02/18/simple-datagrid-combobox-as-item-editor-example/

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> 
     <mx:XML id="xml" source="weather.xml"/> 
     <mx:DataGrid id="myDatagrid" dataProvider="{xml.city}" 
       variableRowHeight="true" editable="true" rowHeight="50" 
       width="300" height="300"> 
       <mx:columns> 
       <mx:DataGridColumn dataField="Location"/> 
       <mx:DataGridColumn dataField="Climate" editable="true" editorDataField="value"> 
         <mx:itemEditor> 
           <mx:Component> 
             <mx:ComboBox editable="true"> 
               <mx:dataProvider> 
                 <mx:String>Mild</mx:String> 
                 <mx:String>Hot</mx:String> 
                 <mx:String>Foggy</mx:String> 
                 <mx:String>Rainy</mx:String> 
                 <mx:String>Snow</mx:String> 
               </mx:dataProvider> 
             </mx:ComboBox> 
           </mx:Component> 
         </mx:itemEditor> 
       </mx:DataGridColumn> 
       <mx:DataGridColumn dataField="CloudCoverPercent" editable="true" editorDataField="value" 
         itemEditor="CloudCover"/> 
     </mx:columns> 
     </mx:DataGrid> 
</mx:Application> 
+0

謝謝這個項目編輯器運作良好,但你知道如何做與itemrenderer相同。事實上,在我的情況下,如果我將itemEditor修改爲itemrenderer,會出現一個錯誤/ – Flex60460

+0

您是否可以更改editible =「false」並僅將其用於顯示?請參閱http://www.adobe.com/devnet/flex/videotraining/exercises/ex4_05.html獲取更多建議。稍後我會回到你身邊,當我回到家時,看看我使用itemrenderers寫的代碼。 – ShaunOReilly

+0

如果y把可編輯爲true,那就沒問題。但在數據網格上,我們顯示標籤而不是組合框。謝謝 – Flex60460