开发者

Flex Restore Datagrid to Original Columns Defined in MXML

I have a datagrid, containing three columns defined in the MXML.

I am also adding some columns dynamically to the datagrid, depending on the data retrieved.

Initially, I set the original columns to an array in the initialize event of the application:

private var _origColumns:Array();

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

And when I invoke the function to add columns, I want to restore first the datagrid to its original set of columns

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

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

  myDataGrid.columns = columnToAdd;
}

The problem I have is that, when I invoke the function addCols, those columns that were added also remains, even though I am trying to revert back the new array of columns to be added to the _origColumns array. Basically I am ending up with the other columns originally added, plus the new columns that were just added.开发者_C百科 I would like to revert to the first three columns first, then just add over the columns which I want to add.

I checked the _origColumns variable and it seems to be growing when the columns have already been added to the datagrid, although I am not re-assigning it to the value of the datagrid columns.

I hope someone can provide more insight on why this is happening. Been banging my head for the last couple of hours on this.

I tried to add a changewatcher utility to the array _origColumns but it seems it is being changed but the watch event is not dispatching.

Thanks,


It is because in your appInit function, you have overwritten _origColumns with the same underlying array object from the datagrid. So whenever a column is added to the datagrid, by default it is being added to your _origColumns array.

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

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

A quick fix would be to make a copy of the array, you can do so using the Array.concat method (I'll assume at least Flex 3). Change the offending line to:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜