如何将Flex Combobox中的预设值作为选定项目处理

时间:2015-10-24 15:23:48

标签: flex combobox

我的Flex程序遇到了一个非常基本但令人厌烦的问题。

总而言之,我的DataGrid在每一行(你已经可以添加和删除行的行)中有一个基于ArrayCollection元素的Combobox( responsabili ),对Combobox的每次更改都已存储对db的更改。 我希望Combobox显示从db加载的值作为选定值。

<s:Panel title="qqq" width="100%" height="100%" styleName="light">
    <s:layout>
        <s:VerticalLayout paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10" />
    </s:layout>
    <s:VGroup width="100%" height="100%">
        <mx:Text text="www" />
        <components:GraphicButton styleName="add" toolTip="Aggiungi elemento" id="aggCompito" click="aggCompito_clickHandler(event)"/>
        <s:DataGrid width="100%" height="100%" dataProvider="{MyModel.instance.compiti}" editable="true" gridItemEditorSessionSave="changeHandler(event)">
            <s:columns>
                <s:ArrayList>
                    <s:GridColumn headerText="Responsabile" editable="false" width="180">
                        <s:itemRenderer>
                            <fx:Component>
                                <s:GridItemRenderer>
                                    <fx:Script>
                                        <![CDATA[
                                        import it.aaa.frontend.model.MyModel;

                                        import mx.binding.utils.ChangeWatcher;
                                        import mx.collections.ArrayCollection;
                                        import mx.controls.Alert;
                                        import mx.core.FlexGlobals;
                                        import mx.events.CloseEvent;
                                        import mx.events.IndexChangedEvent;

                                        import spark.events.IndexChangeEvent;

                                        protected function changeHandler(event:IndexChangeEvent):void {
                                            data.responsabile = myResponsabile.selectedItem;
                                            // TODO Auto-generated method stub
                                            data.tipo = event.newIndex;
                                            dispatchEvent(new Event("compitiChange", true));
                                        }

                                        protected function responsabililabelFunc(item:Object):String {
                                            return String(item.cognome) + " " + String(item.nome);
                                        }

                                        protected function guessSelectedItem(item:Object):int {
                                            return item.responsabile.id;
                                        }

                                        ]]>
                                    </fx:Script>
                                    <s:HGroup>
                                        <s:ComboBox id="myResponsabile"
                                                    dataProvider="{MyModel.instance.responsabili}"
                                                    change="changeHandler(event)"
                                                    labelFunction="responsabililabelFunc"
                                                    selectedItem="{data.responsabile}"
                                                />
                                    </s:HGroup>
                                </s:GridItemRenderer>
                            </fx:Component>
                        </s:itemRenderer>
                    </s:GridColumn>
                </s:ArrayList>
            </s:columns>
        </s:DataGrid>
    </s:VGroup>
</s:Panel>

MyModel.as

public var responsabili:ArrayCollection; //initialized with actual ArrayCollection of "responsabili", with id, cognome, nome.

1 个答案:

答案 0 :(得分:0)

它只适用于selectedItem =&#34; {data.responsabile}&#34;如果您使用某些actionscript类作为您的数据对象,并且属性resposabile被标记为可绑定:

[Bindable] public var responsabile:Object;

如果您只有一些通用对象作为数据,则需要在itemrenderer中设置数据的位置进行捕获。所以你的GridItemRenderer需要另一种方法:

override public function set data(value:Object):void
{
    super.data = value;

    // only do something if the data is set. When the renderer is destroyed the data will be null
    if (data)
    {
       myResponsabile.selectedItem = data.responsabile;
    }
}

我不记得是否在创建了所有组件(包括组合框)之后或之前设置了数据。因此,如果因为myResponsabile在此时为空,您将获得空指针异常,它将变得更复杂:

private var _myResponsabile:Object;
private var _myResponsabileChanged:Boolean;

override public function set data(value:Object):void
{
    super.data = value;

    // only do something if the data is set. When the renderer is destroyed the data will be null
    if (data)
    {
       _myResponsabile = data.responsabile;
       _myResponsabileChanged = true;
       invalidateProperties();
    }
}

override protected function commitProperties():void
{
    super.commitProperties();

    if(_myResponsabileChanged)
    {
        _myResponsabileChanged = false;
        myResponsabile.selectedItem = _myResponsabile;
    }
}