views:

258

answers:

2

Sort of a Flex newbie here so bear with me. I've got a DataGrid defined as follows:

<mx:Script>
...
private function getColumns(names:ArrayCollection):Array {
    var ret:Array = new Array();
    for each (var name:String in names) {
        var column:DataGridColumn = new DataGridColumn(name);
        ret.push(column);
    }
    return ret;
}
</mx:Script>
<mx:DataGrid id="grid" width="100%" height="100%" paddingTop="0"
  columns="{getColumns(_dataSetLoader.columnNames)}"
  horizontalScrollPolicy="auto" labelFunction="labelFunction"
  dataProvider="{_dataSetLoader.data}"
/>

...where _dataSetLoader is an instance of an object that looks like:

[Bindable]
public class DataSetLoader extends EventDispatcher {
    ...
    private var _data:ArrayCollection = new ArrayCollection();
    private var _columnNames:ArrayCollection = new ArrayCollection();
    ...
    public function reset():void {
        _status = NOTLOADED;
        _data.removeAll();
        _columnNames.removeAll();
    }
    ...

When reset() is called on the dataSetLoader instance, the DataGrid empties the data in the cells, as expected, but leaves the column names, even though reset() calls _columnNames.removeAll(). Shouldn't the change in the collection trigger a change in the DataGrid?

A: 

Your data is properly bound because you refer directly to the variable as dataProvider. For the columns, you refer to a function call.

Can you assign the values of _dataSetLoader.columnNames to a bindable ArrayCollection instead? Then use that ArrayCollection as columns.

Well, columns is an Array rather than an ArrayCollection. But at any rate, I also tried adding an Array called _columnNamesAsArray to the DataSetLoader class, referencing that in the column= clause, and emptying it in reset(), but got the same result. Also, my understanding is that data binding works across function calls as well, as long as the argument to the function is bound? That is, the change in _columnNames ought to trigger a call to getColumns(). Am I wrong on that?
Jason
A: 

Well there are various alternatives or work arounds. It depends on what exactly is your requirement.

Below is what you can do with your Datagrid component

  1. If you already know the column names i.e In your UI Interface the column names do not change. You might wanna hard code them instead of supplying dynamically.

  2. If the column name changes with the array collection or the dataprovider, I suggest you remove the column property of your datagrid and let the default column names be displayed.

  3. You can also add Columns at run time depending on inputs provided by drop down or check boxes or some other conditions.

Check out Flex Documentation for more info.

bakore
Yeah, none of those options work for me. The DataGrid is basically implementing a CSV reader. The first line of the CSV is the column names, second line is field types, and the rest of the lines are data. So the column names change all the time, depending on the file being read. That's why I'd like to store them in an ArrayCollection and use data binding to manage updates to the view.
Jason
If you are not filtering out any columns then option 2 should work.
bakore