开发者

ItemRender data change

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 m开发者_运维技巧y 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


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.


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.


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.


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>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜