分派事件后 DataGrid 引用为 NULL

发布于 2024-08-13 11:38:03 字数 2103 浏览 2 评论 0原文

我是一名 Flex 新手,我在事件模型方面遇到了困难。我有以下场景:

  • 将 DataGrid 的 dataProvider 设置为 ArrayCollection
  • 数据网格是一个简单的待办事项列表,第一列包含复选框作为项目渲染器,
  • 其他列是纯字符串

我需要实现的是创建数据网格后或初始化,我需要有条件地更新项目渲染器值的颜色样式。条件表示,如果属性 Done(存储在数据提供程序中)的值为 true,则将文本颜色设置为灰色。

问题是项目渲染器在创建数据网格之前初始化,因此我可以在项目渲染器中获取的数据网格引用为 NULL。因此,我决定数据网格完成后通知项目渲染器。问题是如何使用 Flex 事件模型来做到这一点。

看起来项目渲染器没有监听数据网格调度的事件。请看一下我的代码:

<!-- Data grid inside root panel main.mxml -->
<mx:DataGrid id="taskGrid" dataProvider="{tasks}" creationComplete="dispatchEvent(new Event('update',true));">
 <mx:columns>
  <mx:DataGridColumn dataField="done" headerText="!">
   <mx:itemRenderer>
     <mx:Component>
       <c:StatusCheckBox change="this.onChange(event);"/>
     </mx:Component>
   </mx:itemRenderer>
  </mx:DataGridColumn>
  <mx:DataGridColumn dataField="status" headerText="Status" editable="false" itemRenderer="components.CustomLabel"/>
 </mx:columns>
</mx:DataGrid>

<!-- components.CustomLabel.mxml -->
<mx:Label xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init();">
 <mx:Script >
   <![CDATA[
    import (...)
    private var dg:DataGrid; 
    private var tasks:ArrayCollection;

    private function init():void {
     dg = this.listData.owner as DataGrid;  
     addEventListener("update",updateStyle); 
     if (dg) Alert.show("dg is not null!"); // data grid is not null when init() finish
    }

    private function updateStyle(e:Event = null):void {
     if (dg) {
      if (listData.rowIndex < dg.dataProvider.length) {
       var task:Task = dg.dataProvider[listData.rowIndex] as Task;
        if (task.done) this.setStyle("color","Blue");
        else this.setStyle("color","Black");
       }
      } 
     }
   ]]>
  </mx:Script>
 </mx:Label>

当我启动应用程序并创建数据网格时,会触发“更新”事件。但是,CustomLabel 组件中的 updateStyle 函数中使用的数据网格实例 (dg) 为 null。 为什么它现在为空?正如您在 init() 方法中看到的那样,dg 变量不为空(项目渲染器的每个实例都会弹出警报)

您能帮助我吗?非常感谢你们。

I'm a total Flex newbie and I struggle with event model. I have the following scenario:

  • having DataGrid with dataProvider set to ArrayCollection
  • the data grid is a simple to-do list, first column contains check box as item renderer
  • other columns are plain strings

What I need to achieve is that after data grid has been created or initialised, I need to update the colour style of item renderers values conditionally. The condition says, if value of property Done (stored in data provider) is true, then set colour of text to grey.

The problem is that item renderers are initialised before data grid is created, therefore data grid reference which I can obtain in item renderer is NULL. So I decided to notify item renderers after data grid is completed. Question is how to do it using Flex event model.

It looks like event dispatched by data grid is not listened by item renderer. Please have a look at my code:

<!-- Data grid inside root panel main.mxml -->
<mx:DataGrid id="taskGrid" dataProvider="{tasks}" creationComplete="dispatchEvent(new Event('update',true));">
 <mx:columns>
  <mx:DataGridColumn dataField="done" headerText="!">
   <mx:itemRenderer>
     <mx:Component>
       <c:StatusCheckBox change="this.onChange(event);"/>
     </mx:Component>
   </mx:itemRenderer>
  </mx:DataGridColumn>
  <mx:DataGridColumn dataField="status" headerText="Status" editable="false" itemRenderer="components.CustomLabel"/>
 </mx:columns>
</mx:DataGrid>

<!-- components.CustomLabel.mxml -->
<mx:Label xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init();">
 <mx:Script >
   <![CDATA[
    import (...)
    private var dg:DataGrid; 
    private var tasks:ArrayCollection;

    private function init():void {
     dg = this.listData.owner as DataGrid;  
     addEventListener("update",updateStyle); 
     if (dg) Alert.show("dg is not null!"); // data grid is not null when init() finish
    }

    private function updateStyle(e:Event = null):void {
     if (dg) {
      if (listData.rowIndex < dg.dataProvider.length) {
       var task:Task = dg.dataProvider[listData.rowIndex] as Task;
        if (task.done) this.setStyle("color","Blue");
        else this.setStyle("color","Black");
       }
      } 
     }
   ]]>
  </mx:Script>
 </mx:Label>

When I start-up my application and data grid is created the 'update' event is triggered. However the data grid instance (dg) used in updateStyle function in CustomLabel component is null. Why it's now null? As you can see in init() method dg variable is not null (the Alert pops up for every instance of item renderer)

Could you help me? Many thanks guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清醇 2024-08-20 11:38:03

如果您监听 dataChange 事件,它可以触发处理程序。

If you listen to dataChange event, it can trigger the handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文