2011-05-09 28 views
3

我有一个List ItemRenderer。当我点击一个按钮时,绑定到列表的ArrayCollection I的一个属性被改变。ItemRender数据变化

当我点击按钮,它确实会改变属性,但列表不会改变。

我该如何解决这个问题。

这里是我的代码

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

谢谢!

文森特

+0

请显示您的项目渲染器。此外,ControllerCollection的外观如何? – 2011-05-09 14:53:29

+0

感谢您的回复。 ControllerCollection实际上只是一个ArrayCollection。在itemrender中没有什么特别的。我将添加代码。 – Vinzcent 2011-05-09 15:15:18

回答

1

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

我相信你的问题在于这样的陈述:(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> 

这应该这样做。但未经测试。

+0

非常感谢。这个filterFunction以我想要的方式工作。 – Vinzcent 2011-05-10 07:25:33

2

两种方式:

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

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


更新: 我注意到,您的代码设置修改

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

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

我想一定要指定这个集合的第一个元素未必是ArrayCollection中的第一个元素视图中当前可见的第一个元素。我想知道这是否会引起你的问题。

+0

感谢您的回复,但方法无效。没有改变。我已将您的代码添加到上面的代码中。 – Vinzcent 2011-05-09 15:16:04

+0

然后告诉我们更多关于你的ControllerCollection类。用ArrayCollection这些应该工作。 – JeffryHouser 2011-05-09 16:05:28

+0

@Vinzcent我稍微更新了我的答案;但我只是猜测可能是什么问题。您必须向我们展示itemRenderer和您的自定义集合类,以便我们可以确定地知道。 – JeffryHouser 2011-05-09 19:39:36

1

试试这个:

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

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

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

如果我的假设是错误的,我们需要看到代码给你任何进一步的想法。