2011-12-19 41 views
0

我在我的flex项目中有一个组件A,人们可以投票给他们最喜欢的艺术家。有人“喜欢”一位艺术家,它会在我的数据库中更新。还有一个组件B显示了最受欢迎的艺术家的图表。我在Flex中调用creationcomplete数据库中的数据。问题是,当一个人首先对艺术家投票时,它将不会在数据网格中可见,直到用户重新加载主页面(?)或重新打开整个应用程序。所以我想知道如何每次有人添加投票到我的数据库我可以更新datagrid。我现在正在使用更新按钮,但这当然不是很友好。欢迎任何帮助!Flex:更新datagrid每次添加一个值(来自另一个组件)

回答

1

使用一个ArrayCollection,你只要把它绑定所有的方式通过所有部件

<fx:Script> 
    <![CDATA[ 
     import mx.collections.ArrayCollection; 
     import mx.events.FlexEvent; 

     [Bindable] 
     public var artistData:ArrayCollection = new ArrayCollection([ 
      {artistName : "Paula", artistLikes : "0"}, 
      {artistName : "Bob", artistLikes : "0"}, 
      {artistName : "Arthur", artistLikes : "0"} 
     ]); 

    ]]> 
</fx:Script> 

<s:layout> 
    <s:VerticalLayout/> 
</s:layout> 

<s:DataGrid id="artists" dataProvider="{artistData}" width="100%" height="150"> 
    <s:columns> 
     <s:ArrayList> 
      <s:GridColumn dataField="artistName" headerText="Artist Name"/> 
      <s:GridColumn dataField="artistLikes" headerText="Artist Likes"/> 
     </s:ArrayList> 
    </s:columns> 
</s:DataGrid> 

<components:CompA artistDataInCompA="{artistData}" height="100"/> 

<components:CompB artistDataInCompB="{artistData}" height="100"/> 

假设你在com中增加喜欢P A(不要忘记调用刷新()上的ArrayCollection)

<fx:Script> 
    <![CDATA[ 
     import mx.collections.ArrayCollection; 

     [Bindable] 
     public var artistDataInCompA:ArrayCollection; 

     protected function button_clickHandler(name:String):void 
     { 
      var index:Number = getItemIndexByProperty(artistDataInCompA, "artistName", name); 
      var numLikes:Number = artistDataInCompA[index].artistLikes; 
      // Here you update your PHP 
      // HTTPService... 

      // and the ArrayCollection that is binded all the way through all components 
      artistDataInCompA[index].artistLikes = numLikes + 1; 
      artistDataInCompA.refresh(); 
     } 

     protected function getItemIndexByProperty(array:ArrayCollection, property:String, value:String):Number 
     { 
      for (var i:Number = 0; i < array.length; i++) 
      { 
       var obj:Object = Object(array[i]) 
       if (obj[property] == value) 
        return i; 
      } 
      return -1; 
     } 

    ]]> 
</fx:Script> 

<s:layout> 
    <s:VerticalLayout/> 
</s:layout> 

<s:Button label="Add a like to Paula" click="button_clickHandler('Paula')"/> 

<s:Button label="Add a like to Bob" click="button_clickHandler('Bob')"/> 

<s:Button label="Add a like to Arthur" click="button_clickHandler('Arthur')"/> 

现在,你有你的份B做什么

<fx:Script> 
    <![CDATA[ 
     import mx.collections.ArrayCollection; 

     [Bindable] 
     public var artistDataInCompB:ArrayCollection; 

    ]]> 
</fx:Script> 

<s:layout> 
    <s:VerticalLayout/> 
</s:layout> 

<s:Label text="Paula has {artistDataInCompB.getItemAt(0).artistLikes} like(s)"/> 

<s:Label text="Bob has {artistDataInCompB.getItemAt(1).artistLikes} like(s)"/> 

<s:Label text="Arthur has {artistDataInCompB.getItemAt(2).artistLikes} like(s)"/> 

+0

Wondefull!谢谢:) – 2011-12-19 13:33:32

+0

+1解决问题的办法....但你必须允许askers工作他们自我和理解。我认为只是提供逻辑就足够了:) – 2011-12-21 05:46:06

0

如果您想要缓慢响应,您应该使用服务器端脚本系统(如PHP)为您的操作脚本代码添加通信功能。如果您想要实时响应,则应使用套接字服务或Web服务。

1

每次投票时,在发送命令添加一个投票后,将这些行添加到函数中。

public function voteUpFunction():void 
{ 
    // Your Code to Vote up <Enter Code here> 
    YourDataGridId.dataprovider = null; 
    //call new listofLikedPersons using conn.call <Enter Code here> 
    YourDataGridId.dataprovider = listOfLikedPersons; 
} 

希望这会有所帮助,告诉我,如果它解决了问题

0

它是一个简单的,你只需要维护一个计时器变量,并在某个时间让1秒钟,你只需要刷新datagrid的dataprovider。

祝您有愉快的一天。

相关问题