ItemRender 数据更改

发布于 2024-11-06 10:54:50 字数 1087 浏览 1 评论 0原文

我有一个带有 ItemRenderer 的列表。当我单击按钮时,绑定到列表的 ArrayCollection 的属性就会更改。

当我单击该按钮时,它确实会更改属性,但列表不会更改。

我该如何解决这个问题。

这是我的代码

<fx:Script>
    <![CDATA[

        [Bindable] 
        public var controllers:ControllerCollection = new ControllerCollection();

        private function hideTheFirstItem(evt:MouseEvent):void
        {
            (controllers[0] as Controller).meetsRequirements = false;

                    //these methods don't work unfortunatly
                    controllers.itemUpdated(controllers[0]);
                    controllers.refresh();

        }


    ]]>
</fx:Script>

<mx:List id="listControllers" dataProvider="{controllers}">
    <mx:itemRenderer>
        <fx:Component>
            <solutionItems:displaySolutionItem visible="{data.meetsRequirements}" />
        </fx:Component>
    </mx:itemRenderer>
</mx:List>
<mx:Button label="test" click="hideTheFirstItem(event)" />

(ControllerCollection 扩展了 ArrayCollection)

谢谢!

文森特

I have a List with an ItemRenderer. When I click a button, then a property of the ArrayCollection I bound to the list is changed.

When I click the button, then it does change the property, but the list doesn't change.

How do I solve this.

Here's my code

<fx:Script>
    <![CDATA[

        [Bindable] 
        public var controllers:ControllerCollection = new ControllerCollection();

        private function hideTheFirstItem(evt:MouseEvent):void
        {
            (controllers[0] as Controller).meetsRequirements = false;

                    //these methods don't work unfortunatly
                    controllers.itemUpdated(controllers[0]);
                    controllers.refresh();

        }


    ]]>
</fx:Script>

<mx:List id="listControllers" dataProvider="{controllers}">
    <mx:itemRenderer>
        <fx:Component>
            <solutionItems:displaySolutionItem visible="{data.meetsRequirements}" />
        </fx:Component>
    </mx:itemRenderer>
</mx:List>
<mx:Button label="test" click="hideTheFirstItem(event)" />

(ControllerCollection extends ArrayCollection)

Thanks!

Vincent

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

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

发布评论

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

评论(4

娇女薄笑 2024-11-13 10:54:50

两种方式:

  1. 集合。刷新()
  2. collection.itemUpdated()

当然,ControllerCollection 不是一个标准的 Flex Collection 类;所以我只是假设它实现了 ICollectionView 接口。


更新:
我确实注意到您的代码设置为修改 ArrayCollection 的第一个元素,

private function hideTheFirstItem(evt:MouseEvent):void
{
 (controllers[0] as Controller).meetsRequirements = false;

 //these methods don't work unfortunatly
 controllers.itemUpdated(controllers[0]);
 controllers.refresh();
}

我想确保指定集合的​​第一个元素可能不是视图中当前可见的第一个元素。我想知道这是否给你带来了问题。

Two ways:

  1. collection.refresh()
  2. collection.itemUpdated()

Of course, ControllerCollection is not a standard Flex Collection class; so I am just assuming that it implements the ICollectionView interface.


Update:
I do notice that your code is set to modify the first element of the ArrayCollection

private function hideTheFirstItem(evt:MouseEvent):void
{
 (controllers[0] as Controller).meetsRequirements = false;

 //these methods don't work unfortunatly
 controllers.itemUpdated(controllers[0]);
 controllers.refresh();
}

I wanted to be sure to specify that the first element of the collection may not be the first element currently visible in the view. I wonder if that is causing you issues.

泅人 2024-11-13 10:54:50

在没有看到您的项目渲染器的情况下,我需要做出一些假设。

首先,我假设您的项目渲染器使用数据绑定到 meetsRequirements 属性。如果是这种情况,则 meetsRequirements 属性需要在该属性发生更改时发出通知。如果将 [Bindable] 标记添加到该属性或 Controller 类,则 meetsRequirements 属性将通知 itemRenderer 根据该字段更新该字段关于您的数据绑定。

如果我的假设是错误的,我们需要查看代码以便为您提供进一步的想法。

Without seeing your item renderer, I need to make some assumptions.

First, I will assume that your item renderer is using data binding to the meetsRequirements property. If that is the case, then the meetsRequirements property needs to notify when that property changes. If you add the [Bindable] tag to that property or the Controller class, then the meetsRequirements property will notify the itemRenderer to update that field based on your data binding.

If my assumptions are wrong, we need to see the code to give you any further thoughts.

画▽骨i 2024-11-13 10:54:50

首先,如果不需要,请不要尝试创建新集合。

我相信您的问题在于以下语句: (controllers[0] as Controller).meetsRequirements = false; 编译时应该失败,因为无法使用方括号注释检索集合项。您需要使用 getItemAt(index:int) 函数。

此外,如果您想“删除”项目渲染器,您不会希望将其可见设置为 false,因为这样您就会有一个空位。你想要做的就是将其过滤掉:

<s:Application creationComplete="onCreationComplete()">
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;

            [Bindable] public var data:ArrayCollection = new ArrayCollection();

            private function onCreationComplete():void
            {
                // TODO: need to add data here
                // Adding filter function
                data.filterFunction = filterItems;
            }

            private function filterItems(item:Object):Boolean
            {
                return item.meetsRequirements;
            }

            private function hideFirstItem():void
            {
                if(data.length > 0)
                {
                    Controller(data.getItemAt(0)).meetsRequirements = false;
                }

                data.refresh();
            }
        ]]>
    </fx:Script>

    <mx:List id="listControllers" dataProvider="{data}" />
    <mx:Button label="test" click="hideFirstItem()" />
</s:Application>

这应该可以做到。虽然未经测试。

First, don't try to create new collections if you don't need to.

I believe your problem lies with this statement: (controllers[0] as Controller).meetsRequirements = false; which should fail on compile because a collection item cannot be retrieved using the square bracket annotation. You need to use getItemAt(index:int) function.

Furthermore, you wouldn't want to set visible to false to an item renderer if you want to 'remove' it because then you'd have an empty spot. What you want to do is filter it out:

<s:Application creationComplete="onCreationComplete()">
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;

            [Bindable] public var data:ArrayCollection = new ArrayCollection();

            private function onCreationComplete():void
            {
                // TODO: need to add data here
                // Adding filter function
                data.filterFunction = filterItems;
            }

            private function filterItems(item:Object):Boolean
            {
                return item.meetsRequirements;
            }

            private function hideFirstItem():void
            {
                if(data.length > 0)
                {
                    Controller(data.getItemAt(0)).meetsRequirements = false;
                }

                data.refresh();
            }
        ]]>
    </fx:Script>

    <mx:List id="listControllers" dataProvider="{data}" />
    <mx:Button label="test" click="hideFirstItem()" />
</s:Application>

This should do it. Untested though.

失眠症患者 2024-11-13 10:54:50

试试这个:

<fx:Script>
    <![CDATA[

        [Bindable(Event="refreshMyList")] 
        public var controllers:ControllerCollection = new ControllerCollection();

        private function hideTheFirstItem(evt:MouseEvent):void
        {
            (controllers[0] as Controller).meetsRequirements = false;

            dispatchEvent(new Event("refreshMyList"));
        }


    ]]>
</fx:Script>

Try this:

<fx:Script>
    <![CDATA[

        [Bindable(Event="refreshMyList")] 
        public var controllers:ControllerCollection = new ControllerCollection();

        private function hideTheFirstItem(evt:MouseEvent):void
        {
            (controllers[0] as Controller).meetsRequirements = false;

            dispatchEvent(new Event("refreshMyList"));
        }


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