【问题标题】:Flex DataGrid does not sort correctly if a column contains custom UIComponent如果列包含自定义 UIComponent,则 Flex DataGrid 无法正确排序
【发布时间】:2012-10-03 23:04:33
【问题描述】:

我有一个 DataGrid,第一列是字符串(“Red”、“Blue”等),第二列是相应颜色的圆圈(自定义 UIComponent)。如果我单击第一列进行排序,所有颜色的名称都可以很好地排序,但第二列中的那些圆圈的顺序完全错误。有人知道下面的代码出了什么问题吗?

这是应用程序 mxml:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx"
               xmlns:components="components.*"
               minWidth="955" minHeight="600">

    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            [Bindable]
            public var myArray:ArrayCollection = new ArrayCollection([
                { name:"Red",    color:0xff0000 },
                { name:"Orange", color:0xff8000 },
                { name:"Yellow", color:0xffff00 },
                { name:"Green",  color:0x00ff00 },
                { name:"Blue",   color:0x0000ff },
                { name:"Purple", color:0xff00ff }
            ]);
        ]]>
    </fx:Script>

    <s:DataGrid dataProvider="{myArray}">
        <s:columns>
            <s:ArrayCollection>
                <s:GridColumn dataField="name"/>
                <s:GridColumn dataField="color">
                    <s:itemRenderer>
                        <fx:Component>
                            <s:GridItemRenderer>
                                <s:VGroup>
                                    <components:Circle color="{data.color}" />
                                </s:VGroup>
                            </s:GridItemRenderer>
                        </fx:Component>
                    </s:itemRenderer>
                </s:GridColumn>
            </s:ArrayCollection>
        </s:columns>
    </s:DataGrid>
</s:Application>

这是自定义的 UIComponent:

package components
{
    import mx.core.UIComponent;

    public class Circle extends UIComponent
    {
        public var color:uint;

        public function Circle()
        {
            super();
            height = 20;
            width = 20;
        }

        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            super.updateDisplayList(unscaledWidth, unscaledWidth);
            graphics.clear();
            graphics.beginFill(color, 1);
            graphics.drawCircle(10, 10, 8);
            graphics.endFill();
        }
    }
}

【问题讨论】:

    标签: apache-flex sorting datagrid uicomponents


    【解决方案1】:

    您的自定义组件可能根本没有重绘。最简单的解决方案是根本不使用自定义组件,而是使用 FXG 图形来绘制您的圆圈。

    用这个替换你的 itemrenderer,它应该可以正常工作:

    <s:GridItemRenderer>
         <s:Ellipse width="20" height="20">
             <s:fill>
                  <s:SolidColor color="{data.color}" />
             </s:fill>
         </s:Ellipse>
    </s:GridItemRenderer>
    

    也就是说,我会将标签和颜色样本放在一列(而不是两列),因为它们代表相同的数据。

    【讨论】:

    • 我实际上需要做一些更复杂的事情,所以我必须继承 UIComponent。我发布的代码经过高度简化,但可以重现我面临的问题。
    • 然而它让我给你一个不是真正答案的答案(这对我们俩来说真的是浪费时间)。这个更复杂的事情到底是什么?它是否涉及graphics(因为我现在不知道)?如果是这样,你可以用 FXG 做不到的图形做什么(我想不出任何东西)。如果您关心性能,为什么将扩展的 UIComponent 组合到渲染器中而不是直接扩展 GridItemRenderer(在纯 AS 中)?
    猜你喜欢
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 2011-08-21
    • 2015-08-09
    • 1970-01-01
    相关资源
    最近更新 更多