Skip navigation
Rohini_1327
Currently Being Moderated

Checkbox itemrender of Advanced grid does not hold proper selections on next open event of tree in F

Jul 31, 2013 12:34 AM

Tags: #flex #actionscript #itemrenderer

I have a checkbox in AdvancedDataGrid as GroupItemrenderer.I have  selected certain child nodes in tree and closed its parent,later when I  reopen the same parent node,selected child nodes are not those that i  selected.How to retain the correct selections in checkbox.??

 

Also am unable to set the value for checkbox (GroupItemRenderer) by default from component and also unable to access the value for checkbox in Data property.

I am using 2D array as dataprovider to ADG.

 

package
{
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;

import mx.collections.ICollectionView;
import mx.collections.IHierarchicalCollectionView;
import mx.collections.IHierarchicalData;
import mx.collections.IViewCursor;
import mx.controls.AdvancedDataGrid;
import mx.controls.Alert;
import mx.controls.CheckBox;
import mx.controls.Image;
import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
import mx.controls.advancedDataGridClasses.AdvancedDataGridGroupItemRenderer;
import mx.controls.advancedDataGridClasses.AdvancedDataGridListData;
import mx.core.FlexGlobals;
import mx.core.mx_internal;

use namespace mx_internal;



public class CheckADGRenderer extends AdvancedDataGridGroupItemRenderer
{
   
protected var myImage:Image;
   
public var status:String = "false";

   
// set image properties
   
private var imageWidth:Number   = 6;
   
private var imageHeight:Number  = 6;
   
private var inner:String    = "inner.png";
   
protected var myCheckBox:CheckBox;
   
static private var STATE_SCHRODINGER:String = "schrodinger";
   
static private var STATE_CHECKED:String = "checked";
   
static private var STATE_UNCHECKED:String = "unchecked";

   
public function CheckADGRenderer ()
   
{
       
super();
        mouseEnabled
= false;
   
}
   
private function toggleParents(item:Object,adg:AdvancedDataGrid,state:String):void
   
{
       
if (item == null)
       
{
           
return;
       
}
       
else
       
{
            item
.@state = false;
            toggleParents
(adg.getParentItem(item), adg, getState (adg, adg.getParentItem(item)));
       
}
   
}

   
private function toggleChildren (item:Object, adg:AdvancedDataGrid, state:String):void
   
{

       
if (item == null)
       
{
           
return;
       
}
       
else
       
{
           
//item.@state = state;
           
var adgCollection:IHierarchicalCollectionView = adg.dataProvider as IHierarchicalCollectionView;
           
var adgHD:IHierarchicalData = adgCollection.source;
           
if (adgHD.hasChildren(item))
           
{
               
var children:ICollectionView = adgCollection.getChildren (item);
               
var cursor:IViewCursor = children.createCursor();
               
while (!cursor.afterLast)
               
{
                    toggleChildren
(cursor.current, adg, state);
                    cursor
.moveNext();
               
}
           
}
       
}
   
}

   
private function getState(adg:AdvancedDataGrid, parent:Object):String
   
{
       
var noChecks:int = 0;
       
var noCats:int = 0;
       
var noUnChecks:int = 0;
       
if (parent != null)
       
{
           
var adgCollection:IHierarchicalCollectionView = adg.dataProvider as IHierarchicalCollectionView;
           
var cursor:IViewCursor = adgCollection.getChildren(parent).createCursor();
       
}
       
if ((noChecks > 0 && noUnChecks > 0) || (noCats > 0))
       
{
           
return STATE_SCHRODINGER;
       
}
       
else if (noChecks > 0)
       
{
           
return STATE_CHECKED;
       
}
       
else
       
{
           
return STATE_UNCHECKED;
       
}
   
}
   
private function imageToggleHandler(event:MouseEvent):void
   
{
        myCheckBox
.selected = !myCheckBox.selected;
        checkBoxToggleHandler
(event);
   
}
   
var selectArr:Array = new Array();
   
private function checkBoxToggleHandler(event:MouseEvent):void
   
{

       
if (data)
       
{
           
var myListData:AdvancedDataGridListData = AdvancedDataGridListData(this.listData);
           
var selectedNode:Object = myListData.item;

           
var adg:AdvancedDataGrid = AdvancedDataGrid(myListData.owner);

           
var toggle:Boolean = myCheckBox.selected;
           
if (toggle)
           
{
                toggleChildren
(data, adg, STATE_CHECKED);
           
}
           
else
           
{
                toggleChildren
(data, adg, STATE_UNCHECKED);
           
}
           
var parent:Object = adg.getParentItem (data);
            toggleParents
(parent, adg, getState (adg, parent));
           
//Alert.show(selectArr.toString()+"\t\t"+selectArr.length+"\t\t"+selectArr[0].length+"\t\t"+adg.selectedIndices.length);
       
}
   
}

   
override protected function createChildren():void
   
{
       
super.createChildren();
        myCheckBox
= new CheckBox();
        myCheckBox
.setStyle( "verticalAlign", "middle" );
        myCheckBox
.addEventListener( MouseEvent.CLICK, checkBoxToggleHandler );
        addChild
(myCheckBox);

   
}  

   
private function setCheckState (checkBox:CheckBox, value:Object, state:Boolean):void
   
{
       
if (state == STATE_CHECKED)
       
{
            checkBox
.selected = state;
       
}
       
else if (state == STATE_UNCHECKED)
       
{
            checkBox
.selected = false;
       
}
       
else if (state == STATE_SCHRODINGER)
       
{
            checkBox
.selected = false;
       
}
        checkBox
.selected = state;
   
}     
   
override public function set data(value:Object):void
   
{
       
super.data = value;

       
var myListData:AdvancedDataGridListData = AdvancedDataGridListData(this.listData);
       
//var adg:AdvancedDataGrid = AdvancedDataGrid(myListData.owner);

       
var selectedNode:Object = myListData.item;

        myCheckBox
.selected =  AdvancedDataGridListData(super.listData).item.show;
       
//var adg:AdvancedDataGrid = AdvancedDataGrid(myListData.owner);

       
//setCheckState (myCheckBox, value, Boolean(value.state));

   
}

  
override protected function commitProperties():void
  
{
      
super.commitProperties();
      
var dg:AdvancedDataGrid = AdvancedDataGrid(listData.owner);

      
var column:AdvancedDataGridColumn =
           dg
.columns[listData.columnIndex];      
       label
.wordWrap = dg.columnWordWrap(column);
  
}

   
/**
     *  @private
     */

   
override protected function measure():void
   
{
       
super.measure();

       
var w:Number = data ? AdvancedDataGridListData(listData).indent : 0;

       
if (disclosureIcon)
            w
+= disclosureIcon.width;

       
if (icon)
            w
+= icon.measuredWidth;

       
if (myCheckBox)
            w
+= myCheckBox.measuredWidth;

       
// guarantee that label width isn't zero because it messes up ability to measure
       
if (label.width < 4 || label.height < 4)
       
{
            label
.width = 4;
            label
.height = 16;
       
}

       
if (isNaN(explicitWidth))
       
{
            w
+= label.getExplicitOrMeasuredWidth();   
            measuredWidth
= w;
       
}
       
else
       
{
            label
.width = Math.max(explicitWidth - w, 4);
       
}

        measuredHeight
= label.getExplicitOrMeasuredHeight();
       
if (icon && icon.measuredHeight > measuredHeight)
            measuredHeight
= icon.measuredHeight;
       
if (myCheckBox && myCheckBox.measuredHeight > measuredHeight)
            measuredHeight
= myCheckBox.measuredHeight;
   
}


  
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
  
{
       
super.updateDisplayList(unscaledWidth, unscaledHeight);
       
if(super.data)
       
{
           
if (super.icon != null)
           
{
                myCheckBox
.x = super.icon.x;
                myCheckBox
.y = (unscaledHeight - myCheckBox.height) / 2;
               
super.icon.x = myCheckBox.x + myCheckBox.width + 17;
               
if (icon.x + icon.width > unscaledWidth)
                    icon
.setActualSize(0, unscaledHeight);
               
super.label.x = super.icon.x + super.icon.width + 3;
               
super.label.setActualSize(Math.max(unscaledWidth - super.label.x, 4), unscaledHeight);
           
}
           
else
           
{
                myCheckBox
.x = super.label.x;
                myCheckBox
.y = (unscaledHeight - myCheckBox.height) / 2;
               
super.label.x = myCheckBox.x + myCheckBox.width + 17;
               
super.label.setActualSize(Math.max(unscaledWidth - super.label.x, 4), unscaledHeight);
           
}
           
if (myCheckBox.x + myCheckBox.width > unscaledWidth)
                myCheckBox
.visible = false;
       
}

        trace
(label.width);
   
}
}
}

 

The above is my GroupItemrenderer.

 
Replies
  • Currently Being Moderated
    Aug 10, 2013 9:16 PM   in reply to Rohini_1327

    Checkbox selection value should be updated on the underlyng data object. In the override set data method, update the selection based on the value from the data object

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points