Apache – Selecting row on selection of CheckBox in Datagrid

apache-flex

I have a datagrid with 5 columns. The first column has checkboxs and rest four columns have data from the dataProvider.

I have two relating issues:
First: I want the row to get selected when I click on a checkbox, or if i click on multiple checkboxes, every row should stay selected in the datagrid for whose checkbox is selected.

Second: If a row or multiple rows are selected, the corresponding checkbox should get selected too.

My code is given below:

<mx:DataGrid id="dg_trashContent" allowMultipleSelection="true"
                     verticalGridLines="true"
                     dataProvider="{trashDataProvider}"                      
                     width="100%"                        
                     height="100%"
                     fontFamily="Arial">    
    <mx:columns>
        <mx:DataGridColumn id="col0"
                            textAlign="left"
                            sortable="false"
                            headerText="Name"
                            headerStyleName="datagridHeader"
                            dataField="name">
                    <mx:itemRenderer>
                        <mx:Component>
                            <mx:VBox horizontalAlign="center">
                                <mx:Script source="base.as"/>
                                <mx:Script>
                                    <![CDATA[
                                        import mx.controls.Alert;                                           
                                        public function selectRow():void{

                                        }                                           
                                    ]]>
                                </mx:Script>
                                <mx:CheckBox id="rowCheckBox" click="selectRow();"/>
                            </mx:VBox>
                        </mx:Component>
                    </mx:itemRenderer>
        </mx:DataGridColumn>
        <mx:DataGridColumn id="col1"
                            textAlign="left"
                            sortable="false"
                            headerText="Name"
                            headerStyleName="datagridHeader"
                            dataField="name"/>
        <mx:DataGridColumn id="col2"
                            textAlign="left"
                            sortable="false"
                            headerText="Original Location"
                            headerStyleName="datagridHeader"
                            dataField="absoluteTag"/>                                           
        <mx:DataGridColumn id="col4"
                            textAlign="left"
                            sortable="false"
                            headerText="Date Deleted"
                            headerStyleName="datagridHeader"
                            dataField="date"/>
        <mx:DataGridColumn id="col5"
                            textAlign="right"
                            sortable="false"
                            headerText="Size (mb)"
                            headerStyleName="datagridHeader"
                            dataField="size"/>
    </mx:columns>
</mx:DataGrid>          

I have been trying to figure out how should i do it from a while now. But could not get any solution. Can someone plz help me.

Regards
Zeeshan

Best Answer

you need to write a custom item renderer for checkbox and also modify the code of DataGrid by extending it and override some of the methods like selectItem()

see: http://cookbooks.adobe.com/post_Datagrid_headerRenderer_checkbox_to_select_checkbo-7262.html

Related Topic