【问题标题】:Data Grid not displaying data in array collection数据网格未在数组集合中显示数据
【发布时间】:2010-10-07 16:06:08
【问题描述】:

我的数据网格显示的是陈旧数据,而不是其数据提供程序(数组集合)中可用的实时数据。我尝试刷新集合中的数据,但这没有效果。下面是我的代码,有没有人看到可能是什么问题?

<mx:Accordion/>
<fx:Script>
<![CDATA[
private var _gridData:ArrayCollecton = new ArrayCollection;
[Bindable] public function get gridData():ArrayCollection { return _gridData; }
public function set gridData(value:ArrayCollection):void { _gridData = value; }
public function loadGridData():void {

// imgCollection is the data returned from the server
var tempCollection:ArrayCollection = new ArrayCollection();
 for (var i:int = 0; i < imgCollection.length; i++)
            {
                var img:Object = new Object();
                img.id = imgCollection.getItemAt(i).imgId;
                img.url = "http://..." + imgCollection.getItemAt(i).imgId;
                img.caption = (imgCollection.getItemAt(i).imgCaption == null) ? "": imgCollection.getItemAt(i).imgCaption;
                img.group = images;
                tempCollection.addItem(new ObjectProxy(img));
            }
gridData = tempCollection;

<!-- Use http service to get data and save it in grid data array collection, this is run on accordion create completion and whenever data is added or removed from the array collection -->
}
]]>
</fx:Script>
<!-- NOTE: There is a cyclic binding between the data grid and the gridData array collection -->
<fx:Binding source="dg.dataProvider as ArrayCollection" destination="gridData"/>
...
...
<s:NavigatorContent>
<s:Panel>
<mx:DataGrid dataProvider="{gridData}" ...>
...
...
</mx:DataGrid>
</s:Panel>
</s:NavigatorContent>

更新: 我尝试了下面提到的建议,但是,它们并没有解决问题。数据网格有自定义项渲染器,这可能是问题吗?

<?xml version="1.0" encoding="utf-8"?>
<s:MXDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                          xmlns:s="library://ns.adobe.com/flex/spark" 
                          xmlns:mx="library://ns.adobe.com/flex/mx" 
                          focusEnabled="true">
    <mx:Image id="image" source="{data.url}" height="65" maintainAspectRatio="true" scaleContent="true"/>
</s:MXDataGridItemRenderer>

【问题讨论】:

  • 您是否尝试在提供程序更新时将 dataProvider 重新绑定到 DataGrid?
  • 我会摆脱绑定并直接设置 DP,它们似乎是不必要的,而且几乎肯定会导致问题。
  • 好吧,我有(循环)绑定,因为我在网格中有一个可编辑的列。如果我删除绑定,我将不得不手动考虑数组集合和数据网格中的更改,对吗?

标签: apache-flex actionscript-3


【解决方案1】:

您不需要“循环”绑定,因为在您的数据网格中您不会更改集合,而是更改其项目。收藏品完好无损。 DataGrid 的 dataprovider 和您的 _gridData 指向同一个集合。

【讨论】:

    【解决方案2】:

    如果我没记错的话,你应该在 setter 上也有 [Bindable],因为数据网格没有其他方法可以知道你的数据何时发生了变化。

    问候,阿林

    【讨论】:

    • 我很确定这会导致编译错误。你只需要一次。
    • 将可绑定的元标记放在 setter 上被认为是多余的。但是,为此我尝试使用 "dg.invalidateProperties()" & "dg.invalidateDisplayList()";都没有用。
    【解决方案3】:

    在我看来你想多了。由于您没有在 getter/setter 中执行任何操作,因此您可以摆脱它们并将 ArrayCollection 标记为 Bindable,然后将其设置为 DataGrid 的 dataProvider 并完成:

    <fx:Script>
        <![CDATA[
            [Bindable]
            public var gridData:ArrayCollecton = new ArrayCollection;
    
            public function loadGridData():void { 
                // Whenever you change the gridData, the DataGrid will update appropriately.
            }
        ]]>
    </fx:Script>
    
    <mx:DataGrid dataProvider="{gridData}"></mx:DataGrid>
    

    您现有代码的问题可能是您没有在 setter 中调度更改事件。摆脱 getter/setter 允许 ArrayCollection 为您处理调度该事件。希望对您有所帮助。

    编辑:根据更新后的问题,您可能想尝试将渲染器更改为如下所示,如果您的自定义数据对象不可绑定,这将有所帮助。

    <?xml version="1.0" encoding="utf-8"?>
    <s:MXDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                              xmlns:s="library://ns.adobe.com/flex/spark" 
                              xmlns:mx="library://ns.adobe.com/flex/mx" 
                              focusEnabled="true">
    <fx:Script>
        <![CDATA[
            override public function set data(value:Object):void { 
                super.data = value;
                image.source = value.url;
            }
        ]]>
    </fx:Script>
        <mx:Image id="image" source="{data.url}" height="65" maintainAspectRatio="true" scaleContent="true"/>
    

    【讨论】:

    • 我尝试切换到:[Bindable] public var gridData:ArrayCollection = new ArrayCollection();但这并没有解决问题。我的数据网格有自定义项目渲染器;这可能是问题的原因吗?
    • 可能。您如何在自定义渲染器中设置属性?你重写了 set data 方法吗?
    • 我在自定义渲染器中使用默认的“数据”属性;请参阅上面的更新。
    • 如果您绑定到默认数据属性,并且数据对象本身不可绑定,您将看不到更改。 (我假设您的数据对象是某种自定义对象。)如果是这种情况,您可以将自定义数据对象类标记为可绑定,或者您可以覆盖渲染器上的 set data 方法并手动设置属性您的渲染器基于在您覆盖的 set 数据方法中设置的内容。
    • 为了澄清,数据对象有些自定义。但是,如果您查看“loadGridData”函数,自定义对象是对象代理,然后将其添加到绑定数组集合中。因此,对象不也是绑定的吗?
    猜你喜欢
    • 2013-06-14
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 2019-06-08
    相关资源
    最近更新 更多