根据源更新刷新 HiearachicalData?

发布于 2024-10-29 13:47:08 字数 763 浏览 0 评论 0原文

我有一个 ArrayCollection,我们称之为“Items”。它本质上是分层数据的平面集合(每个项目都有 ParentChildren 属性)。我想要一个 AdvancedDataGrid 以分层形式显示数据,所以本质上我可以这样做,它会显示得很好:

// 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;

这有效,但我希望能够在 myDataGrid< 中看到更新/code> 当 Items 集合更新时(因为 RootItems 不会获取任何子项的更新,只会获取顶级任务的更新)。有什么简单的方法可以做到这一点吗?我猜我必须创建一个扩展 HierarchicalData 的类,并在 Items 更改时以某种方式发出警报,但这听起来会非常慢。预先感谢您提供的任何帮助!

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 RootItems 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

极致的悲 2024-11-05 13:47:08

您有两种选择来解决这个问题。您可以创建自己的 IHierarchicalData 实现(它不必扩展 HierarchicalData,在这种特殊情况下,不会有太多可以重用的代码),或者您进行更改稍微处理数据的方式,使其适合标准用例:

[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);

然后您可以在 ADG 中使用 hcv 作为 dataProvider 并使用其方法来添加和删除项目。每当您添加、删除或更新项目时,ADG 都会刷新。

我建议您按照标准方式进行操作,除非那确实不可能。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文