apache-flex 如何在Adobe Flex中为DataGrid上的下拉列表设置选定项?

zpjtge22  于 2022-11-01  发布在  Apache
关注(0)|答案(1)|浏览(183)

我在DataGrid上呈现了DropDownList。我的DataGrid由两列组成,第一列由项的名称组成,第二列由DropDownList(带有要分配给该项的标签)组成。
我能够加载以从数据库中获取标签值并将其加载到DropDownList。
因此每一行都由Item name和DropDownList以及加载到其中的标签数据组成。
现在我想要的是,我希望每个下拉菜单都显示与该特定项目关联的标签。
我的弹性代码:

<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>
4szc88ey

4szc88ey1#

如果您希望dropdown中的selectedItemarrAllItem中定义的行数据一致,则解决方案如下:

<?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>

相关问题