Extracting the source from an IHierarchicalCollectionView

When you set Object/Collection as a source for an AdvancedDataGrid to display the source in a Hierarchical manner, the AdvancedDataGrid.dataProvider will return an instance of IHierarchicalCollectionView because internally the source is used to construct a HierarchicalCollectionView which is returned via the dataProvider property.

What if, you want the original source from this IHierarchicalCollectionView. Well, there are some API’s for achieving this –


// First, get the HierarchicalData used to create the HierarchicalCollection
var hd:IHierarchicalData = IHierarchicalCollectionView(adg.dataProvider).source;


//From the HierarchicalData, get the source collection/object
var source:Object = hd.getRoot();

Here is a sample.
Source here.

16 thoughts on “Extracting the source from an IHierarchicalCollectionView

  1. I subclassed HierarchicalData so that it will dispatch a CollectionEvent when its source changes, as recommended here: http://bugs.adobe.com/jira/browse/FLEXDMV-1334. I am using an instance of this custom HierarchicalData class as the dataProvider for an ADG, and the source of the HierarchicalData is an ArrayCollection. I am dynamically modifying the ArrayCollection, and the HierarchicalData is dispatching the CollectionEvent as expected. If I debug, I can see the new values in the ArrayCollection. However, I can’t get the ADG to reflect the changes. I have tried refreshing the ArrayCollection, refreshing the HierarchicalData, and explicity re-setting the dataProvider for the ADG, but to no avail. I have a feeling that I need to call something on the ADG for it to reflect the changes, like validateNow(). Any ideas? Many thanks for any suggestions.

    • HierarchicalData does not dispatch data binding events when source is changed,How do you deal with?can you tell me? thanks a lot !

  2. You can call IHierarchicalCollectionView(adg.dataProvider).refresh();
    If the issue still exists, kindly send a sample in which you are facing this issue.

  3. Thank you for the suggestion, Sameer. Unfortunately this doesn’t work either.

    Let me provide more detail. I am populating an ArrayCollection of agency objects and setting this as the source for the HierarchicalData. In order to not have to make an API call to get a list of advertisers for each agency up front, I am sticking an empty object in each agency’s children:Array property so that the expand arrow appears next to the agency in the ADG. In the handler for the ADG’s itemOpening event, I am making the API call to get the advertisers for the agency that was expanded. In the result handler for the API call, I loop through the ArrayCollection and add the array of advertisers to the children property of the appropriate agency, replacing the empty placeholder object. If I debug, the advertisers are added to the agency’s children in the ArrayCollection. However the changes are never reflected in the ADG. The problem seems to be related to doing this on itemOpening. I am grateful for any insight.

    If you send me your email I can provide an example to demonstrate this.

    Warm Regards,
    Bryan

  4. I have answered my own question, but I will post in case anyone else has this issue. When I attempted to modify the object directly in the ArrayCollection directly, and then refreshed the IHierarchicalCollectionView, the ADG didn’t update. If I modified the node through the IHierarchicalCollectionView (using the IHierarchicalCollectionView’s getChildren() addChildAt() and removeChildAt() methods), the ADG was updated.

  5. Great.
    I can explain the reason –
    The ArrayCollection that you have consists of Objects.
    And Objects are not Bindable. So, whenever you change a property in an Object, no change event is triggered and no one is notified.
    This is the reason why nothing is getting updated.
    Using the API will make sure that all the things goes well.

  6. I am using Advanced Datagrid. I am using three buttons. Add Task,AddSubTask,Delete Task.I am able to Add and Delete Task. I want to know how is it possible to add subtasks like tree structure.That means selecting a Task and adding Subtasks for that selected Task. For example, if i have added three tasks say Task1,Task2,Task3. If i select Task2 in the datagrid and then click the Add Sub Task button. If iam adding 2 subtasks for Task2, it should look like this(Like a tree structure)

    Task1
    Task2
    Sub Task1
    Sub Task2
    Task3

    Iam attaching the code also.

    Please help me

    User1
    User2
    User3
    User4

    High
    Medium
    Low
    Urgent
    None

    Assigned
    Not Assigned
    Not Started

    0 )
    {

    taskCol.removeItemAt(TaskRole1Grid.selectedIndex);

    }
    }
    ]]>

  7. User1
    User2
    User3
    User4

    High
    Medium
    Low
    Urgent
    None

    Assigned
    Not Assigned
    Not Started

    0 )
    {

    taskCol.removeItemAt(TaskRole1Grid.selectedIndex);

    }
    }
    ]]>

  8. xml version=”1.0″ encoding=”utf-8″?>

    User1
    User2
    User3
    User4

    High
    Medium
    Low
    Urgent
    None

    Assigned
    Not Assigned
    Not Started

    0 )
    {

    taskCol.removeItemAt(TaskRole1Grid.selectedIndex);

    }
    }
    ]]>

  9. Set HierarchicalData as the dataProvider to the grid and use –
    IHierarchicalCollectionView.addChild(parent:Object, newChild:Object)

    For example,
    (adg.dataProvider as IHierarchicalCollectionView).addChild(adg.selectedItem, newChild);

  10. Hi Sameer,

    Nice post. I have a rather complex doubt related to the above article. I am creating an application where i have to dynamically group a column in advancedDataGrid.I have extended AdavncedDataGridRenderer to provide a drop-down menu kind of functionality which has one of the options as Dynamic Grouping.Let me explain my problem with the portion of the code in my MenuHeaderRenderer class where i handle the Dynamic Grouping :

    var groupingCollection:GroupingCollection = new GroupingCollection();
    var grouping:Grouping = new Grouping();
    var groupingField:GroupingField = new GroupingField(gridColumn.dataField);
    grouping.fields = new Array();
    groupingCollection.source = ICollectionView(grid.dataProvider);–(1)–
    grouping.fields.push(groupingField);
    groupingCollection.grouping = grouping;
    groupingCollection.refresh();
    grid.dataProvider = groupingCollection; –(2)–

    My question : Is there a way of converting Hierarchical data to an ArrayCollection (flat data).The reason why i ask this is because initially my grid.dataProvider is an ArrayCollection but if i apply dynamic grouping for one column then the dataProvider becomes Hierarchical data in line 2. Because of this next time i apply dynamic grouping on any column then it gives error at –(1)–. I hope you have understood my problem.

    Any help in this regard would be highly appreciated.

    Thanks,
    Shishir.

  11. You can add a check at (1) above to see if you have done grouping or not. That is, the dataProvider is already hierarchical or not.
    if (grid.dataProvider is IHierarchicalCollectionView)
    {
    var hd:IHierarchicalData = IHierarchicalCollectionView(grid.dataProvider).source;
    groupingCollection.source = hd.getRoot();
    }
    else
    {
    groupingCollection.source = ICollectionView(grid.dataProvider);
    }

  12. Hi Sameer,

    Thanks a lot for your reply.I implemented the above solution but unfortunately it didn’t work for me.The reason is as follows :

    I basically want to have the initial flat arrayCollection as my dataProvider every time i dynamically group on some column.But in this case after i have grouped once on a column the next time the dataProvider has a hierarchical structure and thus if i try to dynamically group by any other column,it throws an error and shows “Not Available” as the grouped entity.I basically wanted to retain the original flat ArrayCollection so that each time i want to dynamically group on any column i would be grouping with a flat ArrayCollection as the dataProvider.

    I actually solved the problem a couple of days back,though with a pretty naive method.I am using a static variable inside my HeaderRenderer code.The first time when someone groups on a column i take the backup of my initial flat ArrayCollection and i use this backup up flat source to intialise my grid’s dataProvider evertyime before it groups on the chosen column.

    I would appreciate if you can tell me a better solution for the above problem.

    Thanks a lot,
    Shishir.

  13. I am using a advanced datagrid, there I am displaying hierarchical grouped data. When I sort my datagrid for the first time it sort it properly but after that it doesn’t sort the datagrid.

    Thanks,
    Lakhan

Leave a reply to Sameer Cancel reply