Datagrid项呈示器中选定复选框的问题

时间:2012-05-16 12:18:21

标签: flex datagrid checkbox itemrenderer

我的数据网格中有一个复选框作为itemRenderer。我根据Arraycollection中包含的对象的布尔属性设置选定的值。

当Arraycollection从我的后端返回时,该属性的所有布尔值都从后端设置为false ...但是数据网格继续显示先前选择的值,尽管我已将DataGrid的数据提供者设置为新的ArrayCollection。

这是我的代码:

我的MXML:

<mx:DataGrid id="dg" width="392" rowCount="8" rowHeight="24" >
        <mx:columns>
            <mx:DataGridColumn headerText="Nom" dataField="name" width="70"  />
            <mx:DataGridColumn headerText="Statut" dataField="currentStatus" width="100" />
            <mx:DataGridColumn headerText="TA"   dataField="ta" width="20" />
            <mx:DataGridColumn headerText="TP"   dataField="tp" width="20"  />

            <comp:CheckBoxHeaderColumn id="checkAllCol"
                                       fontWeight="bold"
                                       visible="{isVisible}"
                                       sortable="false"
                                       rendererIsEditor="true"
                                       headerRenderer="be.rtbf.radio.cockpit.rds.view.component.CheckBoxHeaderRenderer"
                                       itemRenderer="be.rtbf.radio.cockpit.rds.view.component.CheckBoxItemRenderer"
                                       headerText="EON"
                                       textAlign="center"
                                       width="60" />
        </mx:columns>

CheckBoxItemRenderer:

{

    import flash.events.Event;
    import flash.events.MouseEvent;
    import mx.collections.ArrayCollection;
    import mx.controls.Alert;
    import mx.controls.CheckBox;
    import mx.controls.DataGrid;
    import mx.events.ListEvent;

    import org.puremvc.as3.patterns.facade.Facade;

    public class CheckBoxItemRenderer extends CheckBox
    {
        private var _checkBoxHeaderColumn : CheckBoxHeaderColumn;
        private var _data :  Object;

        private var _deselect : Boolean = false;

        public function CheckBoxItemRenderer()
        {
            super();
        }

        override public function set data(value:Object):void {

            if(!value) {
                selected = false;
            }
            else {
                var dataGrid : DataGrid = listData.owner as DataGrid;
                var list : ArrayCollection = dataGrid.dataProvider as ArrayCollection;
                dataGrid.dataProvider.

                _data = value;


                _checkBoxHeaderColumn = dataGrid.columns[listData.columnIndex];
                if(selected) {
                    addItem(_checkBoxHeaderColumn.itemsSelected, encoder);
                }

                _checkBoxHeaderColumn = dataGrid.columns[listData.columnIndex];
                _checkBoxHeaderColumn.addEventListener(MouseEvent.CLICK, columnHeaderClickHandler);
                _checkBoxHeaderColumn.checkBoxItemRenderer = this;
                if (_checkBoxHeaderColumn.itemsSelected != null && list != null) {
                    selected = containsItem(_checkBoxHeaderColumn.itemsSelected, value);
                }
                super.data = value;
            }
        }

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

        override protected function clickHandler(event:MouseEvent):void {
            super.clickHandler(event);
            var dataGrid : DataGrid = listData.owner as DataGrid;
            var list : ArrayCollection = dataGrid.dataProvider as ArrayCollection;
            var item : Encoder = list[listData.rowIndex + dataGrid.verticalScrollPosition];

            if (!selected) {
                _checkBoxHeaderColumn.checkBoxHeaderRenderer.selected = selected;
                removesItem(_checkBoxHeaderColumn.itemsSelected, item);
            }
            else {
                addItem(_checkBoxHeaderColumn.itemsSelected, item);
                if (_checkBoxHeaderColumn.itemsSelected.length == list.length)
                    _checkBoxHeaderColumn.checkBoxHeaderRenderer.selected = selected;
            }

            // Envoie de l'objet sélectionné et activation des boutons save/cancel
            var eventL : ListEvent = new ListEvent(ListEvent.ITEM_CLICK,true);
            eventL.rowIndex = listData.rowIndex;
            dispatchEvent(eventL);

            /*if(_checkBoxHeaderColumn.itemsSelected.length == 0) {
                dispatchEvent(new Event(Event.CANCEL, true));
            }*/

        }

        public function containsItem(list : ArrayCollection, target : Object) : Boolean {
            for each (var item : Object in list) {
                if (item.id == target.id)
                    return true;
            }
            return false;
        }

        public function removesItem(list : ArrayCollection, target : Encoder) : void {
            for (var index : int = 0; index < list.length; index++) {
                if (list.getItemAt(index).id == target.id) {
                    list.removeItemAt(index);
                    return;
                }
            }
        }

        public function addItem(list : ArrayCollection, target : Encoder) : void {
            if(!list) {
                list = new ArrayCollection();
            }
            list.addItem(target);
            _checkBoxHeaderColumn.itemsSelected = list;
        }

        private function columnHeaderClickHandler(event:MouseEvent):void {
            selected = _checkBoxHeaderColumn.checkBoxHeaderRenderer.selected;

        }
    }
}

CheckBoxHeaderRenderer

public class CheckBoxHeaderRenderer extends CheckBox {
        private var _headerColumn : CheckBoxHeaderColumn;
        private var  _checkBoxItemRenderer : CheckBoxItemRenderer;
        private var _deselect : Boolean = false;

        public function CheckBoxHeaderRenderer() {
            super();
        }

        override public function set data(value:Object):void {
            super.data = value;
            var list : ArrayCollection = (listData.owner as DataGrid).dataProvider as ArrayCollection;
            _headerColumn = value as CheckBoxHeaderColumn;
            _headerColumn.checkBoxHeaderRenderer = this;
            label = _headerColumn.headerText;

            //selected = (_headerColumn.itemsSelected.length == list.length) ? true : false;
        }

        override protected function clickHandler(event:MouseEvent):void {
            super.clickHandler(event);
            if (!selected) {
                _headerColumn.itemsSelected = new ArrayCollection();
                var array : ArrayCollection = (listData.owner as DataGrid).dataProvider as ArrayCollection;

                if(!_deselect) {
                    dispatchEvent(new Event(Event.CANCEL, true));
                }
            }
            else {
                var list : ArrayCollection = cloneArrayColection((listData.owner as DataGrid).dataProvider as ArrayCollection);
                if(list.length==0) {
                    return;
                }
                _headerColumn.itemsSelected = list;
                dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE,true));

            }
            _deselect = false;
            _headerColumn.dispatchEvent(event);
        }

提前致谢

0 个答案:

没有答案