Flex Nested data grid - datagrid

I am creating nested data grid using flex, I have different columns for each level(refer screenshot). But its rendering new data grid for each column.
Here is my mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
private var dpHierarchy:ArrayCollection = new ArrayCollection([
{Country:"INDIA", Category:"Developing Country" ,Population:100000000,
children: [
{State:"Andhra", Language:"Telugu", SoilColor:"Red",
children:[
{District:"GUNTUR", HeadQuaters:388865, Crops:"Tobacco"},
{District:"Vijayawada", HeadQuaters:388865, Crops:"rice"}
] },
{State:"Karnataka", Language:"Kannada", SoilColor:"Black",
children:[
{District:"Mysore", HeadQuaters:388865, Crops:"Mirchi"},
{District:"Mandya", HeadQuaters:388865, Crops:"Vegetables"}
] }
] },
{Country:"KOREA", Category:"UnDeveloping Country", Population:100000000,
children:[
{State:"fgff", Language:"fggff", SoilColor:"Red",
children: [
{District:"fgdfgfg", HeadQuaters:388865, Crops:"Tobacco"},
{District:"gfgdfgfg", HeadQuaters:388865, Crops:"rice"}
] },
{State:"fgfgdfg", Language:"gdfgdfg", SoilColor:"Black",
children:[
{District:"ggff", HeadQuaters:388865, Crops:"Mirchi"},
{District:"gfgfgfg", HeadQuaters:388865, Crops:"Vegetables"}
] }
] }
]);
]]>
</fx:Script>
<mx:AdvancedDataGrid width="100%" height="100%" variableRowHeight="true">
<mx:dataProvider>
<mx:HierarchicalData source="{dpHierarchy}"/>
</mx:dataProvider>
<mx:columns>
<mx:AdvancedDataGridColumn dataField="Country" headerText="Country"/>
<mx:AdvancedDataGridColumn dataField="Category" headerText="Category"/>
<mx:AdvancedDataGridColumn dataField="Population" headerText="Population"/>
<mx:AdvancedDataGridColumn dataField="children" headerText="children"/>
</mx:columns>
<mx:rendererProviders>
<mx:AdvancedDataGridRendererProvider columnIndex="1" columnSpan="4" depth="2"
renderer="InnerGrid"/>
<mx:AdvancedDataGridRendererProvider columnIndex="2" columnSpan="3" depth="3"
renderer="InnerChildGrid"/>
</mx:rendererProviders>
</mx:AdvancedDataGrid>
</s:Application>
Here is my custom renderer [InnerGrid.mxml]
<?xml version="1.0" encoding="utf-8"?>
<mx:AdvancedDataGrid xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
override public function set data(value:Object):void {
this.dataProvider = value;
}
]]>
</fx:Script>
<mx:columns>
<mx:AdvancedDataGridColumn dataField="State" headerText="State"/>
<mx:AdvancedDataGridColumn dataField="Language" headerText="Language"/>
<mx:AdvancedDataGridColumn dataField="SoilColor" headerText="SoilColor"/>
</mx:columns>
</mx:AdvancedDataGrid>
Here is my custom renderer [InnerChildGrid.mxml]
<?xml version="1.0" encoding="utf-8"?>
<mx:AdvancedDataGrid xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
override public function set data(ivalue:Object):void {
this.dataProvider = ivalue.Districts;
}
]]>
</fx:Script>
<mx:columns>
<mx:AdvancedDataGridColumn dataField="District"/>
<mx:AdvancedDataGridColumn dataField="HeadQuaters"/>
<mx:AdvancedDataGridColumn dataField="Crops"/>
</mx:columns>
</mx:AdvancedDataGrid>

I solved the problem, I am attaching the code for someone to refer in future
[Application mxml]
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
<mx:Script>
<![CDATA[
import mx.collections.HierarchicalData;
private var masterData:Array = [
{ OrderId: 10248, CustomerId:"WILMK", EmployeeId:5, OrderDate:"1-Feb-2007",
children:[
[
{ProductId:11, ProductName:"Quesbo Cabrales", UnitPrice:14, Quantity:12, Discount:0, Price:168, children:[[{count:"28",place:"HYD",Amount:"1234"},{count:"28",place:"HYD",Amount:"1234"}]]},
{ProductId:42, ProductName:"Singaporean Hokkien Fried Mee", UnitPrice:9.8, Quantity:10, Discount:0, Price:98, children:[[{count:"28",place:"HYD",Amount:"1234"},{count:"28",place:"HYD",Amount:"1234"}]]},
{ProductId:42, ProductName:"Mozzarella di Giovanni", UnitPrice:34.8, Quantity:5, Discount:0, Price:174, children:[[{count:"28",place:"HYD",Amount:"1234"},{count:"28",place:"HYD",Amount:"1234"}]]}
]
]},
{ OrderId: 10249, CustomerId:"TRADH", EmployeeId:6, OrderDate:"3-Feb-2007",
children:[
[
{ProductId:51, ProductName:"Manjimup Dried Appels", UnitPrice:42.4, Quantity:40, Discount:0, Price:1696, children:[[{count:"28",place:"HYD",Amount:"1234"},{count:"28",place:"HYD",Amount:"1234"}]]},
{ProductId:14, ProductName:"Tofu", UnitPrice:18.6, Quantity:9, Discount:0, Price:167.4, children:[[{count:"28",place:"HYD",Amount:"1234"},{count:"28",place:"HYD",Amount:"1234"}]]}
]
]},
{ OrderId: 10250, CustomerId:"HANAR", EmployeeId:4, OrderDate:"4-Feb-2007",
children:[
[
{ProductId:51, ProductName:"Manjimup Dried Appels", UnitPrice:42.4, Quantity:35, Discount:0.15, Price:1261, children:[[{count:"28",place:"HYD",Amount:"1234"},{count:"28",place:"HYD",Amount:"1234"}]]},
{ProductId:41, ProductName:"Jack's Clam Chowder", UnitPrice:7.7, Quantity:10, Discount:0, Price:77, children:[[{count:"28",place:"HYD",Amount:"1234"},{count:"28",place:"HYD",Amount:"1234"}]]},
{ProductId:65, ProductName:"Hot pepper Sauce", UnitPrice:16.8, Quantity:10, Discount:0.15, Price:214.2, children:[[{count:"28",place:"HYD",Amount:"1234"},{count:"28",place:"HYD",Amount:"1234"}]]}
]
]}
];
]]>
</mx:Script>
<mx:AdvancedDataGrid dataProvider="{new HierarchicalData(masterData)}" variableRowHeight="true" width="600" height="600">
<mx:columns>
<mx:AdvancedDataGridColumn dataField="OrderId" headerText="Ordere ID" width="100"/>
<mx:AdvancedDataGridColumn dataField="CustomerId" headerText="Customer ID" />
<mx:AdvancedDataGridColumn dataField="EmployeeId" headerText="Employee ID" />
<mx:AdvancedDataGridColumn dataField="OrderDate" headerText="Order Date" />
</mx:columns>
<mx:rendererProviders>
<mx:AdvancedDataGridRendererProvider depth="2" columnIndex="1" renderer="DetailGrid" columnSpan="0" />
</mx:rendererProviders>
</mx:AdvancedDataGrid>
</mx:Application>
[DetailGrid.mxml]
<?xml version="1.0" encoding="utf-8"?>
<mx:AdvancedDataGrid xmlns="*" variableRowHeight="true" xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.collections.HierarchicalData;
override public function set data(value:Object):void
{
dataProvider=new HierarchicalData(value);
this.rowCount = value.length+5;
// dataProvider = value;
}
]]>
</mx:Script>
<mx:columns>
<mx:AdvancedDataGridColumn dataField="ProductId" headerText="Product ID" />
<mx:AdvancedDataGridColumn dataField="ProductName" headerText="Product Name" />
<mx:AdvancedDataGridColumn dataField="UnitPrice" />
<mx:AdvancedDataGridColumn dataField="Discount" />
<mx:AdvancedDataGridColumn dataField="Price" />
</mx:columns>
<mx:rendererProviders>
<mx:AdvancedDataGridRendererProvider depth="2" columnIndex="1" renderer="InnerDetailGrid" columnSpan="5" />
</mx:rendererProviders>
</mx:AdvancedDataGrid>
[InnerDetailGrid.mxml]
<?xml version="1.0" encoding="utf-8"?>
<mx:AdvancedDataGrid xmlns="*" xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
override public function set data(value:Object):void
{
dataProvider = value;
this.rowCount = value.length+1;
}
]]>
</mx:Script>
<mx:columns>
<mx:AdvancedDataGridColumn dataField="count" headerText="count" />
<mx:AdvancedDataGridColumn dataField="place" headerText="place" />
<mx:AdvancedDataGridColumn dataField="Amount" headerText="Amount" />
</mx:columns>
</mx:AdvancedDataGrid>
Here is the final o/p

Related

Enable and disable (editable) columns on checkbox chaangehandler

I have 3 columns in advanced data grid, First contains checkbox and other two contains text boxes,all of these are inside separate itemrenderers of datagrid columns
If checkbox is checked subsequent textboxes in particular row should be editable and when checkbox is unchecked textboxes should not be editable
This Code will work
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="100%" height="100%" creationComplete="maximize()">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
public var arrayColl:ArrayCollection = new ArrayCollection([
{sno:1, fname: 'ABC 1', lname: 'DEF 1'},
{sno:2, fname: 'ABC 2', lname: 'DEF 2'},
{sno:3, fname: 'ABC 3', lname: 'DEF 3'},
{sno:4, fname: 'ABC 4', lname: 'DEF 4'}
]);
]]>
</fx:Script>
<s:VGroup width="100%" height="100%" verticalAlign="middle" horizontalAlign="center">
<mx:AdvancedDataGrid id="advancedDataGrid" width="50%" height="50%" dataProvider="{arrayColl}">
<mx:columns>
<mx:AdvancedDataGridColumn dataField="" headerText="" width="50">
<mx:itemRenderer>
<fx:Component>
<s:MXAdvancedDataGridItemRenderer>
<fx:Script>
<![CDATA[
protected function chkEdit_changeHandler(event:Event):void
{
data.isEditable = chkEdit.selected;
outerDocument.arrayColl.refresh();
}
]]>
</fx:Script>
<s:CheckBox id="chkEdit" selected="false" change="chkEdit_changeHandler(event)"
verticalCenter="0" horizontalCenter="0"/>
</s:MXAdvancedDataGridItemRenderer>
</fx:Component>
</mx:itemRenderer>
</mx:AdvancedDataGridColumn>
<mx:AdvancedDataGridColumn dataField="fname" headerText="First Name">
<mx:itemRenderer>
<fx:Component>
<s:MXAdvancedDataGridItemRenderer>
<fx:Script>
<![CDATA[
import spark.events.TextOperationEvent;
override public function set data(value:Object):void {
super.data = value;
if (data != null) {
txtFName.editable = data.isEditable;
}
}
protected function txtFName_changeHandler(event:TextOperationEvent):void
{
data.fname = event.currentTarget.text;
}
]]>
</fx:Script>
<s:TextInput id="txtFName" text="{listData.label}" editable="false" verticalCenter="0" horizontalCenter="0"
change="txtFName_changeHandler(event)"/>
</s:MXAdvancedDataGridItemRenderer>
</fx:Component>
</mx:itemRenderer>
</mx:AdvancedDataGridColumn>
<mx:AdvancedDataGridColumn dataField="lname" headerText="Last Name">
<mx:itemRenderer>
<fx:Component>
<s:MXAdvancedDataGridItemRenderer>
<fx:Script>
<![CDATA[
import spark.events.TextOperationEvent;
override public function set data(value:Object):void {
super.data = value;
if (data != null) {
txtLName.editable = data.isEditable;
}
}
protected function txtLName_changeHandler(event:TextOperationEvent):void
{
data.lname = event.currentTarget.text;
}
]]>
</fx:Script>
<s:TextInput id="txtLName" text="{listData.label}" editable="false" verticalCenter="0" horizontalCenter="0"
change="txtLName_changeHandler(event)"/>
</s:MXAdvancedDataGridItemRenderer>
</fx:Component>
</mx:itemRenderer>
</mx:AdvancedDataGridColumn>
</mx:columns>
</mx:AdvancedDataGrid>
</s:VGroup>
</s:WindowedApplication>

flex - access linkbutton outside the datagrid

I am using this code:
<mx:DataGridColumn id="test" headerText="Case ID" width="80">
<mx:itemRenderer>
<fx:Component>
<mx:Canvas>
<mx:LinkButton id="lnkCaseId" click="lnkCaseId.dispatchEvent(new MouseEvent(MouseEvent.CLICK,true,false))" label="{data.caseId}" textDecoration="underline" color="#0052A5">
</mx:LinkButton>
</mx:Canvas>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
How can I access lnkCaseId outside the datagrid?
You can add external-document event-listener to Inline renderers using outerdocument method
<mx:LinkButton label="Buy" click="{outerDocument.outerFileMethod(data)}" />
Another way of getting rendrer is by event bubbling please see sample below
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
click="{clicked(event)}">
<mx:Script>
<![CDATA[
import mx.controls.LinkButton;
import mx.core.UIComponent;
import mx.controls.Alert;
public function clicked(event:MouseEvent):void
{
if (event.target is LinkButton)
{
var innerLinkButon:LinkButton = event.target as LinkButton;
Alert.show("Application : "+innerLinkButon.label);
}
}
]]>
</mx:Script>
<mx:DataGrid id="grid">
<mx:dataProvider>
<mx:ArrayCollection>
<mx:Array>
<mx:Object label="AAAA"/>
<mx:Object label="BBBB"/>
<mx:Object label="CCCC"/>
<mx:Object label="DDDD"/>
</mx:Array>
</mx:ArrayCollection>
</mx:dataProvider>
<mx:columns>
<mx:DataGridColumn id="columnA" headerText="columnA" dataField="#label">
<mx:itemRenderer>
<mx:Component>
<mx:LinkButton click="{clicked(event)}" label="{data.label.toString()}">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
public function clicked(event:MouseEvent):void
{
Alert.show("linkButton");
}
]]>
</mx:Script>
</mx:LinkButton>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
</mx:Application>
Hopes that helps

How to show data from two different elements in one column?

How can I show data from two different elements in a single column? or if there is a way to combine two columns?
Like in following example just firstName is showing up. How can I display lastName with it too in the same column?
Folloing is the XML File 'which can't be changed':
<userEmail>
<user firstName="john" lastName="seena" />
<user firstName="pinku" lastName="phil"/>
</userEmail>
Code:
monitoringArray = event.result.userEmail.user;
.
.
.
<mx:DataGrid id="monDataGrid" x="10" y="10" dataProvider="{monitoringArray}">
<mx:columns>
<mx:DataGridColumn width="150" headerText="User Name" dataField="firstName"
</mx:columns>
</mx:DataGrid>
It definetely could be done by using labelFunction property of the DatagridColumn.
rough example:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
private var xml:XML = <userEmail>
<user firstName="john" lastName="seena" />
<user firstName="pinku" lastName="phil"/>
</userEmail>
private function getCombinedUserNameLabel(item:Object, col:DataGridColumn):String
{
return item.#firstName + " " + item.#lastName;
}
]]>
</fx:Script>
<mx:DataGrid id="monDataGrid" x="10" y="10" dataProvider="{xml.user}">
<mx:columns>
<mx:DataGridColumn width="150" headerText="User Name" labelFunction="{getCombinedUserNameLabel}"/>
</mx:columns>
</mx:DataGrid>
EDIT: Arrays also shouldn't be a problem:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
creationComplete="application1_creationCompleteHandler(event)"
>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
private var xml:XML = <userEmail>
<user firstName="john" lastName="seena" />
<user firstName="pinku" lastName="phil"/>
</userEmail>;
private var monitoringArray:Array = [{firstName:"john", lastName:"seena"}, {firstName:"pinku", lastName:"phil"}];
private function getCombinedUserNameLabel(item:Object, col:DataGridColumn):String
{
return item.#firstName + " " + item.#lastName;
}
private function getCombinedUserNameLabel2(item:Object, col:DataGridColumn):String
{
return item.firstName + " " + item.lastName;
}
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
// TODO Auto-generated method stub
}
]]>
</fx:Script>
<s:VGroup>
<mx:DataGrid id="monDataGrid" x="10" y="10" dataProvider="{xml.user}">
<mx:columns>
<mx:DataGridColumn width="150" headerText="User Name" labelFunction="{getCombinedUserNameLabel}"/>
</mx:columns>
</mx:DataGrid>
<mx:DataGrid id="monDataGrid2" x="10" y="10" dataProvider="{monitoringArray}">
<mx:columns>
<mx:DataGridColumn width="150" headerText="User Name" labelFunction="{getCombinedUserNameLabel2}"/>
</mx:columns>
</mx:DataGrid>
</s:VGroup>
</s:Application>
I guess it cannot be done without changing the XML file. The datafield property can take only one string.
Here is the definition from adobe reference website.
dataField : String
The name of the field or property in the data provider item
associated with the column.
Complex columns in datagrids can be achieved with item renderers. You can see examples on the tour de flex app from adobe. Tour De Flex

Deleting selected rows from a DataGrid in Flex

I added a checkbox in a DataGrid using ItemRenderer. I have pasted the code I am using below.
<mx:DataGrid id="dgEmployeeInfo" dataProvider="{resultArray}" x="131" y="95" editable="false">
<mx:columns>
<mx:DataGridColumn headerText="Select" rendererIsEditor="true" editorDataField="selected">
<mx:itemRenderer>
<fx:Component>
<mx:HBox>
<s:CheckBox id="testChk" click="testChk_clickHandler(event)" selected="{cbSelected}">
</s:CheckBox>
<fx:Script>
<![CDATA[
[Bindable]
public var cbSelected:Boolean;
protected function testChk_clickHandler(event:MouseEvent):void
{
cbSelected = testChk.selected;
}
]]>
</fx:Script>
</mx:HBox>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="First Name" dataField="firstName"/>
<mx:DataGridColumn headerText="Last Name" dataField="lastName"/>
<mx:DataGridColumn headerText="City" dataField="city"/>
<mx:DataGridColumn headerText="Employee Code" dataField="empCode"/>
</mx:columns>
</mx:DataGrid>
I also have a button outside the DataGrid, and when this button is clicked, I want to delete all rows which have a checked CheckBox. Can someone tell me how to do this?
HI!
First of all, your code will not work properly, since not every DataGrid row has its own ItemRenderer instance. There are exactly n item renderer instances for a DataGrid having n VISIBLE rows. You can check this easily, if you create DataGrid, that can not fit all the data and then select some rows and scroll datagrid up/down. You'll see unexpected results.
One of solutions could be having additional field "selected" (or whatever you want to name it) in resultArray item properties. Then you can access this property through "data" object of ItemRenderer. Check the code below:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
private var resultArray:ArrayCollection = new ArrayCollection
([
{firstName:"1-1",lastName:"1-2",city:"1-3",empCode:"1-4"},
{firstName:"2-1",lastName:"2-2",city:"2-3",empCode:"2-4"},
{firstName:"3-1",lastName:"3-2",city:"3-3",empCode:"3-4"},
{firstName:"4-1",lastName:"4-2",city:"4-3",empCode:"4-4"},
{firstName:"5-1",lastName:"5-2",city:"5-3",empCode:"5-4"},
{firstName:"6-1",lastName:"6-2",city:"6-3",empCode:"6-4"},
{firstName:"7-1",lastName:"7-2",city:"7-3",empCode:"7-4"},
{firstName:"8-1",lastName:"8-2",city:"8-3",empCode:"8-4"},
{firstName:"9-1",lastName:"9-2",city:"9-3",empCode:"9-4"},
{firstName:"10-1",lastName:"10-2",city:"10-3",empCode:"10-4"},
]);
protected function button1_clickHandler(event:MouseEvent):void
{
for (var i:int=0; i< resultArray.length; i++)
{
if (resultArray[i].selected == true)
{
resultArray.removeItemAt(i);
}
}
dgEmployeeInfo.invalidateList();
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:VGroup>
<mx:DataGrid id="dgEmployeeInfo" dataProvider="{resultArray}" x="131" y="95" editable="false">
<mx:columns>
<mx:DataGridColumn headerText="Select" rendererIsEditor="true" editorDataField="selected">
<mx:itemRenderer>
<fx:Component>
<mx:HBox>
<s:CheckBox id="testChk" click="testChk_clickHandler(event)" selected="{data.selected}">
<fx:Script>
<![CDATA[
[Bindable]
public var cbSelected:Boolean;
protected function testChk_clickHandler(event:MouseEvent):void
{
data.selected = testChk.selected;
}
]]>
</fx:Script>
</s:CheckBox>
</mx:HBox>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="First Name" dataField="firstName"/>
<mx:DataGridColumn headerText="Last Name" dataField="lastName"/>
<mx:DataGridColumn headerText="City" dataField="city"/>
<mx:DataGridColumn headerText="Employee Code" dataField="empCode"/>
</mx:columns>
</mx:DataGrid>
<mx:Button label="Delete Items" click="button1_clickHandler(event)"/>
</s:VGroup>
</s:Application>
I used ArrayCollection as dataProvider as it is much mor easier to add/remove items to Collection objects then with Arrays.
Regards.

flex, AdvancedDataGrid, custom itemrenderer

I seem to be missing some key concept(s) when it comes to flex itemrenderers, particularly as it applies to an AdvancedDataGrid. I'm doing what a lot of other people are trying to do: change the bg color of a field based on data from the row. My problem seems to be in accessing data fields? Basically, when this loads, nothing appears. If I remove the parts from the renderer where I try to access field data, it works, but kind of defeats the purpose.
Here's what I've got:
<mx:AdvancedDataGrid width="100%" height="100%"
id="topAccountsGrid"
borderStyle="solid" dropShadowEnabled="true" treeColumn="{list_name}" editable="false" selectionMode="singleRow"
dragEnabled="true" dropEnabled="true" dragMoveEnabled="true" dragDrop="topAccountsGrid_dragDropHandler(event)"
doubleClickEnabled="true" itemDoubleClick="topAccountsGrid_itemDoubleClickHandler(event)"
sort="topAccountsGrid_sortHandler(event)" backgroundColor="#ffffff">
<mx:dataProvider>
<mx:HierarchicalData source="{filteredList}"
childrenField="children" />
</mx:dataProvider>
<mx:columns>
<mx:AdvancedDataGridColumn id="colRank" headerText="Rank" dataField="Rank__c" width="60">
<mx:itemRenderer>
<fx:Component>
<mx:HBox paddingLeft="2">
<s:Label id="tempLabel" text="{data.Rank__c}" />
<fx:Script>
<![CDATA[
override public function set data(value:Object) : void{
super.data = value;
if(data.Health__c == 0){
setStyle("backgroundColor",0xFF5050);
} else if(data.Health__c == 50){
setStyle("backgroundColor",0xFFFF99);
} else if(data.Health__c == 100){
setStyle("backgroundColor",0x66FF66);
}
}
]]>
</fx:Script>
</mx:HBox>
</fx:Component>
</mx:itemRenderer>
</mx:AdvancedDataGridColumn>
<mx:AdvancedDataGridColumn id="list_name" headerText="Name" dataField="Name" />
<mx:AdvancedDataGridColumn id="colPrevRank" headerText="Previous Rank" dataField="Previous_Rank__c" />
<mx:AdvancedDataGridColumn id="colType" headerText="Type" dataField="Type" />
<mx:AdvancedDataGridColumn id="colContacts" headerText="# Contacts" dataField="Contacts__c" />
<mx:AdvancedDataGridColumn id="colDeals" headerText="# Deals" dataField="Deals__c" />
</mx:columns>
</mx:AdvancedDataGrid>
I've had a similar issue in the past when using HierarchicalData. The way I got around it was to use an AdvancedDataGridRendererProvider.
Here's some sample code:
<mx:AdvancedDataGrid width="100%" height="100%"
id="topAccountsGrid"
backgroundColor="#ffffff">
<mx:dataProvider>
<mx:HierarchicalData source="{filteredList}"
childrenField="children" />
</mx:dataProvider>
<mx:columns>
<mx:AdvancedDataGridColumn id="colRank" headerText="Rank" dataField="Rank__c" width="60" />
<mx:AdvancedDataGridColumn id="list_name" headerText="Name" dataField="Name" />
<mx:AdvancedDataGridColumn id="colPrevRank" headerText="Previous Rank" dataField="Previous_Rank__c" />
<mx:AdvancedDataGridColumn id="colType" headerText="Type" dataField="Type" />
<mx:AdvancedDataGridColumn id="colContacts" headerText="# Contacts" dataField="Contacts__c" />
<mx:AdvancedDataGridColumn id="colDeals" headerText="# Deals" dataField="Deals__c" />
</mx:columns>
<mx:rendererProviders>
<mx:AdvancedDataGridRendererProvider column="{colRank}" depth="1" dataField="Rank__c" renderer="AdvancedDataGridRankCRenderer" />
</mx:rendererProviders>
</mx:AdvancedDataGrid>
And the AdvancedDataGridRendererProvider:
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
backgroundColor="{SetBackgroundColor(data)}"
paddingLeft="2" paddingRight="2" paddingTop="2"
horizontalScrollPolicy="off"
verticalScrollPolicy="off"
height="22">
<fx:Script>
<![CDATA[
[Bindable] private var bgColor:uint = 0xD6E5FF;
private function SetBackgroundColor(obj:Object):uint
{
var returnColor:uint = 0xFF5050;
if (obj["Rank__c"] != null)
{
switch (obj["Rank__c"].toString().toUpperCase())
{
case "0":
returnColor = 0xFF5050;
break;
case "50":
returnColor = 0xFFFF99;
break;
case "100":
returnColor = 0x66FF66;
break;
default:
returnColor = 0xFF5050;
break;
}
}
return returnColor;
}
override public function set data(value:Object):void
{
super.data = value;
rankLabel.text = value["Rank__c"].toString();
validateDisplayList();
}
]]>
</fx:Script>
<mx:Label id="rankLabel" />
</mx:HBox>

Resources