Actionscript – Flex checkbox itemrenderer in datagrid

actionscriptactionscript-3apache-flexdatagridflex3

in my flex application im using a datagrid which has three coloumns
first column is for checkbox as itemRenderer and
other two columns are editable.

my requirement is

now when i fetch some data from DB the datas will be populated in datagrid,

1.after populating datas, first column should be disabled in all rows
2.if i click second or third column in a row for editing.. the checkbox in first column corresponding row should be enabled and checkbox should be ready for check and uncheck..

how can i do this
here's my code

<mx:DataGrid x="46" y="135" dataProvider="{DetailsProvider}" width="836" height="349">
    <mx:columns>
        <mx:DataGridColumn headerText="Select" dataField="isSelect" itemRenderer="com.components.checkbox"/>
        <mx:DataGridColumn headerText="First Name" dataField="fname"/>
        <mx:DataGridColumn headerText="Second Name" dataField="sname"/></mx:columns></mx:DataGrid>

I hope this details is enough to understand my question
Any idea for this.. Thanks in advance..

Best Answer

I am providing you the sample code that i have created that uses ItemRenderer and ItemEditor both in a single DataGrid you just need to create a new mxml component that is name d ComNS and ComCB. Just create that i am givng you the sample code.

Code for main mxml Appication

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" horizontalAlign="center" 
verticalAlign="middle" height="100%" width="100%">

<mx:Script>
    <![CDATA[
        public var arr:Array = new Array({isSelected:true,label:'ABC',score:'78',name:'ABC'},
                                         {isSelected:true,label:'DEF',score:'50',name:'DEF'},
                                         {isSelected:false,label:'GHI',score:'70',name:'GHI'},
                                         {isSelected:false,label:'JKL',score:'80',name:'JKL'},
                                         {isSelected:true,label:'TRE',score:'50',name:'MNO'});

        public function dgCLG_dataChange():void
        {

        }

        public function dgCLG_change():void
        {

        }

        public function btnSubmit_click():void
        {
            dgCopy.dataProvider = dgCLG.dataProvider;
        }

    ]]>
</mx:Script>

<mx:VBox height="100%" width="100%" horizontalAlign="center" verticalAlign="middle">
    <mx:DataGrid id="dgCLG" dataProvider="{arr}" editable="true" dataChange="{dgCLG_dataChange();}" change="{dgCLG_change();}">
        <mx:columns>
            <mx:DataGridColumn headerText="" dataField="isSelected">
                <mx:itemRenderer>
                    <mx:Component>
                        <mx:Box horizontalAlign="center" verticalAlign="middle" height="100%" width="100%">
                            <mx:Script>
                                <![CDATA[
                                    override public function set data(value:Object):void
                                    {
                                        if(value != null)
                                        {
                                            super.data = value;
                                            var temp:Object = value as Object;
                                            chb.selected = temp.isSelected;
                                        }
                                    }
                                ]]>
                            </mx:Script>
                            <mx:CheckBox id="chb"/>
                        </mx:Box>
                    </mx:Component>                     
                </mx:itemRenderer>
            </mx:DataGridColumn>
            <mx:DataGridColumn headerText="Label" dataField="label" editable="false">

            </mx:DataGridColumn>
            <mx:DataGridColumn headerText="Marks" dataField="score" editable="true" itemEditor="ComNS" editorDataField="value">

            </mx:DataGridColumn>
            <mx:DataGridColumn dataField="name" headerText="Person" itemEditor="ComCB" editorDataField="value" editable="true">

            </mx:DataGridColumn>
        </mx:columns>
    </mx:DataGrid>  

    <mx:Button id="btnSubmit" label="Click" click="{btnSubmit_click();}" />

    <mx:DataGrid id="dgCopy" editable="false">
        <mx:columns>
            <mx:DataGridColumn headerText="CopyLabel" dataField="label" />
            <mx:DataGridColumn headerText="CopyMarks" dataField="score" />
            <mx:DataGridColumn headerText="CopyPerson" dataField="name" />
        </mx:columns>
    </mx:DataGrid>
</mx:VBox>

</mx:Application>

Code for ComNS.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:NumericStepper xmlns:mx="http://www.adobe.com/2006/mxml" minimum="0" maximum="100">

</mx:NumericStepper>

Code for ComCB.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml" dataProvider="{arr}" selectedIndex="1" creationComplete="{c_complete();}" >
<mx:Script>
    <![CDATA[
        public var arr:Array = new Array({label:'ABC'},{label:'DEF'},{label:'GHI'},{label:'JKL'},{label:'MNO'})

        public function c_complete():void
        {
            for(var i:int = 0; i < arr.length; i++)
            {
                if(arr[i].label == parentDocument.dgCLG.selectedItem.name)
                {
                    this.selectedItem = arr[i];
                }
            }   
        }
    ]]>
</mx:Script>
</mx:ComboBox>

If any query just comment on my answer....

Related Topic