如何获取DataGrid中的ComboBox的值 [英] How to get the value of a ComboBox within a DataGrid

查看:190
本文介绍了如何获取DataGrid中的ComboBox的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然这可能是一个简单的问题,我有一个时间提出一个解决方案。



我有一个DataGrid,其中一个ComboBox作为ItemRenderer。当用户选择一行时,我想为所选行获取ComboBox的选定值。



EDIT:我应该提到myData中的dataField2_Array属性实际上是一个Array是ComboBox的dataProvider。 myData中的每个对象在该数组中可能具有完全不同的值,因此DataGrid的每一行中的ComboBox可能有完全不同的选项。



任何建议? p>

某些示例代码:

 < mx:DataGrid id =myGrid 
dataProvider ={myData}>
< mx:columns>
< mx:DataGridColumn headerText =Column 1dataField =dataField1/>
< mx:DataGridColumn headerText =Column 2dataField =dataField2_Array>
< mx:itemRenderer>
< mx:Component>
< mx:HBox paddingLeft =5>
< mx:ComboBox id =myComboBoxdataProvider ={data.dataField2_Array}/>
< / mx:HBox>
< / mx:Component>
< / mx:itemRenderer>
< / mx:DataGridColumn>
< / mx:columns>
< / mx:DataGrid>


解决方案

  mx:DataGrid =MyDataGrid> 
< mx:columns>
< mx:DataGridColumn headerText =Resourcewidth =200itemRenderer =com.myClasses.myGridDropdownRenderer/>
< / mx:columns>
< / mx:DataGrid>

这是你的datagrid的itemRenderer。

 <?xml version =1.0encoding =utf-8?& 
< mx:ComboBox prompt =请选择评级change =stuffChanged()dataProvider ={data.dataField2_Array}
xmlns:mx =http://www.adobe。 com / 2006 / mxml>
< mx:Script>
<![CDATA [
import flash.events.Event;
import mx.controls.Alert;
import mx.core.Application;
import mx.collections.ArrayCollection;



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

}



public function stuffChanged():void {
var myListData:DataGridListData = DataGridListData(listData);
var r:int = myListData.rowIndex;
var c:int = myListData.columnIndex;
//Application.application.whateverStuff[r+1][c]=this.value;
Application.application.whateverStuff [r + 1] [c] =
this.selectedItem.data;
}



]]>
< / mx:Script>

< / mx:ComboBox>

这将在您的主应用程序中保存此值。

  [Bindable] public var whateverStuff:ArrayCollection; 

现在当您的数据更改时,它保存数据。您单击按钮将此值存储在行对象中。

  [Bindable] public var rows:Object = new Object(); 
rows = Application.application.whateverStuff;

当您将值发送回数据库时,一起发送此行对象。



更新:



在我阅读了您对上一个riposte的评论后,每个组合框有不同的选项。



当您点击所选行时,您应该能够收集行的ID,这将确保只有ID即使您更新了其他行的组合框,该行也在数据库中更新。



选择一行后,单击并验证您使用Alert或trace选择的行ID,然后通过事件分派器单独发送行值。


While this may be a simple problem, I'm having a heck of a time coming up with a solution.

I have a DataGrid with a ComboBox as an ItemRenderer for one of my columns. When the user selects a row, I want to get the ComboBox's selected value for the selected row.

EDIT: I should have mentioned that the dataField2_Array property in myData is actually an Array is the dataProvider for the ComboBox. Each object in myData could have completely different values in that Array so the ComboBox in each row of the DataGrid could have completely different options to pick from.

Any suggestions?

Some sample code:

<mx:DataGrid id="myGrid"
  dataProvider="{myData}">
    <mx:columns>
      <mx:DataGridColumn headerText="Column 1" dataField="dataField1" />
      <mx:DataGridColumn headerText="Column 2" dataField="dataField2_Array">
        <mx:itemRenderer>
          <mx:Component>
            <mx:HBox paddingLeft="5">
              <mx:ComboBox id="myComboBox" dataProvider="{data.dataField2_Array}" />
            </mx:HBox>
          </mx:Component>
        </mx:itemRenderer>
      </mx:DataGridColumn>
    </mx:columns>
</mx:DataGrid>

解决方案

<mx:DataGrid ="MyDataGrid">
<mx:columns>
<mx:DataGridColumn headerText="Resource" width="200" itemRenderer="com.myClasses.myGridDropdownRenderer"/>
</mx:columns>
</mx:DataGrid>

Here is your itemRenderer for your datagrid.

<?xml version="1.0" encoding="utf-8"?>
<mx:ComboBox prompt="Please select a Rating" change="stuffChanged()" dataProvider="{data.dataField2_Array}"
     xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
        <![CDATA[
            import flash.events.Event;
            import mx.controls.Alert;
            import mx.core.Application;
            import mx.collections.ArrayCollection;



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

            }



           public function stuffChanged():void{
               var myListData:DataGridListData = DataGridListData(listData);
               var r:int=myListData.rowIndex;
               var c:int=myListData.columnIndex;
              //Application.application.whateverStuff[r+1][c]=this.value;
               Application.application.whateverStuff[r+1][c]=
              this.selectedItem.data;
              }



        ]]>
    </mx:Script>

</mx:ComboBox>

This will be in your main Application which will be holding this value.

[Bindable] public var whateverStuff:ArrayCollection;

Now when your data is changed, it holds the data. You click on the button store this value in a rows object.

[Bindable] public var rows:Object = new Object();
rows=Application.application.whateverStuff; 

When your sending the value back to database, send all along with this rows object.

Update:

After i read your comment on the previous riposte, i came to know that each of your combo box has a different options. You should have mentioned it earlier.

When you click on the selected rows, you should able to collect the ID of the row, and this would ensure that only the ID of that row is getting updated in the database regardless even if you update your combo box of other rows.

Once your select a row, click and verify on which row ID you have selected using Alert or trace, then send that rows value alone through an event dispatcher.

这篇关于如何获取DataGrid中的ComboBox的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆