开发者

Selecting row on selection of CheckBox in Datagrid

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 tr开发者_如何学JAVAying 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


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


I would try 2 things(if you haven't tried them):

  1. Make sure "selectable" flag is set to true
  2. When clicking the check box - add that item to the datagrids selectedItems array

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜