Flex:在绑定的ArrayCollection中更改后,DataGrid中的CheckBox不会更新

时间:2009-07-14 20:51:20

标签: flex

我扩展了CheckBox类,将其用作itemRenderer,使其在DataGrid单元格中居中,并反映ArrayCollection更改。这是.as:

打包myComponents {     import flash.display.DisplayObject;     import flash.text.TextField;

import mx.controls.CheckBox;
import mx.controls.dataGridClasses.DataGridListData;
import mx.controls.listClasses.BaseListData;
import mx.controls.listClasses.IDropInListItemRenderer;
import mx.managers.IFocusManagerComponent

public class myCheckBox
    extends CheckBox
    implements IDropInListItemRenderer//,IFocusManagerComponent    
{
    private var _listData:DataGridListData;

    public function myCheckBox()
    {
        super();
    }

    override protected function updateDisplayList(w:Number, h:Number):void
    {
        super.updateDisplayList(w, h);

        var n:int = numChildren;
        for (var i:int = 0; i < n; i++)
        {
            var c:DisplayObject = getChildAt(i);
            if (!(c is TextField))
            {
                c.x = (w - c.width) / 2;
                c.y = 0;
            }
        }
    }

    // Implement the drawFocus() method for the VBox.
    /*
        override public function drawFocus(draw:Boolean):void {
            setFocus();
        }*/

        [Bindable]
        override public function set data(value:Object):void{
            super.data = value;
            selected=data[_listData.dataField];
        }

        override public function get data():Object {
            return super.data;
        }            

           override public function get listData():BaseListData
        {
            return _listData;
        }

        override public function set listData(value:BaseListData):void
        {
            _listData = DataGridListData(value);
        }    

}

}

在应用程序中,我将它与数组集合绑定:

             

        [Bindable]
        private var myAC:ArrayCollection = new ArrayCollection([
            {id:89, Seccion: 'Bob Jones', Leer: true , Escribir: true , Eliminar: false},
            {id:5, Seccion: 'Jane Smith', Leer: true , Escribir: false , Eliminar: false},    
            {id:7, Seccion: 'Doug Johnson', Leer: false , Escribir: true , Eliminar: true},
            {id:15, Seccion: 'John Jackson', Leer: true , Escribir: false , Eliminar: false},    
            {id:21, Seccion: 'Christina Coenraets', Leer: true , Escribir: true , Eliminar: false},
            {id:4, Seccion: 'Joanne Wall', Leer: false , Escribir: false , Eliminar: true},
            {id:461, Seccion: 'Maurice Smith', Leer: false , Escribir: false , Eliminar: false},
            {id:35, Seccion: 'Lorraine Barnes', Leer: true , Escribir: true , Eliminar: true},    
            {id:61, Seccion: 'The Dude', Leer: true , Escribir: false , Eliminar: true},
            {id:56, Seccion: 'Abe Rockaway', Leer: true , Escribir: true , Eliminar: false}

        ]);

        private function init():void{
            myAC.addEventListener(CollectionEvent.COLLECTION_CHANGE, onChange);
            myAC.enableAutoUpdate();
        }

        private function onChange(event:CollectionEvent):void
        {

            //myAC.disableAutoUpdate();
            var obj:Object=event.items[0].source;
            var i:uint=myAC.getItemIndex(obj);
            switch (event.items[0].property)
            {
                case "Leer":
                    if(obj["Leer"]==false)
                    {                            
                        obj["Escribir"]=false;
                        obj["Eliminar"]=false;
                    }
                    break;
                case "Escribir":
                    if(obj["Escribir"]==false)
                        obj["Eliminar"]=false;
                    else
                        myAC[i]["Leer"]=true;
                    break;
                case "Eliminar":
                    if(obj["Eliminar"]==true)
                    {
                        obj["Escribir"]=true;
                        obj["Leer"]=true;
                    }
                    break;
                default:
                    break;
            }                
           // myAC.enableAutoUpdate();
            myAC.itemUpdated(obj);
            //myGrid.validateNow();
        }

    ]]>
</mx:Script>
<mx:DataGrid id="myGrid" 
    dataProvider="{myAC}" editable="true" >  
    <mx:columns>
        <mx:DataGridColumn dataField="Seccion" width="150" headerText="Sección"/>
        <mx:DataGridColumn dataField="Leer"
            width="100"
            headerText="Leer"
            itemRenderer="myComponents.myCheckBox"
            rendererIsEditor="true"
            editorDataField="selected"/>
        <mx:DataGridColumn dataField="Escribir"
            width="100"
            headerText="Escribir"
            itemRenderer="myComponents.myCheckBox"
            rendererIsEditor="true"
            editorDataField="selected"/>
        <mx:DataGridColumn dataField="Eliminar"
            width="100"
            headerText="Eliminar"
            itemRenderer="myComponents.myCheckBox"
            rendererIsEditor="true"
            editorDataField="selected"/>
    </mx:columns>
</mx:DataGrid>  

<mx:TextArea id="cellInfo" width="300" height="150"/>
<mx:Label id="lb" text=""/>

onChange函数是COLLECTION_CHANGE的处理程序。在这个函数中,我更改了绑定到dataGrid的myAC(arrayCollection)。这些更改应选中或取消选中受影响的复选框,但这些复选框的显示仅在滚动后显示在dataGrid显示区域后才会更新。

我不想将复选框放在画布或任何容器中。

为什么myAC.itemUpdated后复选框没有更新?

谢谢!

2 个答案:

答案 0 :(得分:2)

您可以尝试这样做:myAC.refresh();我不确定这是否有效。否则尝试将arrayCollection重新分配给您的数据网格。也许这有点脏,但如果它有效......

还要确保您的班级名称以大写字母

开头

答案 1 :(得分:2)

您不想将其放在容器中,但您也可以使用mxml创建itemRenderer:

    <?xml version="1.0" encoding="utf-8"?>
<mx:CheckBox xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script>
        <![CDATA[
            override public function set data(value:Object):void
            {
                super.data = value;   
                    this.selected = data.Leer;
            }

        ]]>
    </mx:Script>
</mx:CheckBox>