Monday, June 20, 2011

GroupingCollection2 not deleting an item while grouped

So here was our issues, when the grid was grouped, an item wouldn't be visually deleted - although it did actually get deleted from the collection and on the server. Everything worked fine when the grid was not grouped.

I discovered that the issue resides in the GroupingCollection2.getChildren() method. This method violates the SRP (Single Responsibility Principle) and not only does it return the children object, but also builds the parentMap.

If you subclass GroupingCollection2, and redirect getChildren to your own method which just returns the children (as the name might suggest). The parentMap fails to get built.

What this means, is the when you delete an item from the source collection, a parent for that item will not be found, and the grouped collection (one that is built internally ) doesn't respond to the delete and therefore the display doesn't need to update.

The solution is in your subclass of GroupingCollection2 to call super.getChildren, even if you don't need use the return results.

public override function getChildren(node:Object):Object // NO PMD
  {
   
   super.getChildren(node); 
   var rtn:Object  = helper.getChildren(node)
   
   /*
   Make sure this return value is not null. 
   A null value will cause the HierarchicalCollectionView to RTE on its internal refresh method
   */
   if (rtn == null)
   {
    rtn = new Object();
   }
   return rtn;
   
  }

2 comments:

  1. Hi,
    I am facing the same issue... Can you please explain how to fix this?

    ReplyDelete
  2. Naveen, here is the class that I created:

    public class GroupingCollection_IHasExpandability extends GroupingCollection2
    {

    private var helper:Helper_IHasExpandability

    public function GroupingCollection_IHasExpandability()
    {
    super();
    helper = new Helper_IHasExpandability();
    }



    public override function canHaveChildren(node:Object):Boolean
    {
    var rtn:Boolean = false;
    if (node is IHasExpandability)
    {
    rtn = helper.canHaveChildren(node);
    }
    else
    {
    rtn = super.canHaveChildren(node);
    }
    return rtn;
    }

    public override function getChildren(node:Object):Object // NO PMD
    {
    super.getChildren(node); // call to super has to happen to setup the parent map, but don't use the results
    var rtn:Object // NO PMD
    if (node is IHasExpandability)
    {
    rtn = helper.getChildren(node)
    }
    else
    {
    rtn = super.getChildren(node);
    }
    /*
    Make sure this return value is not null.
    A null value will cause the HierarchicalCollectionView to RTE on its internal refresh method
    */
    if (rtn == null)
    {
    rtn = new Object();
    }
    return rtn;

    }

    public override function hasChildren(node:Object):Boolean
    {
    var rtn:Boolean = false;
    if (node is IHasExpandability)
    {
    rtn = helper.hasChildren(node);
    }
    else
    {
    rtn = super.hasChildren(node);
    }
    return rtn;
    }

    }

    ReplyDelete