2017-04-05 80 views
0

我有DropDownList呈現在DataGrid上。我的Datagrid由兩列組成,第一列包含項目名稱,第二列包含DropDownList(帶有分配給該項目的標籤)。如何爲DropDownList設置選定的項目在Adobe Flex中的DataGrid中?

我可以加載從數據庫中獲取標籤值並將它們加載到DropDownList。

因此每一行都包含項目名稱和DropDownList,並將標籤數據加載到該項目中。

現在我想要的是,我希望每個下拉菜單都顯示與該特定項目關聯的標籤。

我的Flex代碼:

<mx:DataGrid id="IdDgItemLabelDisp" left="10" right="10" top="39" bottom="10" dataProvider="{arrAllItem}"> 
<mx:columns> 
    <mx:DataGridColumn dataField="itemName" headerText="Item Name"/> 
    <mx:DataGridColumn headerStyleName="dataGridHeadingStyle" headerText="Label"> 
     <mx:itemRenderer> 
      <fx:Component> 
       <mx:HBox horizontalAlign="center"> 
        <fx:Script> 
         <![CDATA[ 
         ]]> 
        </fx:Script> 
        <s:DropDownList id="IdCmbItemLabel" dataProvider="{outerDocument.arrLabelCombo}" selectedItem="{outerDocument.arrLabelCombo.getItemAt(0)}">                
        </s:DropDownList> 
       </mx:HBox> 
      </fx:Component> 
     </mx:itemRenderer> 
    </mx:DataGridColumn> 
</mx:columns> 

+0

所以你想要按照在arrAllItem中定義的行數據在下拉列表中選擇項目,對不對? – gbdcool

回答

1

如果你想在dropdown按照它在arrAllItem定義的行數據的selectedItem,那麼這裏是解決方案:

<?xml version="1.0"?> 
<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"> 
<fx:Script><![CDATA[ 
    import mx.collections.ArrayCollection; 
    [Bindable] 
    private var arrAllItem:ArrayCollection = new ArrayCollection([ 
     {itemName: "Laptop", quantity:1}, 
     {itemName: "Windows", quantity:2}, 
     {itemName: "Mac", quantity:3}, 
     {itemName: "Tablet", quantity:4} 
    ]); 
    [Bindable] 
    public var arrLabelCombo:ArrayCollection = new ArrayCollection([ 
     {label: "One", data: 1}, 
     {label: "Two", data: 2}, 
     {label: "Three", data: 3}, 
     {label: "Four", data: 4} 
    ]); 
    ]]></fx:Script> 
<mx:DataGrid id="IdDgItemLabelDisp" left="10" right="10" top="39" bottom="10" dataProvider="{arrAllItem}"> 
    <mx:columns> 
     <mx:DataGridColumn dataField="itemName" headerText="Item Name"/> 
     <mx:DataGridColumn headerStyleName="dataGridHeadingStyle" headerText="Label"> 
      <mx:itemRenderer> 
       <fx:Component> 
        <mx:HBox horizontalAlign="center"> 
         <fx:Script> 
         <![CDATA[ 
          override public function set data(value:Object):void 
          { 
           if(data != value) 
           { 
            super.data = value; 
           } 
          } 
          private function getSelectedItem(data:Object):Object 
          { 
           if (data) 
           { 
            for each(var item:Object in IdCmbItemLabel.dataProvider) 
            { 
             if(data.quantity == item.data) 
              return item; 
            } 
           } 
           return null; 
          } 
          ]]> 
         </fx:Script> 
         <s:DropDownList id="IdCmbItemLabel" dataProvider="{outerDocument.arrLabelCombo}" selectedItem="{getSelectedItem(data)}"> 
         </s:DropDownList> 
        </mx:HBox> 
       </fx:Component> 
      </mx:itemRenderer> 
     </mx:DataGridColumn> 
    </mx:columns> 
</mx:DataGrid> 
</s:Application> 

enter image description here

相關問題