2010-01-29 85 views
2

我有簡單的MXML代碼如何將列添加到Adobe Flex mx:DataGrid中的mxml和/或actionsctpt?

<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怎麼做這樣的事?

如何將列添加到Adobe Flex mx:DataGrid在mxml和/或actionsctpt中?

(你可以把這個代碼的Flash或AIR應用程序 - 它沒有錯誤編譯,但不會增加任何列=()

回答

2

添加行的最好方法是使用可綁定數據提供程序,而我更喜歡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"} 
]); 

然後在你的DataGrid,而不是d。通過MXML efining您的DP添加這樣的:

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

然後你就可以通過動作腳本添加項目到您的MyAC變種添加行:

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

因爲它綁定會自動的顯示數據網格。

假設你在MXML中定義你的列在您的例子中,你可以添加裁判柱像這樣:

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

感謝ClownBaby爲列添加因爲這是已經發布。

編輯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> 
+0

對不起) - 它不起作用( – Rella 2010-01-29 22:48:56

+0

它不會將行添加到數據網格不止一次!!!(((( – Rella 2010-01-29 23:20:24

+0

發佈您正在使用的代碼添加所以我們可以看到爲什麼。 – invertedSpear 2010-01-29 23:39:14

3

從我可以收集您嘗試了一些列的動態添加您的Datagrid:

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

對不起 - 它不工作( – Rella 2010-01-29 22:27:41

+0

它不添加((( – Rella 2010-01-29 23:19:35

0

休耕例如增加10列的DataGrid 「myGrid」

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; 
相關問題