4 Replies Latest reply on Jan 18, 2010 9:27 AM by KevMull

    Radiogroup in Datagrid, Adding SelecterdValues to Array

    KevMull

      I have a radiogroup in a datagrid column.

       

      I need to loop through it and add the selected value to an array.

       

      How do I do this?

       

      radiogroup in dg ..

       

      <mx:DataGridColumn headerText="Comp/Opt" dataField="compulsory" textAlign="center" sortable="false" >
                          <mx:itemRenderer>
                          <mx:Component>
                          <mx:HBox>
                          <mx:RadioButtonGroup id="compOptRadioGroup" />                  
                          <mx:RadioButton selected="true" label="comp" value="y" group="{compOptRadioGroup}"/>
                          <mx:RadioButton label="opt" value="n" group="{compOptRadioGroup}"/>                   
                          </mx:HBox>
                          </mx:Component>
                          </mx:itemRenderer>           
          </mx:DataGridColumn>

       

       

       

      Many Thanks

        • 1. Re: Radiogroup in Datagrid, Adding SelecterdValues to Array
          SpaghettiCoder Level 3

          Is looping through it, really necessary? Do you have multiple radio groups or just one, and are you processing this right away, or after the user hits some sort of submit button.

           

          (1) Get selected value.

           

          (2) Then push to array.

           

          [ Example ]

           

          myArray.push(compOptRadioGroup.selectedValue);

          • 2. Re: Radiogroup in Datagrid, Adding SelecterdValues to Array
            Gregory Lafrance Level 6

            This code should provide a solution.

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
              <mx:Script>
                <![CDATA[
                  import mx.collections.ArrayCollection;
                  
                  [Bindable] public var ac:ArrayCollection = new ArrayCollection([
                    {test: "one", compulsory: "y"},
                    {test: "two", compulsory: "y"},
                    {test: "three", compulsory: "y"},
                    {test: "four", compulsory: "y"},
                    {test: "five", compulsory: "y"}        
                  ]);
                ]]>
              </mx:Script>
              <mx:DataGrid dataProvider="{ac}">
                <mx:columns>
                  <mx:DataGridColumn dataField="test"/>
                  <mx:DataGridColumn headerText="Comp/Opt" dataField="compulsory" 
                    textAlign="center" sortable="false" width="150">
                    <mx:itemRenderer>
                      <mx:Component>
                        <mx:HBox>
                          <mx:Script>
                            <![CDATA[
                              private function onChange(evt:Event):void {
                                data.compulsory = compOptRadioGroup.selectedValue;
                                outerDocument.txt.text = "";
                                for each(var obj:Object in outerDocument.ac){
                                  outerDocument.txt.text += obj.compulsory + "\n";
                                }
                              }
                            ]]>
                          </mx:Script>                            
                          <mx:RadioButtonGroup id="compOptRadioGroup" itemClick="onChange(event)"/>                   
                          <mx:RadioButton selected="true" label="comp" value="y" 
                            group="{compOptRadioGroup}"/>
                          <mx:RadioButton label="opt" value="n" 
                            group="{compOptRadioGroup}"/>                    
                        </mx:HBox> 
                      </mx:Component>
                    </mx:itemRenderer>            
                    </mx:DataGridColumn>
                </mx:columns>
              </mx:DataGrid>
              <mx:TextArea id="txt" width="100%" height="100%"/>
            </mx:Application>
            


            If this post answers your question or helps, please mark it as such.


            Greg Lafrance - Flex 2 and 3 ACE certified

            www.ChikaraDev.com

            Flex / AIR Development, Training, and Support Services

            1 person found this helpful
            • 3. Re: Radiogroup in Datagrid, Adding SelecterdValues to Array
              KevMull Level 1

              Many thnaks Greg, that work sgreat.

               

              Just one more thing...

               

              How do I get the corresponding option buttons to default to the selected values in the data?

               

              So if ...

               

              {test: "one", compulsory: "y"},
                      {test: "two", compulsory: "n"},
                      {test: "three", compulsory: "y"},
                      {test: "four", compulsory: "n"},
                      {test: "five", compulsory: "y"}   

               

              Where the left hand button 'comp' = 'y' and the right hand one 'Opt' = 'n'?

               

              In teh above example they should alternate by default.

               

              Thanks again

              • 4. Re: Radiogroup in Datagrid, Adding SelecterdValues to Array
                KevMull Level 1

                Yes I need to loop through all rows of the dg as they wil be inserted into a db

                .

                Upon clicking the 'submit button' I have a function that loops through the dg rows and send the column data to a db.

                The text column works fine but I cannot get  the radio groups slected value, it just shows as null.

                 

                Here is part of the code in the function that does the loop....

                 

                var rows:Number = grid.dataProvider.length;
                var params:Object = new Object;
                var count:Number = 0;
                var planCodeSelected:Array = new Array();  // array 1 for text column works ok
                var compOptSelected:Array = new Array(); // array 2 for radio group selected value does not work

                 

                for (count = 0; count < rows ; count++)
                   {

                    planCodeSelected.push((grid.dataProvider.getItemAt(count)).planCode);

                 

                Alert.show(grid.dataProvider.getItemAt(count).planCode); // shows column value OK
                Alert.show(grid.dataProvider.getItemAt(count).compOptRadioGroup.selectedValue); // SHOWS NOTHING!!

                 

                etc etc

                }

                 

                If I can just get the selcted value to show up in teh abovedemo alert thn i'll be on the right road.

                 

                Many Thanks