3 Replies Latest reply on Jan 13, 2010 7:44 AM by Gregory Lafrance

    CheckBox in Datagrid, adding selected items to array

    KevMull

      I have an checkbox column in a datagrid.

      I would like to add the data from a certain column ('myField') to an array if the check box on that row is selected and removed it is unselected.

       

      Many Thanks

        • 1. Re: CheckBox in Datagrid, adding selected items to array
          Gregory Lafrance Level 6
          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
          This code does what you want:
          
          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
            creationComplete="init();">
            <mx:Script>
              <![CDATA[
                import mx.collections.ArrayCollection;
                
                [Bindable] private var ac:ArrayCollection = new ArrayCollection([
                  {myField: "test1", FLAG: false},
                  {myField: "test2", FLAG: false},
                  {myField: "test3", FLAG: false},
                  {myField: "test4", FLAG: false}
                ]); 
                
                [Bindable] private var selectedAC:ArrayCollection = new ArrayCollection(); 
                
                public function getSelected():void{
                  selectedAC.removeAll();
                  for each(var obj:Object in ac){
                    if(obj.FLAG){
                      selectedAC.addItem(obj.myField);
                    }
                  }
                }          
              ]]>
            </mx:Script>
            <mx:HBox>
              <mx:Label text="Selected values:"/>
              <mx:ComboBox dataProvider="{selectedAC}"/>    
            </mx:HBox>
            <mx:DataGrid id="dg" dataProvider="{ac}">
              <mx:columns>
                <mx:DataGridColumn dataField="myField"/>
                <mx:DataGridColumn dataField="FLAG">
                  <mx:itemRenderer>
                    <mx:Component>
                      <mx:CheckBox selectedField="FLAG" change="onChange(event);" label="">
                        <mx:Script>
                          <![CDATA[
                            private function onChange(evt:Event):void {
                              data.FLAG = !data.FLAG;
                              outerDocument.getSelected();
                            }
                          ]]>
                        </mx:Script>
                      </mx:CheckBox>
                    </mx:Component>
                  </mx:itemRenderer>            
                </mx:DataGridColumn>
              </mx:columns>
            </mx:DataGrid>
          </mx:Application>
          
          • 2. Re: CheckBox in Datagrid, adding selected items to array
            KevMull Level 1

            Thanks for the reply.

             

            However, I cannot check teh checkbox, it seems to be 'locked'!

             

            Also the datagrdid data is coming from an xml.   How would I change it to suit?

            I also need to populate a textArea with the selected values (array).

             

            Sorry for the additional Qs.

             

            Many Thanks

            • 3. Re: CheckBox in Datagrid, adding selected items to array
              Gregory Lafrance Level 6

              Having this data in XML should not be a problem, just have a FLAG field.

               

              You may need to use if(data.@FLAG=="true") because sometimes boolean data is treated as strings.

               

              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