Enable and disable (editable) columns on checkbox chaangehandler - apache-flex

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>

Related

flex itemrenderer prevents datagrid item select

I'm somewhat new to Flex and have encountered a problem with itemRenderer that I can't find the solution to. I've asked a coworker with plenty of flex experience, and I've tried searching the internet with no luck.
My problem is that I have a dataGrid in which each column uses an itemRenderer to display information, and for some reason this causes the user to be unable to select any of the dataGrid rows. I think it must have something to do with itemRenderer, because when I added a dummy column without an itemRenderer, I was able to highlight and select a row by clicking on that dummy column, but the others still did not work. I've tried comparing my code with code for other dataGrids with itemRenderers that do work, but I haven't been able to find any differences that would cause the problem that mine is giving me. Does anyone know why this would happen?
Thank you!
My dataGrid (I tried to only include what I think should be relevant just to keep things concise. If anyone thinks more information is needed, please let me know!):
<mx:DataGrid id="servicegridUI" left="10" right="10" top="10" bottom="85" selectable="true"
styleName="formDataGrid" variableRowHeight="true" toolTip="Double-click to view.">
<mx:columns>
<mx:DataGridColumn headerText="ID" dataField="ID" width="175">
<mx:itemRenderer>
<mx:Component>
<mx:HBox paddingBottom="3" height="70" paddingLeft="5" horizontalGap="1" paddingTop="2" horizontalScrollPolicy="off" verticalScrollPolicy="off">
<mx:Text height="100%" width="100%" id="id" htmlText="" selectable="true" doubleClick="openDoc(event)" doubleClickEnabled="true"/>
<mx:Script>
<![CDATA[
var refId:String = "";
override public function set data(value:Object):void {
//variables for setting text are created here
id.htmlText = 'ID: ' + refId + '<br><font color="#666666">Service ID: ' + servId + '</font>';
id.htmlText +='<br><font color="#666666">Type and Specialty: ' + type + ' - ' + specialty + '</font>';
}
public function openDoc(event:MouseEvent):void {
//removed due to irrelevance
}
]]>
</mx:Script>
</mx:HBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="Claimant" dataField="claimantHeader" width="125">
<mx:itemRenderer>
<mx:Component>
<mx:HBox paddingBottom="3" height="70" paddingLeft="5" horizontalGap="1" paddingTop="2" horizontalScrollPolicy="off" verticalScrollPolicy="off">
<mx:Text height="100%" width="100%" id="claimant" htmlText="" selectable="true" doubleClick="openDoc(event)" doubleClickEnabled="true"/>
<mx:Script>
<![CDATA[
var refId:String = "";
override public function set data(value:Object):void {
//variables for setting text are created here
claimant.htmlText = 'Claim: ' + claim + '<br><font color="#666666">Name: '+ name +'</font>';
claimant.htmlText +='<br><font color="#666666">Date: '+ date+'</font>';
}
// Opens a new browser window and loads the file
public function openDoc(event:MouseEvent):void {
//removed due to irrelevance
}
]]>
</mx:Script>
</mx:HBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="Status" dataField="statusHeader" width="70">
<mx:itemRenderer>
<mx:Component>
<mx:HBox paddingBottom="3" height="70" paddingLeft="5" horizontalGap="1" paddingTop="2" horizontalScrollPolicy="off" verticalScrollPolicy="off" >
<mx:Text height="100%" width="100%" id="status" htmlText="" selectable="true" doubleClick="openDoc(event)" doubleClickEnabled="true"/>
<mx:Script>
<![CDATA[
var refId:String = "";
override public function set data(value:Object):void {
//variables for setting text are created here
status.htmlText = 'Date: ' + refDate;
status.htmlText += '<br><font color="#666666">Status: ' + currStatus + '</font>';
}
// Opens a new browser window and loads the file
public function openDoc(event:MouseEvent):void {
//removed due to irrelevance
}
]]>
</mx:Script>
</mx:HBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="lalala" dataField="serviceID" width="50"/><!---this is the dummy column that I created and is the only one that functions properly-->
</mx:columns>
</mx:DataGrid>
When you override set data function, you need to say
super.data = value;
It will fix your problem.
If you want to run the complete application, here is an example based on your code:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script><![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
private var myArrayCollection:ArrayCollection = new ArrayCollection([
{ID:"1",claimantHeader: "ClaimHeader1",statusHeader:"StatusHeader1", serviceID:"SID1" , servId:"1001", name:"Bikram Dangol", type:"Type 1",specialty:"Speciality 1", claim:"Claim 1", date:"12/06/2014", refDate:"11/06/2014", currStatus:"Active"},
{ID:"2",claimantHeader: "ClaimHeader2",statusHeader:"StatusHeader2", serviceID:"SID2", servId:"1002", name:"Anup Dangol", type:"Type 2",specialty:"Speciality 2", claim:"Claim 2", date:"12/07/2014", refDate:"11/07/2014", currStatus:"Inactive"},
{ID:"3",claimantHeader: "ClaimHeader3",statusHeader:"StatusHeader3", serviceID:"SID3", servId:"1003",name:"Lunish Yakami", type:"Type 3",specialty:"Speciality 3", claim:"Claim 3", date:"12/08/2014", refDate:"11/08/2014", currStatus:"OnHold"},
]);
]]></mx:Script>
<mx:DataGrid id="servicegridUI" left="10" right="10" top="10" bottom="85" selectable="true" dataProvider="{myArrayCollection}"
styleName="formDataGrid" variableRowHeight="true" toolTip="Double-click to view.">
<mx:columns>
<mx:DataGridColumn headerText="ID" dataField="ID" width="175">
<mx:itemRenderer>
<mx:Component>
<mx:HBox paddingBottom="3" height="70" paddingLeft="5" horizontalGap="1" paddingTop="2" horizontalScrollPolicy="off" verticalScrollPolicy="off">
<mx:Text height="100%" width="100%" id="ID" htmlText="" selectable="true" doubleClick="openDoc(event)" doubleClickEnabled="true"/>
<mx:Script>
<![CDATA[
var refId:String = "";
override public function set data(value:Object):void {
super.data = value;
//variables for setting text are created here
ID.htmlText = 'ID: ' + refId + '<br><font color="#666666">Service ID: ' + data.servId + '</font>';
ID.htmlText +='<br><font color="#666666">Type and Specialty: ' + data.type + ' - ' + data.specialty + '</font>';
}
public function openDoc(event:MouseEvent):void {
//removed due to irrelevance
}
]]>
</mx:Script>
</mx:HBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="Claimant" dataField="claimantHeader" width="125">
<mx:itemRenderer>
<mx:Component>
<mx:HBox paddingBottom="3" height="70" paddingLeft="5" horizontalGap="1" paddingTop="2" horizontalScrollPolicy="off" verticalScrollPolicy="off">
<mx:Text height="100%" width="100%" id="claimant" htmlText="" selectable="true" doubleClick="openDoc(event)" doubleClickEnabled="true"/>
<mx:Script>
<![CDATA[
var refId:String = "";
override public function set data(value:Object):void {
super.data = value;
//variables for setting text are created here
claimant.htmlText = 'Claim: ' + data.claim + '<br><font color="#666666">Name: '+ data.name +'</font>';
claimant.htmlText +='<br><font color="#666666">Date: '+ data.date+'</font>';
}
// Opens a new browser window and loads the file
public function openDoc(event:MouseEvent):void {
//removed due to irrelevance
}
]]>
</mx:Script>
</mx:HBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="Status" dataField="statusHeader" width="70">
<mx:itemRenderer>
<mx:Component>
<mx:HBox paddingBottom="3" height="70" paddingLeft="5" horizontalGap="1" paddingTop="2" horizontalScrollPolicy="off" verticalScrollPolicy="off" >
<mx:Text height="100%" width="100%" id="status" htmlText="" selectable="true" doubleClick="openDoc(event)" doubleClickEnabled="true"/>
<mx:Script>
<![CDATA[
var refId:String = "";
override public function set data(value:Object):void {
super.data = value;
//variables for setting text are created here
status.htmlText = 'Date: ' + data.refDate;
status.htmlText += '<br><font color="#666666">Status: ' + data.currStatus + '</font>';
}
// Opens a new browser window and loads the file
public function openDoc(event:MouseEvent):void {
//removed due to irrelevance
}
]]>
</mx:Script>
</mx:HBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="lalala" dataField="serviceID" width="50"/><!---this is the dummy column that I created and is the only one that functions properly-->
</mx:columns>
</mx:DataGrid>
</mx:Application>

Flex Nested data grid

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

How to show the DropDownList component in Flex DataGrid?

I am very new to flex. I have successfully added a dropDownList in my dataGrid table.The dropdownList works fine. Its just that since I am doing this for the user. I want to be able to see the dropDowList component/Icon itself in the dataGrid table, instead of me clicking the cell and then the dropDownList appears for me to select. Is it possible for me to achieve this?? Must I use flex skin or something ? Pls guide me on how can I can achieve this and pls if possible give me an example.
Thanks
Rekha
This is the code I have right now:
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import spark.events.IndexChangeEvent;
import mx.controls.Alert;
[Bindable]
private var myDP:ArrayCollection = new ArrayCollection([
{label1:"Order #2314", quant:3, color:'red'},
{label1:"Order #2315", quant:3, color:'red'}
]);
]]>
</fx:Script>
<s:DataGrid id="myDG" x="29" y="33" width="393" height="151" dataProvider="{myDP}"
editable="true" variableRowHeight="true">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="label1" headerText="Order #" editable="false"/>
<s:GridColumn dataField="quant" headerText="Qty" editable="true"/>
<s:GridColumn dataField="color" headerText="Color" editable="true">
<s:itemEditor>
<fx:Component>
<s:GridItemEditor>
<fx:Script>
<![CDATA[
import mx.core.FlexGlobals;
import mx.events.FlexEvent;
import spark.events.IndexChangeEvent;
override public function set value(newValue:Object):void {
cb.selectedItem = newValue;
}
override public function get value():Object {
return cb.selectedItem.toString();
}
override public function setFocus():void {
cb.setFocus();
}
override public function save():Boolean
{
data[column.dataField] = value;
return true;
}
]]>
</fx:Script>
<s:DropDownList id="cb" requireSelection="true" skinClass="MySkins.mytestddbSkin">
<s:dataProvider>
<s:ArrayCollection>
<fx:String>red</fx:String>
<fx:String>blue</fx:String>
<fx:String>green</fx:String>
</s:ArrayCollection>
</s:dataProvider>
</s:DropDownList>
</s:GridItemEditor>
</fx:Component>
</s:itemEditor>
</s:GridColumn>
</s:ArrayList>
</s:columns >
</s:DataGrid>
You can use an ItemRenderer instead of the ItemEditor. In this case you should set rendererIsEditable property of the GridColumn to true.
Then you can use the change event of the DropDownList, to register the change in your data provider.
I have added the color column twice to let you see, that the data is actually edited after the selection.
<?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 myDP:ArrayCollection = new ArrayCollection([
{label1:"Order #2314", quant:3, color:'red'},
{label1:"Order #2315", quant:3, color:'red'}
]);
]]>
</fx:Script>
<s:DataGrid id="myDG" x="29" y="33" height="151" dataProvider="{myDP}"
editable="true" variableRowHeight="true">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="label1" headerText="Order #" editable="false"/>
<s:GridColumn dataField="quant" headerText="Qty" editable="true"/>
<s:GridColumn dataField="color" headerText="Color" editable="true" width="120"/>
<s:GridColumn dataField="color" headerText="Color" editable="true" rendererIsEditable="true" width="180">
<s:itemRenderer>
<fx:Component>
<s:GridItemRenderer>
<fx:Script>
<![CDATA[
import spark.events.IndexChangeEvent;
protected function onCbChange(event:IndexChangeEvent):void
{
var value:String = (event.currentTarget as DropDownList).selectedItem;
data[column.dataField] = value;
}
]]>
</fx:Script>
<s:DropDownList id="cb" requireSelection="true" width="100%" change="onCbChange(event)">
<s:dataProvider>
<s:ArrayCollection>
<fx:String>red</fx:String>
<fx:String>blue</fx:String>
<fx:String>green</fx:String>
</s:ArrayCollection>
</s:dataProvider>
</s:DropDownList>
</s:GridItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:GridColumn>
</s:ArrayList>
</s:columns >
</s:DataGrid>
</s:Application>

advanceddatagrid component with group and header renderer in flex

How to create advanceddatagrid component with grouprenderer and checkboxheader renderer in flex 3?
Please find my solution below:
<mx:AdvancedDataGridColumn headerText="indexed"
width="100"
dataField="indexed"
showDataTips="true"
editable="false"
headerWordWrap="true"
headerRenderer="index.DatagridCheckBoxHeaderRenderer"
sortable="false">
<mx:itemRenderer>
<fx:Component>
<mx:HBox horizontalAlign="center">
<mx:CheckBox id="indexed"
click="this.data.indexed=!this.data.indexed"
selected="{data.indexed}">
</mx:CheckBox>
</mx:HBox>
</fx:Component>
</mx:itemRenderer>
</mx:AdvancedDataGridColumn>
DatagridCheckboxHeaderRenderer.mxml:
<mx:VBox 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%"
horizontalAlign="center">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.collections.HierarchicalData;
import mx.controls.AdvancedDataGrid;
public function checkAllDatainDatagrid(isObjectSelected:Boolean,propertyName:String):void
{
//if the header is checked,iterate thru the datagrid data
//and set all the checkbox to true
if (isObjectSelected)
{
selectAll(parentDocument.attributeList,isObjectSelected, propertyName);
}
else if(!isObjectSelected)
{
selectAll(parentDocument.attributeList,false, propertyName);
}
parentDocument.gridHierarchialData.source = parentDocument.attributeList;
}
protected function selectAll(dataCollection:ArrayCollection,isObjectSelected:Boolean, propertyName:String):ArrayCollection{
for each (var obj:Object in dataCollection)
{
if(obj.hasOwnProperty('children')){
var childCollection:ArrayCollection = obj.children;
selectAll(childCollection,isObjectSelected, propertyName)
}else if(obj.hasOwnProperty(propertyName)){
obj[propertyName]=isObjectSelected;
}
}
return dataCollection;
}
]]>
</fx:Script>
<s:Label text="Index ?"
verticalAlign="middle"/>
<mx:CheckBox id="headerComboBox"
click="checkAllDatainDatagrid(headerComboBox.selected,data.dataField);"
verticalCenter="0">
</mx:CheckBox>

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.

Resources