DataGrid +CheckBox(全选)

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Declarations>
        <!-- 将非可视元素(例如服务、值对象)放在此处 -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            [Bindable]
            public var selected:Boolean = false;
            public var selectedAC = new ArrayCollection();
            [Bindable]
            public var dataAC = new ArrayCollection([
                {dataField1:1},
                {dataField1:2}
                ]);
            
            protected function button1_clickHandler(event:MouseEvent):void
            {
                
                
                for(var i:int =0;i<selectedAC.length;i++){
                    for(var j:int =0;j<dataAC.length;j++){
                        if(selectedAC[i]==dataAC[j]){
                            dataAC.removeAllElements();
                            dataAC.removeItemAt(j);
                            //dataAC.removeElementAt(j);
                            dataAC.refresh();
                            break;
                        }
                    }
                }
            }
            
            protected function button2_clickHandler(event:MouseEvent):void
            {
                if(this.selected){
                    btn2.label = "全选";
                    this.selected = false;
                    selectedAC.removeAll();
                }else{
                    btn2.label = "取消";
                    this.selected = true;
                    selectedAC.removeAll();
                    selectedAC.addAll(this.dataAC);
                }
            }
            
        ]]>
    </fx:Script>
    <s:DataGrid id="dg" x="177" y="136" requestedRowCount="4" dataProvider="{dataAC}" selectionMode="multipleRows">
        <s:columns>
            <s:ArrayList>
                <s:GridColumn dataField="xx" >
                    <s:itemRenderer >
                        <fx:Component>
                            <s:GridItemRenderer >
                                
                                <fx:Script>
                                    <![CDATA[
                                        import mx.collections.ArrayCollection;
                                        import mx.controls.Alert;
                                        import mx.events.FlexEvent;
                                        
                                        import spark.components.DataGrid;
                                        protected function checkbox_changeHandler1(event:Event):void
                                        {
                                            //checkBox.selected = false;
                                            
                                        }
                                        
                                        
                                        override public function set data(value:Object):void { 
                                            super.data = value; 
                                            checkBox.selected= false;
                                            if(this.parentApplication.selected){
                                                checkBox.selected= true;
                                            }
                                        }
                                        protected function checkbox_changeHandler2(event:Event):void
                                        {
                                            var checkBox:CheckBox = CheckBox(event.target);
                                            var selectedAC :ArrayCollection= ArrayCollection(this.parentApplication.selectedAC)
                                            if(checkBox.selected){
                                                selectedAC.addItem(data);
                                                var dataGrid:DataGrid = DataGrid (this.parentApplication.dg);
                                                for(var i:int =0;i<selectedAC.length;i++){
                                                    for(var j:int=0;j<dataGrid.dataProvider.length;j++){
                                                        if(dataGrid.dataProvider.getItemAt(j)==selectedAC.getItemAt(i)){
                                                            dataGrid.addSelectedIndex(j);
                                                        }
                                                    }
                                                }
                                                
                                            }else{
                                                for(var i:int=0;i<selectedAC.length;i++){
                                                    if(data==selectedAC.getItemAt(i)){
                                                        var dataGrid:DataGrid =DataGrid (this.parentApplication.dg);
                                                        dataGrid.removeSelectedIndex(rowIndex);
                                                        selectedAC.removeItemAt(i);
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                        
                                        
                                    ]]>
                                </fx:Script>
                                <s:HGroup>
                                    <s:Label width="10"/>
                                    <s:CheckBox id="checkBox" selected="{this.parentApplication.selected}" click="checkbox_changeHandler2(event)"/>
                                    <s:Label width="10"/>
                                </s:HGroup>
                            </s:GridItemRenderer>
                        </fx:Component>
                    </s:itemRenderer>
                    <s:headerRenderer>
                        <fx:Component>
                            <s:GridItemRenderer >
                                <fx:Script>
                                    <![CDATA[
                                        import mx.controls.Alert;
                                        import mx.collections.ArrayCollection;
                                        override public function set data(value:Object):void { 
                                            super.data = value; 
                                            this.parentApplication.selected = false;
                                            //checkBox.selected= false;
                                        }
                                        protected function data_changeHandler(event:Event){
                                            selectedAll.selected = false;
                                        }
                                        protected function selectedAll_clickHandler():void
                                        {
                                            var selectedAC :ArrayCollection= ArrayCollection(this.parentApplication.selectedAC)
                                            if(this.parentApplication.selected){
                                                this.parentApplication.selected = false;
                                                selectedAC.removeAll();
                                            }else{
                                                this.parentApplication.selected = true;
                                                selectedAC.removeAll();
                                                selectedAC.addAll(this.parentApplication.dataAC);
                                            }
                                            
                                            
                                            
                                        }
                                    ]]>
                                </fx:Script>
                                <s:HGroup>
                                    <s:Label paddingTop="5" text="全选 "/>
                                    <s:CheckBox id="selectedAll" click="selectedAll_clickHandler()"/>
                                    <s:Label width="10"/>
                                </s:HGroup>
                            </s:GridItemRenderer>
                        </fx:Component>
                    </s:headerRenderer>
                </s:GridColumn>
                <s:GridColumn dataField="dataField1" headerText="列 1"></s:GridColumn>
                <s:GridColumn dataField="dataField3" headerText="列 3"></s:GridColumn>
            </s:ArrayList>
        </s:columns>
        <s:typicalItem>
            <fx:Object dataField1="示例数据" dataField2="示例数据" dataField3="示例数据"></fx:Object>
        </s:typicalItem>
        
    </s:DataGrid>
    <s:Button x="232" y="308" label="删除" click="button1_clickHandler(event)"/>
    <s:Button x="242" y="364" id="btn2" label="全选" click="button2_clickHandler(event)"/>
    


</s:Application>


参考:http://www.myexception.cn/flex/292427_2.html

你可能感兴趣的:(DataGrid +CheckBox(全选))