【问题标题】:Flex: update datagrid everytime a value is added (from another compononent)Flex:每次添加值时更新数据网格(来自另一个组件)
【发布时间】:2011-12-19 01:14:55
【问题描述】:

我的 flex 项目中有一个组件 A,人们可以在其中为他们最喜欢的艺术家投票。有人“喜欢”一个艺术家,它会在我的数据库中更新。还有一个组件 B 显示了最喜欢的艺术家的图表。我在 Flex 中将数据库中的数据称为创建完成。问题是当一个人第一次对艺术家投票时,在用户重新加载主页(?)或重新打开整个应用程序之前,它不会在数据网格中可见。所以我想知道每次有人向我的数据库添加投票时我如何更新数据网格。我现在正在使用更新按钮,但这当然不是很用户友好。欢迎任何帮助!

【问题讨论】:

    标签: actionscript-3 apache-flex


    【解决方案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"/>
    

    假设您在 comp A 中增加了您的喜欢(不要忘记在 arrayCollection 上调用 refresh())

    <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')"/>
    

    现在做你在comp 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)"/>
    

    【讨论】:

    • +1 用于解决问题....但您必须允许提问者自行处理并理解。我认为只提供逻辑就足够了:)
    【解决方案2】:

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

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

    希望对你有帮助,告诉我它是否解决了问题

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        它很简单,您只需要维护一个计时器变量,并且在某个时间让我们花 1 秒时间,您只需要刷新 datagrid 的数据提供者。

        祝你有美好的一天。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-09-22
          • 2017-06-01
          • 2023-03-21
          • 2013-01-01
          • 2016-12-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多