Selecting a Checkbox and deleting a data grid row in Flex - apache-flex

I am trying to implement the following :
First column of datagrid has a checkbox.
Select checkboxes, and then delete the datagrid column.
Dynamically, add checkbox when row is added dynamically.
Do not show check box if now data in row.
Can someone give some guidance ?

I am assuming you want to delete a row and not a column. The following works
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical">
<mx:Script>
<![CDATA[
import mx.events.IndexChangedEvent;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
[Bindable]
private var ac:ArrayCollection=new ArrayCollection([{name: "John", shouldDelete: true}, {name: "Joe", shouldDelete: false}, {name: "Jill", shouldDelete: false}])
private function deleteRows()
{
for each (var row:Object in ac)
{
if (row.shouldDelete == true)
{
var i:int=ac.getItemIndex(row);
ac.removeItemAt(i);
}
}
}
]]>
</mx:Script>
<mx:VBox>
<mx:DataGrid id="dg"
dataProvider="{ac}">
<mx:columns>
<mx:DataGridColumn dataField="name">
</mx:DataGridColumn>
<mx:DataGridColumn id="col2"
editorDataField="selected"
rendererIsEditor="true"
dataField="data.shouldDelete">
<mx:itemRenderer>
<mx:Component>
<mx:CheckBox label="Test"
selected="{data.shouldDelete}"
change="data.shouldDelete=selected"/>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
<mx:Button label="delete"
id="deleteBtn"
click="deleteRows()"/>
</mx:VBox>
</mx:Application>

In itemdatabound u should give enabled as false in particular cell....

Related

flex - get linkbutton label name and selected row of datagrid

i am using below code for using linkbutton in flex datagrid
<mx:DataGridColumn headerText="Case ID" width="80">
<mx:itemRenderer>
<fx:Component>
<mx:Canvas>
<mx:LinkButton id="lnkCaseId" click="outerDocument.lnkCaseIdClick(event)" label="{data.caseId}" textDecoration="underline" color="#0052A5">
</mx:LinkButton>
</mx:Canvas>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn><br/>
now on link button click i want linkbutton label name and selected row inside lnkCaseIdClick method, how can i do this?thanks.
Don't use outerDocument.lnkCaseIdClick(event), it's a horrible practice since you're assuming the function will always be there and makes your code coupled.
You should look into bubbling a custom event that holds the data you need from the item renderer, and then from your container, add the event listener for your custom event.
Here is sample according to explanation in #J_A_X but its using default MouseEvent You can extent *MouseEvent* Class to hold your custom data
<?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

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 CheckBox in Datagrid

In the followin flex Code :
Also viewable at : http://www.cse.epicenterlabs.com/checkBoxDg/checkBoxDg.html
1. Add a row in datagrid by clicking on "AddRow"
2. Click on "CheckDg" to see the values of all the checkboxes
- it shows "checkBox57" or "checkBox64" or some similar string
3. Now, "select" the checkBox in the first row.
4. Click again on "CheckDg"
-it show "true"
So, initially dp.getItemAt(i).date returns a CheckBox
and later it returns the "selected" value of the CheckBox?
Why this difference?
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" viewSourceURL="srcview/index.html">
<mx:Canvas>
<mx:DataGrid x="69" y="119" id="dgFee" editable="true" dataProvider="{dp}">
<mx:columns>
<mx:DataGridColumn headerText="Date" dataField="date" width="100" editable="true"
editorDataField="selected" rendererIsEditor="true">
<mx:itemRenderer>
<mx:Component>
<mx:CheckBox selected="false">
</mx:CheckBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn dataField="amount" headerText="Amount" editable="true">
<mx:itemEditor>
<mx:Component>
<mx:TextInput restrict="0-9"/>
</mx:Component>
</mx:itemEditor>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
<mx:CheckBox x="130" y="54" label="Checkbox" selected="true" click="Alert.show(abc.selected.toString())" id="abc"/>
<mx:Script>
<![CDATA[
import mx.controls.CheckBox;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
public var dp:ArrayCollection = new ArrayCollection();
public function addRow():void
{
var tmp:Object = new Object();
tmp['amount'] = 100;
tmp['date'] = new CheckBox();
dp.addItem(tmp);
}
public function delRow():void
{
if(dgFee.selectedIndex != -1)
dp.removeItemAt(dgFee.selectedIndex);
}
public function loop1():void
{
for(var i:int=0;i<dp.length;i++)
{
Alert.show(dp.getItemAt(i).date);
}
}
]]>
</mx:Script>
<mx:Button x="29" y="89" label="AddRow" click="addRow()"/>
<mx:Button x="107" y="89" label="DelRow" click="delRow()"/>
<mx:Button x="184" y="89" label="CheckDg" click="loop1()"/>
</mx:Canvas>
</mx:Application>
You are not supposed to assign objects to data variables but data. Checkbox.select property is set to your check box object first and then true or false after the preceding actions. Try this instead
public function addRow():void
{
var tmp:Object = new Object();
tmp['amount'] = 100;
tmp['date'] = false; // not new CheckBox();
dp.addItem(tmp);
}
PS: Also dp should be attributed with [Bindable] :-)
When you click on the check box in the grid, it writes "true" or "false" into the date field, replacing the original CheckBox object that was there. I believe what itemEditors (you are using your render as an editor) do is they write the .data property from the respective components into the collection.
Set the 'editable' property for that particular datagrid column as false. This will resolve the issue

I need to get the particular values in Flex DataGrid Column

Let me explain the situation...
dg.selectedItems gives me all the values selected, but if i need one column names value alone then what should i do.
<mx:DataGridColumn dataField="1" headerText="Email" />
dg.selectedItems[itemIndex]["columnName"]
itemIndex = the index of the selected item in the array
note: this is if you have allowMultipleSelection = true... if you are only selected a single item/row use dg.selectedItem. This will return an object of all the columns.
Added full working code example below:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >
<mx:Script>
<![CDATA[
import mx.events.ListEvent;
import mx.events.ItemClickEvent;
import mx.collections.*;
[Bindable]
private var dp:ArrayCollection = new ArrayCollection( [
{Artist:'Pavement', Album:'Slanted and Enchanted', Price:11.99},
{Artist:'Pavement', Album:'Brighten the Corners', Price:12.99},
{Artist:'Miley Cyrus', Album:'Break Out', Price:10.99}] );
private function clickItemHandler(event:ListEvent):void{
if(myGrid.selectedItem != null){
trace(myGrid.selectedItem["Album"]);
}
}
]]>
</mx:Script>
<mx:DataGrid id="myGrid" width="350" height="200"
dataProvider="{dp}" itemClick="clickItemHandler(event);" >
<mx:columns>
<mx:DataGridColumn dataField="Album" />
<mx:DataGridColumn dataField="Price" />
</mx:columns>
</mx:DataGrid>
</mx:Application>

How to find checkbox value ( checked/unchecked) in iteamreander of data grid ?

I have 3 checkboxes for calculating amount purpose. I used Datagrid within datgrid used
<mx:DataGrid>
<mx:itemRenderer>
<mx:Component>
<mx:CheckBox id=mycheckbox change="calc()"/>
</mx:Component>
</mx:itemRenderer>
...
public function calc():void
{
statistic.dataProvider = mycheckbox.selectedItem;
}
but it's throws error like Call to possibly undfined method (calc)
You can't give the checkbox an id the way you have done and expect it to behave as a single component.
When you specify the checkbox as an item renderer for a column you are not talking about a single checkbox.
You will be dealing with as many check boxes as there are rows in the datagrid.
The following example shows you how to determine if the checkbox in a particular row is selected or not
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
private var ac:ArrayCollection=new ArrayCollection([
{name: "John", test: true},
{name: "Joe", test: false}]);
private function init() {
dg.dataProvider=ac;
}
public function check():void {
var obj:Object=dg.selectedItem;
Alert.show("Checkbox=" + obj.test);
}
]]>
</mx:Script>
<mx:DataGrid id="dg"
dataProvider="{ac}"
click="check()">
<mx:columns>
<mx:DataGridColumn dataField="name">
</mx:DataGridColumn>
<mx:DataGridColumn>
<mx:itemRenderer>
<mx:Component>
<mx:CheckBox label="Test"
selected="{data.test}"/>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
</mx:Application>
Sometimes flex seems to have trouble updating the data provider for the datagrid when you have a nested itemrenderer. You can explicitly set the appropriate property of the dataprovider row when the change event occurs on the checkbox as below;
<mx:itemRenderer>
<mx:Component>
<mx:CheckBox label="Test" selected="{data.test}"
change="data.test=selected"/>
</mx:Component>
</mx:itemRenderer>
A checkbox does not have a "selectedItem" function or property...
mycheckbox.selected will return true or false based on whether or not sed checkbox is checked
Not really sure what you're trying to accomplish by setting a dataprovider to true or false, seems to me like you want to use a RadioButtonGroup
use outerDocument.functionname inside itemrenderer, and set the function as public. This is a limitation of Flex, Hierarchy mismanagement.

Resources