6 Replies Latest reply on Feb 8, 2011 1:06 PM by gpbuil

    How to get the selectedIndex from a combobox made by classFactory?

    gpbuil

      Dear All,

       

      I am trying to get the selectedIndex or selecetedItem.banda_id when changing a combobox that is created by classFactory and it is inside a datagrid.

       

      The code I have is:

       

      MXML:

       

      <mx:DataGrid width="100%" height="100%" id="salas_fin_dg" editable="true">

           <mx:columns>

      <mx:DataGridColumn headerText="Titulo" dataField="titulo" editable="false" width="250"/>

      <mx:DataGridColumn headerText="Data" dataField="start_dt" editable="false"/>

      <mx:DataGridColumn headerText="Dur" dataField="duration" editable="false" width="40"/>

      <mx:DataGridColumn headerText="Banda" dataField="banda_nome" editable="true" itemEditor="{combofac}"/>

      <mx:DataGridColumn headerText="$ Ensaio" dataField="total_ensaio" editable="true" itemEditor="controls.NumericInput"/>

      <mx:DataGridColumn headerText="$ Bar" dataField="total_bar" editable="true" itemEditor="controls.NumericInput"/>

      <mx:DataGridColumn headerText="$ Loja" dataField="total_loja" editable="true" itemEditor="controls.NumericInput"/>

      <mx:DataGridColumn headerText="$ CC" dataField="total_cc" editable="true" itemEditor="controls.NumericInput"/>

      <mx:DataGridColumn headerText="$ CH" dataField="total_ch" editable="true" itemEditor="controls.NumericInput"/>

      <mx:DataGridColumn headerText="$ DI" dataField="total_di" editable="true" itemEditor="controls.NumericInput"/>

      <mx:DataGridColumn headerText="$ Pend" dataField="total_pend" editable="true" itemEditor="controls.NumericInput"/>

           </mx:columns>

      </mx:DataGrid>

       

       

      Script:

       

      private function getSalasFinHandler(event:ResultEvent):void

           {

      salas_fin_lst = new ArrayCollection(event.result as Array);

      salas_fin_dg.dataProvider = salas_fin_lst;

      myservice.getBandas();

      combofac=new ClassFactory(ComboBox);

           combofac.properties={dataProvider:bandas_lst, labelField:"banda_nome", prompt:"Selecione"};

           }

       

       

      I am using Remote Object to get the data from a DB, and it is working properly.

       

      As I will update the datagrid cell values, I would like to update them on the DB.

       

      In order to do that, I would need to access the banda_id value, that is part of the combofac dataprovider, I mean that this dataprovider has 2 columns, being banda_nome and banda_id.

       

      The question is how to get the banda_id value, when I change the comobobox?

       

      Many thanks in advance,

       

      Gines

        • 1. Re: How to get the selectedIndex from a combobox made by classFactory?
          Flex harUI Adobe Employee

          Not sure I understand.  You can use ITEM_EDIT_END event and access the

          itemEditorInstance which will be the ComboBox.

          1 person found this helpful
          • 2. Re: How to get the selectedIndex from a combobox made by classFactory?
            gpbuil Level 1

            Dear harUI,

             

            Thanks for the promt response, but I don't know where to use the event ITEM_EDIT_END.

             

            I will try to explain myself better this time..

             

            I have a datgrid that gets data from a dataprovider, let's use the above as example

             

             

            ScoreGroup_name
            AName A
            BName B

            C

            Name C
            DName D

             

            The dataprovider for the combobox is something like the above:

             

             

            IDScore
            1A
            2B
            3C
            4D

             

            So, when I click on my datagrid on column Score, the combox will appear and I will be able to select the richt score, the label will show OK, but I need the ID in order to update the DB properly.

             

            Did I make it easier or harder?

             

            Many thanks!

             

            Gines

            • 3. Re: How to get the selectedIndex from a combobox made by classFactory?
              Andrei Kouzmenkov Level 1

              without going into specific code, just some ideas, which may be enough.

               

              Combobox is in item renderer, so inaccessible directly, as renderers gets reused for other records, destroyed, created, etc.

              Therefore you should update your data model as soon as selection changes. Otherwise it may be too late - the same combobox instance may be reused for another record.

              • 4. Re: How to get the selectedIndex from a combobox made by classFactory?
                gpbuil Level 1

                Dear harUI and kolinitcom,

                 

                Thanks for the your prompt response and guidelines.

                 

                I went to research a little bit more on the itemEditEnd and found a way to access the data inside the combobox.

                 

                The code for all application goes below. If you click on the button, it will show you all the datagrid data, plus the ID of the combobox when changed.

                 

                Thanks again,

                 

                Gines

                 

                <?xml version="1.0" encoding="utf-8"?>

                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

                        layout="vertical"

                        verticalAlign="middle"

                        backgroundColor="white" initialize="init();">

                        <mx:Script>

                           <![CDATA[

                           import mx.utils.ObjectUtil;

                           import mx.controls.ComboBox;

                           import mx.controls.Alert;

                           import mx.events.DataGridEvent;

                           import mx.events.*;

                 

                      [Bindable]

                      private var combofac:ClassFactory;

                 

                  public var newVal:String;

                      

                      private function init():void

                      {

                        combofac=new ClassFactory(mx.controls.ComboBox);

                        combofac.properties={dataProvider:arrcombo, prompt:"Selecione", labelField:"label"};       

                      }

                            public function getCellInfo(event:DataGridEvent):void {

                                var myEditor:ComboBox = ComboBox(event.currentTarget.itemEditorInstance);

                                newVal = myEditor.selectedItem.data;

                         }                             

                 

                public function mostra():void

                {

                var dados:Object = new Object;

                dados.nome = dataGrid.selectedItem.label;

                dados.score = dataGrid.selectedItem.score;

                dados.score_id = newVal;

                Alert.show(ObjectUtil.toString(dados));

                }

                 

                 

                       ]]>

                    </mx:Script>

                 

                    <mx:TextArea id="cellInfo" width="300" height="150" />

                 

                            <mx:ArrayCollection id="arrcombo">

                        <mx:source>

                            <mx:Array>

                                <mx:Object label="1" data="A1"/>

                                <mx:Object label="2" data="A2"/>

                                <mx:Object label="3" data="A3"/>

                                <mx:Object label="4" data="A4"/>

                                <mx:Object label="5" data="A5"/>

                                <mx:Object label="6" data="A6"/>

                                <mx:Object label="7" data="A7"/>

                                <mx:Object label="8" data="A8"/>

                                <mx:Object label="9" data="A9"/>

                            </mx:Array>

                        </mx:source>

                    </mx:ArrayCollection>

                 

                    <mx:ArrayCollection id="arrColl">

                        <mx:source>

                            <mx:Array>

                                <mx:Object label="Student A" score="8" />

                                <mx:Object label="Student B" score="4" />

                                <mx:Object label="Student C" score="7" />

                                <mx:Object label="Student D" score="8" />

                                <mx:Object label="Student E" score="2" />

                                <mx:Object label="Student F" score="6" />

                                <mx:Object label="Student G" score="7" />

                                <mx:Object label="Student H" score="7" />

                                <mx:Object label="Student I" score="9" />

                                <mx:Object label="Student J" score="8" />

                                <mx:Object label="Student K" score="4" />

                                <mx:Object label="Student L" score="7" />

                            </mx:Array>

                        </mx:source>

                    </mx:ArrayCollection>

                 

                    <mx:DataGrid id="dataGrid"

                            dataProvider="{arrColl}"

                            editable="true"

                            rowCount="8"

                            itemEditEnd="getCellInfo(event);">

                        <mx:columns>

                            <mx:DataGridColumn dataField="label"

                                    editable="false" />

                            <mx:DataGridColumn dataField="score"

                                    editable="true"

                                    itemEditor="{combofac}"/>

                <mx:DataGridColumn headerText="Salvar" width="50">

                <mx:itemRenderer>

                <mx:Component>

                <mx:VBox width="100%" height="100%">

                <mx:Button label="See the values" click="outerDocument.mostra();"/>

                </mx:VBox>

                </mx:Component>

                </mx:itemRenderer>

                </mx:DataGridColumn>

                        </mx:columns>

                    </mx:DataGrid>

                </mx:Application>

                • 5. Re: How to get the selectedIndex from a combobox made by classFactory?
                  gpbuil Level 1

                  Dear harUI and kolinitcom,

                   

                  Thanks for the your prompt response and guidelines.

                   

                  I went to research a little bit more on the itemEditEnd and found a way to access the data inside the combobox.

                   

                  The code for all application goes below. If you click on the button, it will show you all the datagrid data, plus the ID of the combobox when changed.

                   

                  Thanks again,

                   

                  Gines

                   

                  <?xml version="1.0" encoding="utf-8"?>

                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

                          layout="vertical"

                          verticalAlign="middle"

                          backgroundColor="white" initialize="init();">

                          <mx:Script>

                             <![CDATA[

                             import mx.utils.ObjectUtil;

                             import mx.controls.ComboBox;

                             import mx.controls.Alert;

                             import mx.events.DataGridEvent;

                             import mx.events.*;

                   

                        [Bindable]

                        private var combofac:ClassFactory;

                   

                    public var newVal:String;

                        

                        private function init():void

                        {

                          combofac=new ClassFactory(mx.controls.ComboBox);

                          combofac.properties={dataProvider:arrcombo, prompt:"Selecione", labelField:"label"};      

                        }

                              public function getCellInfo(event:DataGridEvent):void {

                                  var myEditor:ComboBox = ComboBox(event.currentTarget.itemEditorInstance);

                                  newVal = myEditor.selectedItem.data;

                           }                            

                   

                  public function mostra():void

                  {

                  var dados:Object = new Object;

                  dados.nome = dataGrid.selectedItem.label;

                  dados.score = dataGrid.selectedItem.score;

                  dados.score_id = newVal;

                  Alert.show(ObjectUtil.toString(dados));

                  }

                   

                   

                         ]]>

                      </mx:Script>

                   

                      <mx:TextArea id="cellInfo" width="300" height="150" />

                   

                              <mx:ArrayCollection id="arrcombo">

                          <mx:source>

                              <mx:Array>

                                  <mx:Object label="1" data="A1"/>

                                  <mx:Object label="2" data="A2"/>

                                  <mx:Object label="3" data="A3"/>

                                  <mx:Object label="4" data="A4"/>

                                  <mx:Object label="5" data="A5"/>

                                  <mx:Object label="6" data="A6"/>

                                  <mx:Object label="7" data="A7"/>

                                  <mx:Object label="8" data="A8"/>

                                  <mx:Object label="9" data="A9"/>

                              </mx:Array>

                          </mx:source>

                      </mx:ArrayCollection>

                   

                      <mx:ArrayCollection id="arrColl">

                          <mx:source>

                              <mx:Array>

                                  <mx:Object label="Student A" score="8" />

                                  <mx:Object label="Student B" score="4" />

                                  <mx:Object label="Student C" score="7" />

                                  <mx:Object label="Student D" score="8" />

                                  <mx:Object label="Student E" score="2" />

                                  <mx:Object label="Student F" score="6" />

                                  <mx:Object label="Student G" score="7" />

                                  <mx:Object label="Student H" score="7" />

                                  <mx:Object label="Student I" score="9" />

                                  <mx:Object label="Student J" score="8" />

                                  <mx:Object label="Student K" score="4" />

                                  <mx:Object label="Student L" score="7" />

                              </mx:Array>

                          </mx:source>

                      </mx:ArrayCollection>

                   

                      <mx:DataGrid id="dataGrid"

                              dataProvider="{arrColl}"

                              editable="true"

                              rowCount="8"

                              itemEditEnd="getCellInfo(event);">

                          <mx:columns>

                              <mx:DataGridColumn dataField="label"

                                      editable="false" />

                              <mx:DataGridColumn dataField="score"

                                      editable="true"

                                      itemEditor="{combofac}"/>

                  <mx:DataGridColumn headerText="Salvar" width="50">

                  <mx:itemRenderer>

                  <mx:Component>

                  <mx:VBox width="100%" height="100%">

                  <mx:Button label="See the values" click="outerDocument.mostra();"/>

                  </mx:VBox>

                  </mx:Component>

                  </mx:itemRenderer>

                  </mx:DataGridColumn>

                          </mx:columns>

                      </mx:DataGrid>

                  </mx:Application>

                  • 6. Re: How to get the selectedIndex from a combobox made by classFactory?
                    gpbuil Level 1

                    Thanks harUI, your tip was KEY to that!!