Is it possible to dynamically show row index for DataGrid rows ?
I have a static ArrayCollection which is set to be DataGrid's data provider and I would like to always have row numbering for each populated row (e.g. in a column called "#").
Thanks!
Yes, create a labelFunction that returns the ArrayCollction's getItemIndex for the current object, plus 1. Example
Related
my application needs to allow users to insert rows below the current datagrid row. My solution is to to add a row to the dataproviders collection. This works, but the row does not appear beneath the current row the user clicked on.
The Datagrid has a default sort order (date ASC), which re-orders the data...so this seems to affect the position of the row in the grid.
Any ideas how to fix this?
Two possible answers:
1. define your own sort function that sorts according to item order in dataprovider (i.e. it does nothing), and assign it to the sortFunction property
2. simply comment out the sorting of the data inside the component.
Using a standard Flex DataGrid, is there an easy way to determine the grid row index given the data object or index of the data object in the data provider? On the flip side, is there a way to get the index of an object in the data provider based on the DataGrid row index? Thanks in advance!
Assuming I understand what you're asking for correctly and assuming your dataProvider is an ArrayCollection:
To get the selected row's index in your dataProvider:
var rowIndex:int = myArrayCollection.getItemIndex(myDataGrid.selectedItem);
To get the object in your dataProvider from the DataGrid:
var obj:Object = myDataGrid.selectedItem;
Since DataGrid's recycle their itemRenderers to improve memory performance, there's no specific row index for a given object in your dataProvider. As you scroll and records are no longer visible, those records itemRenderers are reused for new records that scroll into view. You can read up more on itemRenderers and recycling here.
EDIT:
Here's a link to an example on how to filter your data in a dataGrid:
http://www.flex-blog.com/arraycollection-filter-example/
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
In Flex 4 using a pre populated data grid, how can I get or set specific values programatically, IE I wont be using selectedItems etc.
How do I reference the value of a cell in row 4 colum 6 for example.
Please and thank you in advance for your help.
Craig
Cast the dataProvider of the DataGrid to ListCollectionView and use its getItemAt method.
ListCollectionView(dataGrid.dataProvider).getItemAt(requiredRow).appropriateProperty = newValue;
Update: In case the column name is dynamic, you can fetch it using something like:
var data_field:String = dgViewPreview.columns[6].dataField; //for 6th column
ListCollectionView(dataGrid.dataProvider).getItemAt(requiredRow)[data_field] = newValue;
There are two ways to access, based on Grid column DataField Mapping to object property or LabelFunction set to grid column
I pasted the scenario with example here
http://pastebin.com/iwrnHD1c
I have a flex datagrid with 4 columns.I have a comboBox with 4 checkboxes,containing the column names of datagrid as its label.I want the datagrid to display only those columns which are selected in combobox.Can anyone tell me how this filtering of columns in datagrid can be done?
Thanks in advance.
You can manipulate the columns attached to the data grid using the .columns property. Bear in mind that this method is a getter and returns you a copy of the list of columns on the datagrid, so if you manipulate its contents you have to apply those changes back to the data grid using the equivalent setter, e.g.
<mx:DataGrid id="dg" />
in ActionScript code
var columns:Array = dg.column;
columns.push(new DataGridColumn("hello"));
dg.columns = columns;
In your case you could hold your master list of columns in a separate array and push them onto the data grid as the user checks and un-checks them from the list in your comboBox.
Alternatively you can iterate through the column list looking for the ones which are checked in your comboBox and set their .visible property accordingly.
HTH