I am dragging item from a datagrid and while dragging I could see all of the columns in the selected record, being getting dragged. However I only want to show one column (maybe name or id of the record)? Is there a way to achieve this? Also, could I show an icon or image instead of the record while dragging.
Thanks Guys.
This can be done by extending the DataGrid to expose the DataGridDragProxy property. Check out http://dgrigg.com/blog/2006/11/03/datagrid-drag-image/ for a working example.
The extended DataGrid:
<mx:DataGrid xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.controls.dataGridClasses.DataGridDragProxy;
import mx.core.IUIComponent;
/**
* #public
* class to use as DragProxy image
* set the default value to the standard DataGridDragProxy class
*/
[Bindable]
public var dragProxyImage: Class = DataGridDragProxy;
override protected function get dragImage():IUIComponent
{
var image:IUIComponent = new dragProxyImage();
image.owner = this;
return image;
}
]]>
</mx:Script>
</mx:DataGrid>
Using the DataGrid:
<controls:DataGrid
dataProvider="{dataSource}"
rowHeight="40"
dragEnabled="true"
height="140"
dragProxyImage="com.dgrigg.controls.CustomDragProxy"
allowMultipleSelection="true">
<controls:columns>
<mx:DataGridColumn headerText="Image" dataField="image">
<mx:itemRenderer>
<mx:Component>
<mx:Image source="{data.image}"/>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="Product" dataField="name"/>
<mx:DataGridColumn headerText="Description" dataField="description"/>
</controls:columns>
</controls:DataGrid>
Related
I have a flex datagrid to which I need to add this functionality: user clicks on particular cell, window should pop-up with additional information about the value in that cell. The pop-up might need to be another datagrid with more info.Can u provide flex code for this.I am new to flex.
<!--DGCRenderer.mxml-->
<mx:Label xmlns:mx="http://www.adobe.com/2006/mxml" selectable="true"
click="handleClick()">
<mx:Script>
<![CDATA[
private function handleClick():void
{
CustComponent(listdata.owner).showPopUp(this.data);
}
]]>
</mx:Script>
</mx:Label>
<!-- inside the datagrid in the CustComponent -->
<mx:DataGridColumn dataField="name" headerText="Name" itemRenderer="DGCRenderer"/>
<mx:Script>
<![CDATA[
public function showPopUp(item:Object):void
{
var p:PopUpClassName = PopUpManager.createPopUp(this, PopUpClassName);
p.setItem(item);
}
]]>
</mx:Script>
I have a datagrid with one row intially. When I click on the first row, i.e on key down event, I want another row to be added.
Earlier I had a button, on clicking which I added the row. But now I want the row to be added automatically once I click the first row. How to do that?
I added keydown event to the datagrid, but it threw some error.
Can someone help me? I have given my code here.
I have a text box and a button in the task component. When I click the text box in the task component, I want the addTaskRow function to be called..
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.collections.XMLListCollection;
[Bindable]
private var initDG:ArrayCollection = new ArrayCollection([
{Select:true},
]);
private function addTaskRow(event:MouseEvent):void
{
taskDataGrid.dataProvider.addItem(
{
}
);
taskDataGrid.height += 30;
}
]]>
</mx:Script>
<mx:DataGrid id="taskDataGrid" dataProvider="{initDG}" variableRowHeight="true" editable="true"
width="100%" paddingBottom="1" paddingTop="1" height="47">
<!--mx:DataGrid id="taskDataGrid" dataProvider="{initDG}" rowCount="1" variableRowHeight="true" editable="true" width="100%" keyDown="addTaskRow(event);"-->
<mx:columns>
<mx:DataGridColumn dataField="Select"
editable="true"
rendererIsEditor="true"
itemRenderer="mx.controls.CheckBox"
editorDataField="selected" />
<mx:DataGridColumn dataField="TaskName"
width="220"
editable="true"
rendererIsEditor="true"
itemRenderer="components.taskComponent"/>
<mx:DataGridColumn dataField="TaskId"
itemRenderer="mx.controls.TextInput" />
<mx:DataGridColumn dataField="TaskType"
itemRenderer="mx.controls.TextInput"/>
<mx:DataGridColumn dataField="ProjectWon"
itemRenderer="mx.controls.TextInput"/>
<mx:DataGridColumn dataField="ItemCodeVersion"
itemRenderer="mx.controls.TextInput"/>
</mx:columns>
</mx:DataGrid>
You want it on key press or mouse click? Change keyDown to mouseDown.
but it threw some error.
What error did it throw?
If you use mouseDown of the DataGrid, new row will be added when you click on any of the rows, not just the first row.
http://www.switchonthecode.com/tutorials/adding-dynamic-rows-to-flex-datagrid provides code to do it
In a flex datagrid, by default clicking on column headers does sorting. I want it such that if a user clicks a column header the entire column is selected. I have the datagrid listening for the HEADER_RELEASE event so I know when the column header is clicked.
How can I have the column and header appear highlighted similar to how a row is highlighted when selected?
You can do this by setting backgroundColor of the selected column:
<?xml version="1.0" encoding="utf-8"?>
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.DataGridEvent;
[Bindable]
public var mydata:ArrayCollection;
public function init():void
{
mydata = new ArrayCollection();
mydata.addItem( { a:"John", b:"Smith" } );
mydata.addItem( { a:"Jane", b:"Doe" } );
grid1.addEventListener(DataGridEvent.HEADER_RELEASE, selectColumn);
}
public function selectColumn(event:DataGridEvent):void
{
var selectedColumn:DataGridColumn = grid1.columns[event.columnIndex];
selectedColumn.setStyle("backgroundColor", "0x7FCEFF");
event.stopImmediatePropagation();
}
]]>
</mx:Script>
<mx:DataGrid id="grid1" editable="true" dataProvider="{mydata}" >
<mx:columns>
<mx:DataGridColumn dataField="a" headerText="A" />
<mx:DataGridColumn dataField="b" headerText="B" />
</mx:columns>
</mx:DataGrid>
I have a wee demo (with source) on how to do this on my website Here. Basically, you check to see if the DataGrid sort is the same as the column name in the Item renderer, and if it is, you draw a colored background.
Hope this helps.
Caspar
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 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.