4 Replies Latest reply on Sep 9, 2010 11:54 PM by Ibarim

    comboBox inside dataGrid: setting combo selected index

    Ibarim

      Hi,

       

      so I got a dataGrid with an itemEditor which is a comboBox. Like this:

       

      <mx:DataGrid id="dg" dataProvider="{myArrayCollection}" editable="true">
         <mx:columns>
             <mx:DataGridColumn dataField="name" >
                <mx:itemEditor>
                  <mx:Component>
                  <mx:ComboBox labelField="name" dataProvider="{parentDocument.myArrayCollectionDict}"/>
                  </mx:Component>
               </mx:itemEditor>
             </mx:DataGridColumn>
         </mx:columns>
      </mx:DataGrid>
      

       

      And I got two array collections filled with objects which are database records:

       

      myArrayCollection :

       

      [0] = Object 
      id = 1     
      idP = 1 
      name = "name 1"
      [1] = Object 
      id = 2     
      idP = 1 
      name = "name 2"
      
      

       

      myArrayCollectionDict (which is a dictionary for myArrayCollection):

       

      [0] = Object 
      id = 1     
      name = "name 1"
      
      [1] = Object 
      id = 2     
      name = "name 2"
      

       

      And I want to set combo box at the index which has the same name as the name in dataGrid. But now when I click on the grid cell with some name in it the combo appears, but with no value at all. Is there a way to set it's index somehow to appropriate name?

        • 1. Re: comboBox inside dataGrid: setting combo selected index
          betheflexcoder Level 2

          Create a cutom Item renderer for the combobox , override the set data method and write your required logic in the set data method. Overriding this method is very important to get the item renderers work properly as expected.

          • 2. Re: comboBox inside dataGrid: setting combo selected index
            betheflexcoder Level 2

            Create a custom Item renderer for the combobox , override the set data method and write your required logic in the set data method. Overriding this method is very important to get the item renderers work properly as expected.

            • 3. Re: comboBox inside dataGrid: setting combo selected index
              Gregory Lafrance Level 6

              Please show minimal yet complete code showing the issue, it makes it easier to understand what you are trying to achieve and then offer suggestions.

              • 4. Re: comboBox inside dataGrid: setting combo selected index
                Ibarim Level 1

                Ok. So I got an array collection filled with brand names (it's called myArrayCollectionDict). And I got second array collection filled with brand names assigned to a specific person (this array collection is called myArrayCollection). I display some person's profile with a data grid - this data grid show brands assigned to this person (data provider is myArrayCollection). I want this data grid to be editable, so after clicking on the brand's name in data grid the combo box should show up. This combo box should be filled with all the brands names (data provider is myArrayCollectionDict). And this combo box should have selected item set on the brand name which was clicked in the data grid.

                And this last part is where I have problem. I suppose I can create a loop inside this item editor component, this loop would search inside myArrayCollectionDict for the property 'name' which value is the same as value of property 'name' choosen in the data grid. Or I can compare myArrayCollection 'name' property with itemEditor's 'data.name' property. But in both cases I need to have a loop inside itemEditor. I was wondering if I can do this without a loop, in some magic way of which I didn't hear yet:)

                 

                I'm not sure if I make myself clear here:) So I'll put my proposition of loop below.

                 

                <mx:DataGrid id="dg" dataProvider="{myArrayCollection}" editable="true">        <mx:columns>
                       <mx:DataGridColumn dataField="name" >
                          <mx:itemEditor>
                            <mx:Component>
                              <mx:hBox>
                                 <mx:ComboBox id="combo1" labelField="name" dataProvider="{parentDocument.myArrayCollectionDict}"/>
                                    <mx:Script>
                                        <![CDATA[
                                            for (var i:int=0;i<parentDocument.myArrayCollectionDict.length;i++) {
                                              if (parentDocument.dg.selectedItem.name = parentDocument.myArrayCollectionDict[i].name) {
                                                  combo1.selectedIndex = i;
                                                  break;
                                               }
                                            }
                                        ]]>
                                </mx:Script>
                
                              </mx:hBox>
                            </mx:Component>
                         </mx:itemEditor>
                       </mx:DataGridColumn>
                   </mx:columns>
                </mx:DataGrid>
                 

                 

                I'm unfamiliar to item editors and item renderers so I'm not sure which of them to use? Should I use override data set method? Should I use the loop or is there more simple way?