0

我遇到了一个有趣的问题,并认为值得发布:

在我必须在运行时使用分层数据设置数据提供程序的情况下,我正在使用高级数据网格。adobe 文档并没有真正涵盖这种事情(至少不是我可以挖掘到的水平)。

有没有人遇到过这个?

我唯一能想到的是有点骇人听闻,并且似乎通过高级网格的披露图标引入了一些奇怪的行为。我在下面包含了一个测试用例:

advancedDataGridProblem.as:

 import mx.collections.ArrayCollection;

 public var dataProvider:ArrayCollection = new ArrayCollection([{label:"item1"},
             {label:"item2", children:[{label:"subItem1"},{label:"subItem2"},
             {label:"subItem3"}]},
             {label:"item3"}]);

 public function init():void{
      //using callLater to ensure that the grid has been created before setting DP
      this.callLater(setDataProvider,[dataProvider]);
 }

 public function setDataProvider(list:ArrayCollection):void{
      heirData.source = list;
 }

和mxml:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
     <mx:Script source="advancedDataGridProblem.as"/>


  <mx:AdvancedDataGrid id="advDG" width="100%">

   <mx:columns>
        <mx:AdvancedDataGridColumn headerText="What the heck?"/>
        <mx:AdvancedDataGridColumn headerText="Label" dataField="label"/>
        <mx:AdvancedDataGridColumn headerText="Column 2"/>
        <mx:AdvancedDataGridColumn headerText="Column 3"/>
   </mx:columns>

   <mx:dataProvider>
        <mx:HierarchicalData id="heirData"/>
   </mx:dataProvider>
 </mx:AdvancedDataGrid>

</mx:Application>

正如您在运行测试应用程序时所看到的,应该显示在“标签”列中的图标实际上被放置在“What the heck?”中。柱子。如果您将 Label 列拖过来并尝试将其与“What the heck?”交换。列,图标保持在原来的位置,但神奇的是,以前为“标签”列左对齐的标签现在为图标设置了适当的格式。将标签列拖出插槽一将使网格恢复到原始状态。

这个有点在我头上 - 有人有什么建议吗?

理想情况下,我想一起解决这个问题,并在运行时像往常一样分配 dataProvider 。

提前致谢!

我将很快向 adobe 提交一份关于此问题的错误报告,并将链接放入评论中。

4

2 回答 2

0

出色地..

经过一个周末,今天让我的头好好摇晃,眼睛焕然一新,我找到了解决问题中“简单”部分的方法。

直接设置 AdvancedDataGrid 的 dataProvider 比我想象的要简单得多;您只需要将 ArrayCollection 转换为 HierarchicalData。老实说,我以前试过这个,但是拼写错误 Hierarchical .. facepalm

总之,advancedDataGridProblem.as 应该改为:

import mx.collections.ArrayCollection;
import mx.collections.HierarchicalData;

public var dataProvider:ArrayCollection = new ArrayCollection([{label:"item1"},
                                                                {label:"item2", children:[{label:"subItem1"},{label:"subItem2"},
                                                                {label:"subItem3"}]},
                                                                {label:"item3"}]);

public function init():void{
    //using callLater to ensure that the grid has been created before setting DP
    this.callLater(setDataProvider,[dataProvider]);
}

public function setDataProvider(list:ArrayCollection):void{
    advDG.dataProvider = new HierarchicalData(list);
}

mxml中dataProvider的声明也可以去掉:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
 <mx:Script source="advancedDataGridProblem.as"/>


<mx:AdvancedDataGrid id="advDG" width="100%">

 <mx:columns>
    <mx:AdvancedDataGridColumn headerText="What the heck?"/>
    <mx:AdvancedDataGridColumn headerText="Label" dataField="label"/>
    <mx:AdvancedDataGridColumn headerText="Column 2"/>
    <mx:AdvancedDataGridColumn headerText="Column 3"/>
 </mx:columns>
 </mx:AdvancedDataGrid>

</mx:Application>

运行此代码,您仍然可以在错误的列中看到披露图标,但至少删除了在 mxml 中设置网格的 dataProvider 的“hackish”解决方案。这样做还允许正确的验证和网格的显示。设置网格的 dataProvider 允许正确的显示验证,显然设置它的 dataProvider 的分层数据源不会调用那些相同的验证方法,需要用户调整网格大小或程序员以某种方式强制验证(这在我的一生中我不能做)。

我会及时通知您披露图标问题。同时,我希望锁定第一列并将其仅用于这些图标在我的应用程序中会很好。我什至可以把这个“缺陷”变成一个“功能”:P

干杯!

[编辑]

另一种在使用绑定数据时实现更简单且业务逻辑更少的方法是:

advancedDataGridProblem.as:

import mx.collections.ArrayCollection;
import mx.collections.HierarchicalData;

public var dataProvider:ArrayCollection = new ArrayCollection([{label:"item1"},
                                                                {label:"item2", children:[{label:"subItem1"},{label:"subItem2"},
                                                                {label:"subItem3"}]},
                                                                {label:"item3"}]);

mxml:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 <mx:Script source="advancedDataGridProblem.as"/>


<mx:AdvancedDataGrid id="advDG" width="100%">

 <mx:columns>
    <mx:AdvancedDataGridColumn headerText="What the heck?"/>
    <mx:AdvancedDataGridColumn headerText="Label" dataField="label"/>
    <mx:AdvancedDataGridColumn headerText="Column 2"/>
    <mx:AdvancedDataGridColumn headerText="Column 3"/>
 </mx:columns>

 <mx:dataProvider>
     <mx:HierarchicalData source="{dataProvider}"/>
 </mx:dataProvider>

 </mx:AdvancedDataGrid>

</mx:Application>

这种方法避免了在 ActionScript 中设置网格的 dataProvider 后必须手动使网格的列表、显示列表等无效和验证的可能问题。我对使用此方法的一个小保留是,因为这会在 flex 3 中创建单向绑定,所以我不确定是否能够使用 HierarchicalData 对象的方法来更改绑定的 ArrayCollection。也就是说,我没有在 HierarchicalData 或 AdvancedDataGrid 的源代码中四处寻找,但我不会怀疑它们会直接更改源代码。我会在结果出现时发布结果。

干杯!

于 2010-09-13T15:26:14.143 回答
0

要解决上述披露图标问题,您可以指定 AdvancedDataGrid 的“treeColumn”属性,传递您的树数据应位于的列的 id。

显然,前几天我在看 livedocs 时失明了。

干杯!

于 2010-09-13T17:31:21.550 回答