开发者

DataGrid Column names don't seem to be binding

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?


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 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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜