2011-07-29 30 views
0

我有一個數據網格,其中包含MXML中定義的三列。Flex將Datagrid恢復爲MXML中定義的原始列

我還將一些列動態添加到數據網格,具體取決於檢索的數據。

起初,我設定的原始列到一個數組中的應用程序的初始化事件:

private var _origColumns:Array(); 

private function appInit() :void 
{ 
    _origColumns = new Array(); 
    _origColumns = myDataGrid.columns; 
} 

當我調用該函數增加一列,我想給DataGrid先恢復到原來的設置列

private function addCols() :void 
{ 
    var columnToAdd:Array = new Array(); 
    columnToAdd = _origColumns; 

    // Add the new columns to the columnToAdd Array. 
    // 
    // 

    myDataGrid.columns = columnToAdd; 
} 

我的問題是,當我調用函數addCols,添加的這些列也仍然存在,即使我試圖恢復列的新陣列添加到_origColumns陣列。基本上,我結束了最初添加的其他列,以及剛剛添加的新列。我想先回到前三列,然後添加我想添加的列。

我檢查了_origColumns變量,它似乎在列已經被添加到數據網格時增長,雖然我不重新分配給datagrid列的值。

我希望有人能提供更多的洞察,爲什麼會發生這種情況。在過去的幾個小時裏,我一直在喋喋不休。

我試圖添加一個changewatcher實用程序到數組_origColumns,但它似乎正在改變,但watch事件不調度。

感謝,

+0

您可以在部分中發佈這些代碼//將新列添加到columnToAdd數組中,因爲它們可能是發生這種情況的原因。 – Angelo

回答

1

這是因爲在你appInit功能,已覆蓋_origColumns與從DataGrid相同的底層數組對象。所以無論何時將一列添加到數據網格中,默認情況下都會將其添加到您的_origColumns數組中。

private function appInit() :void 
{ 
    _origColumns = new Array(); 

    _origColumns = myDataGrid.columns; // Offending line 
    // both properties will reference the same underlying Array object 
} 

速戰速決將是使該數組的副本,你可以這樣做使用Array.concat方法(我假設至少Flex 3的)。將違規行更改爲:

_origColumns = myDataGrid.columns.concat(); // passing no arguments makes a shallow copy