apache-flex 如何在mxml和/或actionsctpt中将列添加到Adobe flex mx:DataGrid?

lstz6jyr  于 2022-11-01  发布在  Apache
关注(0)|答案(3)|浏览(145)

我有简单的mxml代码

<mx:DataGrid id="DGG"
             editable="true">
    <mx:dataProvider>
        <mx:Object scheduledDate="4/1/2006"/>
    </mx:dataProvider>
</mx:DataGrid>
<mx:Button id="SetBut"
           label="Set Array as Data Provider"
           click="SetDP(); AddBut.visible = true;"
           x="100.5"
           y="164"
           width="211"/>
<mx:Button id="AddBut"
           label="Add a column!"
           click="AddCol();"
           x="100.5"
           y="194"
           width="211"
           visible="false"/>
<mx:Script>
    <![CDATA[
        import mx.controls.dataGridClasses.DataGridColumn;
        import mx.collections.ArrayCollection;

        [Bindable]
        public var MyAC:ArrayCollection=new ArrayCollection([{scheduledDate: "4/1/2006", homeTeam: "Chester Bucks"}]);

        public function SetDP():void
        {
            DGG.dataProvider=MyAC
        }

        public function AddCol():void
        {
            MyAC.addItem({scheduledDate: "4/5/2007", homeTeam: "Long Valley Hitters", Umpire: "Amanda Hugenkis"});
            DGG.columns.push(new DataGridColumn("Umpire"));
        }
    ]]>
</mx:Script>

我想添加行到我的表datagrid如何做这样的事情?
如何在mxml和/或actionsctpt中将列添加到Adobe flex mx:DataGrid?
(You我可以将此代码放在Flash或AIR应用程序中-它将编译而不会出错,但不会添加任何列=()

d6kp6zgx

d6kp6zgx1#

据我所知,您正在尝试动态地向Datagrid添加一些列:

dataGrid.columns.push(new DataGridColumn("dataField"));
0yycz8jy

0yycz8jy2#

添加行的最佳方法是使用可绑定的dataProvider,我更喜欢ArrayCollection类。

[Bindable] public var MyAC:ArrayCollection = new ArrayCollection([
  {scheduledDate:"4/1/2006", homeTeam:"Chester Bucks",awayTeam:"Long Valley Hitters", field:"Dawn Field", umpire:"Phil McKraken"}
]);

然后,在数据网格中,不要通过MXML定义DP,而是按如下方式添加:

<mx:DataGrid id="dataGrid" editable="true" dataprovider="{MyAC}"....>

然后,您可以通过操作脚本将项目添加到MyAC var中来添加行:

MyAC.AddItem({scheduledDate:"4/5/2006", homeTeam:"Long Valley Hitters",awayTeam:"Chester Bucks", field:"Sunset Field", Umpire:"Amanda Hugenkis"})

因为它是绑定的,所以它将自动显示在dataGrid中。
假设您在MXML中定义了列,如示例所示,您可以按如下方式添加“裁判”列:

dataGrid.columns.push(new DataGridColumn("Umpire"));

感谢ClownBaby添加AS的列,该列已经发布。
2010年2月1日编辑:完整代码示例

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >
<mx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;

        [Bindable] public var MyAC:ArrayCollection = new ArrayCollection([
          {scheduledDate:"4/1/2006", homeTeam:"Chester Bucks",awayTeam:"Long Valley Hitters", field:"Dawn Field", umpire:"Phil McKraken"}
        ]);

        public function addRow():void{
            MyAC.addItem({scheduledDate:"4/5/2006", homeTeam:"Long Valley Hitters",awayTeam:"Chester Bucks", field:"Sunset Field", Umpire:"Amanda Hugenkis"})
        }

        public function addCol():void{
            var dgc:DataGridColumn = new DataGridColumn("Umpire");
                var cols:Array = dataGrid.columns;
                cols.push(dgc);
                dataGrid.columns = cols;
        }
    ]]>
</mx:Script>

<mx:DataGrid id="dataGrid" editable="true" dataProvider="{MyAC}">
    <mx:columns>
        <mx:DataGridColumn dataField="scheduledDate" />
        <mx:DataGridColumn dataField="homeTeam" />
        <mx:DataGridColumn dataField="awayTeam" />
        <mx:DataGridColumn dataField="field" />
    </mx:columns>
</mx:DataGrid>
    <mx:Button x="10" y="150" label="Add Row" click="addRow();"/>
    <mx:Button x="202" y="150" label="Add Col" click="addCol();"/>

</mx:Application>
bxgwgixi

bxgwgixi3#

下面的示例向datagrid“myGrid”添加10列

private var columns:Array;

        for (var i:int=0; i<10;i++){
          var dgc:DataGridColumn = new DataGridColumn("column name");
          dgc.headerText="column header";

          columns.push(dgc);
        }

        myGrid.columns=columns;

相关问题