【问题标题】:Flex: CheckBoxes in DataGrid not updating after change in binded ArrayCollectionFlex:在绑定的 ArrayCollection 更改后,DataGrid 中的复选框未更新
【发布时间】:2009-07-14 20:51:19
【问题描述】:

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

打包我的组件 { 导入 flash.display.DisplayObject; 导入 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 之后复选框没有更新?

谢谢!

【问题讨论】:

    标签: apache-flex


    【解决方案1】:

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

    还要确保你的类名以大写开头

    【讨论】:

      【解决方案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>
      

      【讨论】:

        猜你喜欢
        • 2011-10-23
        • 2011-02-24
        • 2017-10-16
        • 2011-04-07
        • 2012-12-04
        • 2012-01-14
        • 1970-01-01
        • 2017-03-23
        • 2019-11-10
        相关资源
        最近更新 更多