aodz

Dynamically add remove columns in a datagrid

Posted on: August 6, 2008


i had a hard time figuring out how to add remove columns  dynamically from a datagrid and after lots of code iterations i have reached a workable scenario. i still need to add other important functionalities. close headerrenderer for each column, delete entire row functionality , user defined default state … etc

check out the code:

(make a new dynamicGridColum.mxml component and add in an application tag)


<?xml version=”1.0″ encoding=”utf-8″?>
<mx:HDividedBox xmlns:mx=”http://www.adobe.com/2006/mxml&#8221; height=”100%” width=”100%”
xmlns:containers=”framework.view.components.containers.*”
dividerRelease=”handleDivderRelease()”>
<mx:Script>
<![CDATA[
import mx.controls.CheckBox;
import mx.controls.ComboBox;
public var counter : int = 0;

public var conDataProvider : Array =[
{label : “col1”, data : “c1”},
{label : “col2”, data : “c2”},
{label : “col3”, data : “c3”},
{label : “col4”, data : “c4”},
{label : “col5”, data : “c5”},
{label : “col6”, data : “c6″},

private function init():void{

// do something!!
}
private function hideShowColumns():void  {
var actualColumns:Array = ResultGrid.columns;
var actualSelectedColumns:Array =  lstColumns.selectedItems;
var dataGridColumn:DataGridColumn;
var sDataField:String;
var sDataFieldCur:String;
var columnVisible:Boolean
for (var i:int=0;i<actualColumns.length;i++)  {
columnVisible = false
dataGridColumn = actualColumns[i];
sDataField = dataGridColumn.dataField;
for (var j : int = 0; j < actualSelectedColumns.length; j++)  {
if(actualSelectedColumns[j] == null) continue;
sDataFieldCur = actualSelectedColumns[j].data;
if (sDataFieldCur == sDataField)  {
columnVisible = true;
break;
}
}
if (columnVisible) {
dataGridColumn.visible = true;
}
else  {
dataGridColumn.visible = false;
}
}
}

private function selectAll():void{
lstColumns.selectedIndices=[0,1,2,3,4,5];
hideShowColumns();
}
private function clearAll():void{
lstColumns.selectedIndices=[];
hideShowColumns();
}
private function setDefault():void{
lstColumns.selectedIndices=[0,1,2,3];
hideShowColumns();
}
private function handleDivderRelease():void{
if(counter == 0){
controlBox.width =0
}else
if(counter == 1){
controlBox.width =120
}
counter = (counter +1)%2;
}

]]>
</mx:Script>
<mx:VBox id=”controlBox” height=”100%” width=”120″ maxWidth=”120″  verticalGap=”0″ horizontalScrollPolicy=”off”>
<mx:Label text=”Selected columns” fontWeight=”bold” />
<mx:List height=”100%” width=”120″ id=”lstColumns” dataProvider=”{conDataProvider}”
labelField=”label” borderColor=”#000000″
allowMultipleSelection=”true” click=”hideShowColumns()”
selectedIndices=”[0,1,2,3]”/>
<mx:TextArea wordWrap=”true” selectable=”false” editable=”false” borderStyle=”none”
text=”For multiple selection use ctrl/shift keys” width=”120″ />
<mx:Spacer height=”5″ />
<mx:Button label=”Select All”  width=”100%” click=”selectAll()”/>
<mx:Button label=”Default”  width=”100%” click=”setDefault()”/>
<mx:Button label=”Clear All” width=”100%” click=”clearAll()”/>
</mx:VBox>

<mx:VBox height=”100%” width=”100%”>
<mx:DataGrid  height=”100%” id=”ResultGrid” selectedIndex=”0″ enabled=”true” selectable=”true” creationComplete=”init()”>
<mx:columns>
<mx:DataGridColumn headerText=”col1″ dataField=”c1″ visible=”true” />
<mx:DataGridColumn headerText=”col2″ dataField=”c2″ visible=”true” />
<mx:DataGridColumn headerText=”col3″ dataField=”c3″ visible=”true” />
<mx:DataGridColumn headerText=”col4″ dataField=”c4″ visible=”true” />
<mx:DataGridColumn headerText=”col5″ dataField=”c5″ visible=”false” />
<mx:DataGridColumn headerText=”col6″ dataField=”c6″ visible=”false” />
</mx:columns>
</mx:DataGrid>
</mx:VBox>
</mx:HDividedBox>

Advertisements
Tags: ,

7 Responses to "Dynamically add remove columns in a datagrid"

moms teaching teens sex Cool Super! Everything is very clear and competently, and at the same time without philosophising and narcissism, and in accessible language. Rare case when a person is divided into topical and interesting infoy. Thanks to author! Blog just super, I will recommend to friends! They like. Must be waiting to continue. This topic is certainly of interest to all. You know now who rarely writes on the subject, very pleasant to read, I would advise to add more pictures! I do not know how the rest, but I liked it. On your blog, familiar in the ICQ link tossed. It turned out that nothing I liked. Tepr always read will Who is mulberry? All true, but how else?

plump women with large tits Many find useful for themselves Thank you! Article interesting. How delightful !!!!!!!!!!!!) Very good site, but not bad-would make a version for smartphones. Your site in the opera is not the adjustment is shown, and everything is fine! спасибки you for your clever ideas! Happiness – not a reward for virtue, but virtue itself, not because we enjoy the happiness that curb their passions, but rather enjoy the happiness makes us able to curb them. – Spinoza somewhere I have already seen this … Thanks for the post, and this issue

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

I have moved to a different location

check out my new home Flexout

calendar

August 2008
M T W T F S S
« Oct   Sep »
 123
45678910
11121314151617
18192021222324
25262728293031

Blog Stat

  • 83,703 Hop's so far!!!

follow me

Archives

Linkedin Blogger Twitter Youtube Orkut

Top Clicks

  • None

latest flickr photos

top rated

%d bloggers like this: