在Flex 4中使用ComboBox作为ItemEditor

时间:2012-04-27 11:42:58

标签: flex datagrid itemeditor

我有一个带数据的简单DataGrid。其中一列,我想使用ComboBox编辑字段,而不是标准编辑框。

我该怎么做?我已经尝试过在互联网上找到的所有类型的东西,但它们都只是简单地更新了价值。我要说这不应该太难。

2 个答案:

答案 0 :(得分:0)

我实际上正在自己做这个,并且使用spark:DataGrid它实际上比光环更容易 - 但它们都遵循相同的设置/架构。

开始于:

spark.components.gridClasses.ComboBoxGridItemEditor;

根据您的数据设置的性质和/或此类编辑对您的应用程序的多样性,您可以将其内联编写,因为大多数文档将在< fx:component>中建议,或者只是将其子类化(虽然在幕后这些是相同的 - 后者更容易重用。我的场景中组合的数据是一个更大的父对象的子选择,所以我选择让自己更容易,并添加一个额外的属性 dataField 来模仿其他渲染器/编辑器 - 实际上是什么仅显示单元格本身(不处于编辑模式时)。

基本设置看起来或多或少像这样(至少我的):

public class AccountComboEditor extends ComboBoxGridItemEditor
{
     private _dataField:String;

     public function AccountComboEditor()
     {
         super();
         //note - typically you wouldn't do "logic" in the view but it's simplified as an example
         addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
     }

     public function get dataField():String { return _dataField; }

     public function set dataField(value:String):void
     {
         if (_dataField !=value) //dosomeadditionalvalidation();
         _dataField = value;
     }


     override public function prepare():void
     {
        super.prepare();

        if (data && dataField && comboBox) comboBox.labelField = data[dataField];
     }

     protected function onCreationComplete(event:FlexEvent):void
     {
         //now setup the dataProvider to your combo box - 
         //as a simple example mine comse out of a model

         dataProvider = model.getCollection();
         //this isn't done yet though - now you need a listener on the combo to know
         //what item was selected, and then get that data_item (label) back onto this 
         //editor so it has something to show when the combo itself isn't in
         //editor mode
     }
}

真正的好处是在子类内部设置组合框的 labelField ,如果需要将其作为附加属性公开,则在外部设置。

下一部分是将其用作实际数据网格的 mx.core.ClassFactory 的一部分。一个简单的视图看起来像是类似的东西:

<s:DataGrid>
   <fx:Script>
        private function getMyEditor(dataField:String):ClassFactory
        {
           var cf:ClassFactory = new ClassFactory(AccountComboEditor);
               cf.properties = {dataField : dataField };
           return cf;
        }
   </fx:Script>

   <s:columns>
      <mx:ArrayList>
         <s:GridColumn itemEditor="{getMyEditor('some_data_property')}" />
      </mx:ArrayList>
   </s:columns>
</s:DataGrid>

Creating item renderers...文档会为您提供更多信息。

答案 1 :(得分:0)

我明白了。我只想要一个简单的下拉框,而不是文本编辑字段。

以下代码确实需要:

        <mx:DataGridColumn dataField="type" headerText="Type" editorDataField="value">
            <mx:itemEditor>
                <fx:Component>
                    <mx:ComboBox>
                        <mx:dataProvider>
                            <fx:String>Gauge</fx:String>
                            <fx:String>Graph</fx:String>
                            <fx:String>Indicator</fx:String>
                        </mx:dataProvider>
                    </mx:ComboBox>
                </fx:Component>
            </mx:itemEditor>
        </mx:DataGridColumn>
相关问题