重新渲染DataGrid

时间:2011-05-23 07:03:07

标签: datagrid flex4

我在自定义组件中有一个数据网格。此DG接受嵌套对象作为其数据提供者,因此我的数据网格呈现如下:

<mx:DataGrid id="privilegesDG" dataProvider="{privArray}" width="100%" variableRowHeight="true">
        <mx:columns>
            <mx:DataGridColumn dataField="Name" />
            <mx:DataGridColumn dataField="Alias" />
            <mx:DataGridColumn headerText="Roles Assigned" dataField="roles">
                <mx:itemRenderer>
                    <fx:Component>
                        <mx:VBox creationComplete="box1_creationCompleteHandler(event)">
                            <fx:Script>
                                <![CDATA[
                                    import com.pm.modules.events.UpdateDBEvent;

                                    import mx.containers.HBox;
                                    import mx.controls.Label;
                                    import mx.controls.LinkButton;
                                    import mx.events.FlexEvent;

                                    [Bindable]private var prID:int;
                                    protected function box1_creationCompleteHandler(event:FlexEvent):void
                                    {
                                        for each(var temp:Object in data.roles){
                                            prID = temp.rid;
                                            var hgrp:HBox = new HBox();
                                            hgrp.autoLayout = false;

                                            var lbl:Label = new Label();
                                            lbl.text = temp.rname;

                                            var lb:LinkButton = new LinkButton();
                                            lb.label = 'X';
                                            lb.focusEnabled = true;
                                            lb.addEventListener(MouseEvent.CLICK,handleClick);

                                            hgrp.addElement(lbl);
                                            hgrp.addElement(lb);

                                            this.addElement(hgrp);
                                        }
                                    }

                                    protected function handleClick(event:MouseEvent):void{
                                        dispatchEvent(new UpdateDBEvent(UpdateDBEvent.ON_DELETE_PRIVILEGE_ROLE_MAP,0,0,0,prID,0,true));
                                    }
                                ]]>
                            </fx:Script>
                        </mx:VBox>
                    </fx:Component>
                </mx:itemRenderer>
            </mx:DataGridColumn>
        </mx:columns>
    </mx:DataGrid>

所以我有一个显示嵌套数据的内联itemrenderer。现在eveytime发生了更新操作,我调用一个函数来重新填充这个数据网格。但是用嵌套数据dsnt填充的列显示任何内容......

我在网上搜索过,发现mayb我会为这个组件调用updateDisplayList函数吗?

所以我尝试过这样的事情:

override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
            super.updateDisplayList(unscaledWidth,unscaledHeight);
        }


        protected function handleResult(event:ResultEvent):void{
            if(event.result.toString() == 'false')
                Alert.show("Could not perform operation");
            else{
                RO.getPrivilegesAndRoles(); //re-populates DG
                invalidateDisplayList();
            }
        }

我真的不知道我把它放在那个功能里面。我应该在updateDisplayList中调用重新填充函数。我试过了,但它没有工作......

1 个答案:

答案 0 :(得分:2)

项目渲染器的creationComplete事件在初始化时只出现一次。就项目渲染器进行回收而言,新数据不会填充在您的项目渲染器中。

您可以通过两种方式解决问题:

  1. 创建MXML项呈示器。至于它的基类是VBox,这是最好的选择。因此,您可以摆脱数据绑定以自动填充数据更改。要使用Repeater作为数据提供者,请使用{data.roles}组件而不是循环来填充所有角色。
  2. 第二个选项是使用ActionScript并将UIComponent作为基类。在这种情况下,您应该覆盖override public function set data(value:Object):void并在invalidateDisplayList()的{​​{1}}事件订阅中调用data.roles,并从变更处理程序调用{​​{1}}。然后在collectionChange中,您应该循环数据并创建/填充所有控件。第二个选项可以为您提供更好的性能,但对于初学者来说这不是一个好方法。
  3. 因此,我建议您使用带绑定的MXML版本。