views:

737

answers:

2

Hi,

I have a flex datagrid with 4 columns.I have a comboBox with 4 checkboxes,containing the column names of datagrid as its label.I want the datagrid to display only those columns which are selected in combobox.Can anyone tell me how this filtering of columns in datagrid can be done?

Thanks in advance.

+1  A: 

You can manipulate the columns attached to the data grid using the .columns property. Bear in mind that this method is a getter and returns you a copy of the list of columns on the datagrid, so if you manipulate its contents you have to apply those changes back to the data grid using the equivalent setter, e.g.

<mx:DataGrid id="dg" />

in ActionScript code

var columns:Array = dg.column;
columns.push(new DataGridColumn("hello"));
dg.columns = columns;

In your case you could hold your master list of columns in a separate array and push them onto the data grid as the user checks and un-checks them from the list in your comboBox.

Alternatively you can iterate through the column list looking for the ones which are checked in your comboBox and set their .visible property accordingly.

HTH

Simon
Thanks a lot Simon.I have one more problem.How can I get the selected checkBox label(which is present inside a comboBox)so that I can make a comparison with the column name and display only those columns whose name matches with the selected checkbox
Anupama
To help with that I'd have to see the code you use to populate the combo box. In principle you could use a column list to populate the comboBox and then when you retrieve the selectedItem property you would have the DataGridColumn object. That would, however, imply you create a master list.
Simon
A: 

If you are open to a commercial alternative, please look at http://www.flexicious.com. If you want to do the work yourself, its not too difficult - like the other answer, just set the columns property.

Flexicoius