AdvancedDataGrid按Dataprovider添加Column

时间:2012-07-10 14:38:37

标签: flex advanceddatagrid

我在Flex中使用AdvancedDataGrid时遇到问题。 我有一个带有Bindable ArrayCollection的AdvancedDataGrid作为DataProvider。当我设置ArrayCollection时(通过单击按钮1),AdvancedDataGrid将代表4列。

之后我点击Button 2.在这种情况下,AdvancedDataGrid仍显示4列而不是预期的3列。

情景2

首先点击Button2 - > AdvancedDataGrid显示3列 单击Button1 - > AdvancedDataGrid仍然显示3列而不是预期的4列

如何告诉AdvancedDataGrid调整列数?

摘录:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">
    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.events.FlexEvent;
            [Bindable]
            private var dataProv:ArrayCollection;


            protected function button1_clickHandler(event:MouseEvent):void {
                dataProv = new ArrayCollection([
                    {country:"USA", year2004:3.5, year2005:4.2, year2006:3.7},
                    {country:"UK", year2004:1.7, year2005:3.1, year2006:3.6},
                    {country:"Canada", year2004:2.8, year2005:2.9, year2006:3.4},
                    {country:"Japan", year2004:2.6, year2005:2.3, year2006:2.8},
                    {country:"France", year2004:1.4, year2005:2.1, year2006:2.6},
                    {country:"Brazil", year2004:2.6, year2005:4.9, year2006:4.4},
                    {country:"Russia", year2004:6.4, year2005:7.2, year2006:6.7},
                    {country:"India", year2004:8.0, year2005:7.1, year2006:6.6},
                    {country:"China", year2004:9.9, year2005:10.1, year2006:10.6}
                ]); 
                adg.validateNow();
                adg.dataProvider.refresh();
            }


            protected function button2_clickHandler(event:MouseEvent):void{
                dataProv = new ArrayCollection([
                    {country:"USA", year2004:3.5, year2005:4.2},
                    {country:"UK", year2004:1.7, year2005:3.1},
                    {country:"Canada", year2004:2.8, year2005:2.9},
                    {country:"Japan", year2004:2.6, year2005:2.3},
                    {country:"France", year2004:1.4, year2005:2.1},
                    {country:"Brazil", year2004:2.6, year2005:4.9},
                    {country:"Russia", year2004:6.4, year2005:7.2},
                    {country:"India", year2004:8.0, year2005:7.1},
                    {country:"China", year2004:9.9, year2005:10.1}
                ]); 
                adg.validateNow();
                adg.dataProvider.refresh();
            }

        ]]>
    </mx:Script>

    <mx:HBox width="100%" height="100%">
        <mx:AdvancedDataGrid
            id="adg"
            width="100%"
            height="100%"
            dataProvider="{dataProv}"/>
        <mx:Button label="change" click="button1_clickHandler(event)"/>
        <mx:Button label="changeBack" click="button2_clickHandler(event)"/>
    </mx:HBox>

</mx:Application>

1 个答案:

答案 0 :(得分:1)

您需要扩展AdvancedDataGrid类并覆盖collectionChangeHandler。您需要将protected var generatedColumns设置为true,以使动态列代码执行多次。

override protected function collectionChangeHandler(event:Event):void
{
    generatedColumns = true;
    super.collectionChangeHandler(event);
}