4 Replies Latest reply on Feb 15, 2010 12:41 AM by Francisc

    DataGrid Checkbox

    Francisc Level 3

      Hello!

       

      I have a DataGrid with this column:

      <mx:DataGridColumn headerText="" width="25" itemRenderer="mx.controls.CheckBox" rendererIsEditor="true" editorDataField="selected" sortable="false" paddingLeft="6"/>

       

      How can I get the ArrayCollection made up of the rows that have that checkbox selected when a button is pressed for example?

       

      Thank you!

        • 1. Re: DataGrid Checkbox
          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

           

          Here you go:

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
            <mx:Script>
              <![CDATA[
                import mx.collections.ArrayCollection;
                import mx.controls.CheckBox;
                
                private var arr:Array = [
                  {name: "Bob Smith", FLAG: false},
                  {name: "Fred Tompas", FLAG: false},
                  {name: "Mary Freeman", FLAG: false},
                  {name: "Tim Thorat", FLAG: false}
                ];
                
                [Bindable] public var ac:ArrayCollection = new ArrayCollection(arr);
                [Bindable] public var selectedNames:ArrayCollection = new ArrayCollection();
                
                private function selectedOnly(item:Object, idx:uint, arr:Array):Boolean {
                  return (item.FLAG == "true");
                }
              ]]>
            </mx:Script>
            <mx:DataGrid id="dg" dataProvider="{ac}">
              <mx:columns>
                <mx:DataGridColumn dataField="name" width="100" headerText="Name"/>
                <mx:DataGridColumn dataField="FLAG" width="100" headerText="Selected">
                  <mx:itemRenderer>
                    <mx:Component>
                      <mx:CheckBox selectedField="FLAG" change="onChange(event);" label="">
                        <mx:Script>
                          <![CDATA[
                            import mx.controls.Alert;
                            private function onChange(evt:Event):void {
                              data.FLAG = !data.FLAG;
                              if(data.FLAG == true){
                                outerDocument.selectedNames.addItem(data.name);
                              }else{
                                if(outerDocument.selectedNames.contains(data.name)){
                                  outerDocument.selectedNames.removeItemAt(outerDocument.selectedNames.getItemIndex(data.name));                        
                                }
                              }
                            }
                          ]]>
                        </mx:Script>
                      </mx:CheckBox>
                    </mx:Component>
                  </mx:itemRenderer>            
                </mx:DataGridColumn>
              </mx:columns>
            </mx:DataGrid>
            <mx:List dataProvider="{selectedNames}"/>
          </mx:Application>
          
          
          1 person found this helpful
          • 2. Re: DataGrid Checkbox
            Francisc Level 3

            Hm, I was hoping for an easier way.

            Can I not iterate the DataGrid components somehow and check if the checkbox is checked and add to the second ArrayCollection for example?

            • 3. Re: DataGrid Checkbox
              Flex harUI Adobe Employee

              There is a checkbox datagrid example on my blog where the checkboxes change

              the selectedItems property

               

              --

              Alex Harui

              Flex SDK Team

              Adobe System, Inc.

              http://blogs.adobe.com/aharui

              • 4. Re: DataGrid Checkbox
                Francisc Level 3

                Thank you both!