My entry for the Adobe AIR Cookbook

Well, I finally decided on my entry for the Adobe AIR Cookbook contest. I had to do this for a work project and I found very little documentation on how to pull this off. My entry is the addition of a "Select/Deselect All" checkbox that will automatically select or deselect all checkboxes in a certain column of a datagrid. For my application at work, the user was selecting the checkboxes to determine which records they would be downloading to their local machine. In some cases the datagrid had several hundred entries, so checking them all manually just wouldn't do. Let's take a look at the code...


<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initgridData();">
    
    <mx:Script>
<![CDATA[
        //For this example, I'm using an ArrayCollection for my data provider. This data could also come from
        //the local SQLite database or from a CFC calling to a remote server.
import mx.collections.ArrayCollection;

[Bindable]
private var gridData:ArrayCollection = new ArrayCollection([
{checked:false, name:'Jim Leether', address:'123 Spruce Street', phone:'(555)555-1234'},
{checked:false, name:'Yancy Wharton', address:'728 Main Street', phone:'(555)555-4926'},
{checked:false, name:'Anne Smith', address:'923 Log Cabin Lane', phone:'(555)555-7365'},
{checked:false, name:'Robert Davidson', address:'498 Electric Avenue', phone:'(555)555-1674'},
{checked:false, name:'Cathy Perdue', address:'246 Cedar Drive', phone:'(555)555-2795'},
{checked:false, name:'Tom Jones', address:'562 Division Street', phone:'(555)555-4965'}]);

//Initialize your data provider at creation complete
public function initgridData():void {
myDataGrid.dataProvider = gridData;
}

//Loop over your data provider and either check or uncheck all boxes
private function selectAllCheckboxes():void{
    
                var allRows:int = gridData.length;
                
                for (var i:int = 0; i < allRows; i++){
                    if (selectAll.selected == true){
                        trace ("Checked is true");
                        gridData[i].checked = true;
                    }else{
                        trace ("Checked is false");
                        gridData[i].checked = false;
                    }
                }
                
                //After the loop, reset the data provider to refresh the check boxes
                myDataGrid.dataProvider = gridData;
            }
]]>

</mx:Script>
    
    <!-- Create a DataGrid with a check box in the first column using the mx:itemRenderer tag -->
    <mx:DataGrid id="myDataGrid" left="10" top="10">
        <mx:columns>
            <mx:DataGridColumn headerText="Selected" textAlign="center" dataField="checked" width="90">
                <mx:itemRenderer>
                    <mx:Component>
                        <mx:CheckBox click="data.checked=!data.checked" selected="{data.checked}"/>
                    </mx:Component>
                </mx:itemRenderer>
            </mx:DataGridColumn>
            <mx:DataGridColumn headerText="Name" dataField="name" width="110"/>
            <mx:DataGridColumn headerText="Address" dataField="address" width="150"/>
            <mx:DataGridColumn headerText="Phone" dataField="phone" width="110"/>
        </mx:columns>
    </mx:DataGrid>
    
    <!-- This checkbox will call the function that selects or deselects all the checkboxes in the datagrid -->
    <mx:CheckBox id = "selectAll" label="Select/Deselect All" click="selectAllCheckboxes();" left="10" top="175"/>
    
    
</mx:WindowedApplication>

As you can see, it looks complicated, but it's really very simple. The code simply loops over the value of each checkbox in the datagrid's data provider and gives it the same value as the "Select/Deselect All" checkbox. Once it finishes updating the datasource, it just refreshes the display. The check boxes will still function independantly from the Select All box as well. Hopefully this will get some attention.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
dd's Gravatar This is excellent. It took me less than 5 minutes to implement.
Great job!
# Posted By dd | 10/22/08 2:47 PM
Gordon Jones's Gravatar I really liked your method. thanks for sharing this:) hope many people will find it useful as I did. have read lots of articles on the topic downloaded by http://rapid4me.com rapidshare SE , but have never thought that could be so easy!
# Posted By Gordon Jones | 2/15/10 8:58 AM
George's Gravatar Yeah agreed, nice work. Was really quick to implement, thanks. http://www.rapidsloth.com offered some help but this was by far the best.
# Posted By George | 3/8/10 7:55 PM
Jim Leether's Gravatar I'm glad everyone is finding this useful! I need to start blogging a little more consistently now. Maybe I can help more than I originally thought.
# Posted By Jim Leether | 3/9/10 6:31 PM
Kishore's Gravatar This is good examples i have seen till now. I would like to add more on this, how come if the select all check box is in the header of the datagrid(instead of selectall if a check box) when selecting that we can do the same as select all and deselect all.
# Posted By Kishore | 4/9/10 12:00 PM
Jim Leether's Gravatar If I understand what you're asking, you want to be able to put the Select/Deselect All checkbox in the column header of the datagrid. It absolutely can be done! In order to do that you have to write extensions for the datagrid component. You can find the code to do that here. http://my.opera.com/phamthanhson/blog/use-checkbox...
# Posted By Jim Leether | 4/9/10 2:41 PM
# Posted By alice | 4/15/10 5:41 AM
Copyright � 2008 - Jim Leether BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.001. Contact Jim