flex air datagrid itemRenderer - apache-flex

I have a datagrid with custom itemRenderer. When I click in a cell, I get its reference. Now I would like to get the reference of the other column in the sae row.
e.g.
In the datagrid I have clicked in 4th column of the 3rd row, I am getting reference of it no problem in that. Now I would like to get the reference of 1st column of the same row i.e 3rd.
Is there a way?
Thanks.

Your custom item renderers should be data driven. In other words everything you care about should be on the renderer's "data" property. If you need to manipulate another cell, you should manipulate the data for the row, and the cell should update itself.
That's the reason you're having a hard time with that -- there isn't a good way to grab a reference to another cell. There are bad ways, but they're bad :)

don't know if I understand this completely correct but here goes...
put on your datagrid a click event
<mx:DataGrid id="myDatagrid" click="getValues()" dataProvider="{someArrayColl}"/>
let say the first column has the name: 'id_column'
between the script lines:
private function getValues():void{
var first_column_value:String = myDatagrid.selectedItem.id_column;
//if you want the entire row in 1 Array
var the_selected_row:Array = myDatagrid.selectedItem as Array;
}
haven't tested the array statement but it should work. myDatagrid.selectedItem is default an object of an arraycollection.
//you should put a try and catch statement in the getValues function to catch the exception when users click on a headeritem or the datagrid-scrollbar

Related

Datagrid, getting and setting individual cell values

I have Flex 4 DataGrid, what I would like to do is when an cell has been edited,
I would then like to walk through the values of that column and preform math on the values, eg I want to total up certain values.
1) How do I reference individual values of a specific column so that I may set them.
2) How do I then set those values or should I create a new column array and pop it in place of that column.
Please and thank you in Advance.
Craig
var data_field:String = dgViewPreview.columns[6].dataField; //for 6th column
ListCollectionView(dataGrid.dataProvider).getItemAt(requiredRow)[data_field] = newValue;
Thanks to Armagosh for the idea.
You can also listen to CollectionChangeEvent of your dataProvider, check its type, and if this is a PropertyChangeEvent that triggered it - check the name of the property that was changed, and depending on the property perform the calculations. This will work if you change the value not only from dataGrid. And, you would want the calculated values to be marked [Bindable] to have the changes reflected in the UI.
A way to do it in Flex 3 would be to add an event listener on the grid for the ItemEditEnd event. In the handler of that event, you would iterate over your the dataprovider and perform and calculations and updates needed.
When you update the items in your dataprovider, the updated values will be reflected in your grid.
I imagine you would do something similar in Flex 4.
--ron

Flex DataGridColumn with array of objects as data provider

I have a datagrid that uses an array of objects as the data provider. The objects are essentially key/value pairs:
{ foo:"something"}
{ bar:"hello"}
{ caca:"lorem"}
The datagrid has 2 columns. The first column is the key and the second column is the value. Right now my grid looks like:
My dataFormatter function makes sure that depending on the column (i.e. the dataField value) the correct key or value gets printed out. This works fine for displaying. However, as soon as I try and edit the value field it essentially adds a new value into the object with a key of '1'. For example, if I edit the {caca:"lorem"} object it will then contain the value {caca:"lorem",1:"new value"}.
Is there any possible way I can set the DataGridColumn so that when I edit a value it will update the value associated with the key rather than inserting a new value? I've tried using a custom item editor but it still does the insert. It seems like I need to be able to update the 'dataField' with the actual key value but I'm not sure how to do that.
Looks like you need to think about where your data is going to be stored. I would recommend listening for a CollectionEvent.COLLECTION_CHANGE event on your data model. That event object will contain info about what change occurred, and you can make any updates you need to make.

Flex dataGrid how to color empty rows?

My problem is that empty rows (if there are more rows that dataSource items then there are empty rows) look identical to rows binded to dataSource items which are empty (see the difference?).
The only way to know the difference is to hover over them with the mouse, and if they are empty there's no color change, otherwise there's the blue background of the selection..
I want to change the color or in some way hide empty rows, those that are not bound to a dataSource item.
How can I accomplish this?
You can format your DataGrid using ItemRenderer.
The itemRenderer is a display object that get the data from the data provider and display it in the grid.
Writing your own logic can help your specific data display in general. in this case, check for data on the ItemRenderer object creationComplete. it the data is null or empty - display a sign (or whatever).
See this link as reference:
http://blog.flexexamples.com/2007/08/20/formatting-a-flex-datagrid-control-using-a-custom-item-renderer/
Enjoy!
I'm not sure if this is exactly what you are looking for but I cut off my rows at the end of my dataprovider like this:
myGrid.rowCount = myDP.length();
This can of course be modified with some simple logic to have min, max, or if it's a data entry type of grid length()+1.

flex datagrid - item renderers and skipping rows

HI,
I have a datagrid with 6 columns, each with its own item renderer. In the first column i want to be able do a check and see if the column contains some valid data, if not then i want to skip this row and go to the next. In other words i want a way to tell my datagrid to stop processing the rest of the item renderers for the current data object and skip to the next. Any ideas?
I'd say your best bet is to use the filterFunction property on ListCollectionView objects (such as ArrayCollection). This allows you to filter out the objects you don't want to show in your DataGrid before they're displayed in the grid, and should avoid any itemRenderers being processed altogether.
If you still want the "skipped" object to display in the data grid and just change how the item renderers respond to it, then you'll need to write code for that in the renderers.
Inside of the item renderer, you can access the data values of the previous columns. You should examine the listData property available in the item renderer and use your findings to configure how the item renderer should display.
You can find information about the listData here: http://livedocs.adobe.com/flex/3/langref/mx/controls/dataGridClasses/DataGridListData.html
To examine previous values, you might code something like this:
var dgListData:DataGridListData = DataGridListData( listData );
// Process all columns before the current one.
for ( var i:int = 0; i < dgListData.columnIndex; i++)
{
// Do something here to examine previous data
// If we should stop processing based on previous values
// then hide everything inside of this renderer (perhaps
// move to a state name 'empty' that has no children), else
// move to the state that renders something.
currentState = shouldSkipObject ? 'empty' : 'normal';
}
If you want more specific help writing the code inside of the item renderer, please include a sample of what the data looks like inside of the data grid as well as a description of what the item renderer should actually do.

ASP.NETDatagrid, Displaying formatted data

I have a ASPX page where I am rendering a Datagrid with some values.
I am creating BoundCoulmns dynamically in my code behind like the following,
BoundColumn iCustomer = new BoundColumn();
iCustomer.HeaderText = "Customer";
iCustomer.DataField = "CustomerName";
dgridProspList.Columns.Add(iCustomer);
dgridProspList.DataBind();
This will show the CustomerName as I have assigned it to the datafield property. Now I want to do some modification on this CustomerName.ie; I want to pass this "CustomerName" and the return value of the function, i need to assign as the DAtaField. Is there any way to do it?
If I've got the right end of your stick the way you can do this is by using the datagrids ondatabound event. Within this event you will need to pick out the cell with the customer name in it (something like row.cells[3]). From here you should be able to set the contents how ever you want.
Hopefully this will help

Resources