开发者

Refreshing HiearachicalData based on Source Updates?

开发者 https://www.devze.com 2023-02-22 12:37 出处:网络
I have an ArrayCollection we\'ll call \"Items\". It is essentially a flat collection of hierarchical data (each item has Parent and Children properties). I want an AdvancedDataGrid to display the data

I have an ArrayCollection we'll call "Items". It is essentially a flat collection of hierarchical data (each item has Parent and Children properties). I want an AdvancedDataGrid to display the data in hierarchical form, so essentially I could just do this and it would display fine:

// Note: RootItems would be an ArrayCollection that is updated so only the
// top level items are within (item.Parent == null).
var hd:HierarchicalData = new HierarchicalData(model.RootItems);
var hcv:HierarchicalCollectionView = new HierarchicalCollectionView(hd);

myDataGrid.dataProvider = hdc;

This works, but I want to be able to see updates in myDataGrid when the Items collection is updated (since 开发者_如何学GoRootItems won't pick up updates to any children, only the top level tasks). Is there any easy way to do this? I'm guessing I'll have to create a class that extends HierarchicalData and somehow alert it when Items changes, but that sounds like it'll be pretty slow. Thanks in advance for any help you can provide!


You have two options to solve this problem. Either you create your own implementation of IHierarchicalData (it doesn't have to extend HierarchicalData and in this particular case there won't be much code you can reuse) or you change the way you handle your data a little bit so that it fits into the standard use case:

[Bindable] // make it bindable so that the HierarchicalCollectionView gets notified when the object changes
class Foo // your data class
{
    // this constructor is needed to easily create the rootItems below
    public function Foo(children:ArrayCollection = null)
    {
        this.children = children;
    }

    // use an ArrayCollection which dispatches an event if one of its children changes
    public var children:ArrayCollection;

    // all your other fields
}

// Create your rootItems like this. Each object can contain a collection of children
// where each of those can contain children of its own and so forth...
var rootItems:ArrayCollection = new ArrayCollection([
    new Foo(
        new ArrayCollection([
            new Foo(),
            new Foo(),
            new Foo(
                new ArrayCollection([
                    // ...
                ]),
            new Foo()
        ])
    ),
    new Foo(
        // ...
    ),
    // ...
]);

// Create the HierarchicalData and HierachicalCollectionView
var hd:IHierarchicalData = new HierarchicalData(rootItems);

[Bindable]
var hcv:IHierarchicalCollectionView = new HierarchicalCollectionView(hd);

Then you can use hcv as dataProvider in your ADG and use its methods to add and remove items. The ADG will refresh whenever you add, remove or update an item.

I suggest you do it the standard way unless that's really not possible.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号