Flex : AdvancedDataGrid columns dataProvider? - apache-flex

I have a AdvancedDataGrid, defined as:
<mx:AdvancedDataGrid id="mainGrid" width="200" height="200" designViewDataType="flat" >
<mx:columns>
</mx:columns>
</mx:AdvancedDataGrid>
I am adding dynamically the columns to the grid, and the main question is:
How to setup an array or vector as dataprovider to each Column?
P.S.
this function I am using to fill up the data:
private function buildUpTheGrid() : void {
masterColumns = new Array();
masterData = new Array();
var tempColumn : AdvancedDataGridColumn;
for( var iY : int = 0; iY < columsCount; iY++ )
{
masterData.push( new Array() );
tempColumn = new AdvancedDataGridColumn();
tempColumn.width = 20;
for( var iX : int = 0; iX < rowsCount; iX++ )
masterData[ iY ].push( iX );
// tempColumn.dataField = ???
masterColumns.push( tempColumn );
}
mainGrid.columns = masterColumns;
mainGrid.validateNow();
}

Short answer to your question "How to setup an array or vector as dataprovider to each Column ?": You cannot.
The data 'views' in flex are all row based, not column based. So, for this to work, you'll need to transform your columns into rows. It's a fairly simple algorithm that I'm sure you can figure out.
As for adding the columns dynamically, you just need to add the columns to an array and then set that array in the 'columns' property of the grid. You'll need to set a 'dataField' property on the column for it to know which data property to display from the row data you've just transformed.

How to setup an array or vector as dataprovider to each Column ?
In the architecture of a DataGrid, and AdvancedDataGrid individual columns do not have their own, separate, dataProviders. To set the dataProvider on the component, you can do something like this:
<mx:AdvancedDataGrid id="mainGrid" dataPRovider="{myDataProvider}" >
in ActionScript you can set the dataProvider like this:
mainGrid.dataProvider = myDataProvider
If you have data coming from a myriad of different sources that you need to display in a single DataGrid, you should try to combine that data into a single dataProvider; using some common element, such as a database primary key value. Creating a single object to contain each separate element should work. Conceptually something like this:
public class myCombinedObject{
public var commonElement : int;
public var fromDataProvider1 : myCustomDataClass1;
public var fromDataProvider2 : myCustomDataClass2;
}
From there just write itemRenderers for each column to show the relevant data.

Usually we will set the dataprovider to the Datagrid. And in the columns we can set the datafield, which will take the property from the objects of the dataprovider to be displayed in the particular column. Are you aiming to make the columns coming from different arrays? In that case it will be a tricky one.

I've used an Array of Arrays as a dataProvider.
You can just set the dataField property to the index of the "sub Array".
For example:
tempColumn.dataField = iY.toString();
This works, (it fills the column with every value from the the sub array [iY]), but I can't recommend doing this because if the order of your data in the sub arrays changes
it could be become difficult to debug...

Related

Xamarin grid, column and row amounts

Hi im relatively new to c# code and i was wondering if there is any way to get the amount of columns and rows in a grid and store that amount in a variable
Something like:
var columnamount = grid.columnamount;
But i could not find anything that works
Thanks
You can use the following code to get a count of the columns and rows directly via the ColumnDefinitions and RowDefinitions properties. No need to enumerate the children of the grid because you may not have views in every column/row.
var columnCount = grid.ColumnDefintions.Count;
var rowCount = grid.RowDefinitions.Count;
For reference the documentation.
You might be able to do it this way, purely based on what I see in the docs:
var countColumns = grid.Children.Where( c => c.Column).Max();
var countRows = grid.Children.Where( c => c.Row).Max();
But I'm not sure if you can access Row anf Column properties on the child element.
This is not the best way to check, I guess, but it's working (same thing for columns):
EDIT: nope, for columns it doesn't work
int GetRowsCount(Grid grid)
{
var item = grid.Children.FirstOrDefault();
return item != null ? Grid.GetRow(item) + 1 : 0;
}

Java equals string and object from ObservableList

I have a big problem. My code:
TablePosition pos = (TablePosition)
tableView.getSelectionModel().getSelectedCells().get(0);
Object item = tableView.getItems().get(pos.getRow());
Object e = ((List<ObservableList>) item).get(0);
String new_status = "textExample";
and how can I use equals ? new_status.equals(e) ? I don't have any idea on how to convert this.
p.s. in console e = "textExample".
I need this to update a sql row.
If you want to get selected items, you can just use getSelectedItems() (or getSelectedItem() if you only want the latest selection). You don't need to work with selected cells, table positions and rows.
Assuming a TableView<ObservableList<String>> (which you may or may not have). Code to retrieve the value of the first element of the selected row (probably the value represented in the first column), would be as follows:
final String NEW_STATUS = "textExample";
final TableView<ObservableList<String>> tableView = new TableView<>();
final ObservableList<String> selectedItem =
tableView.getSelectionModel().getSelectedItem();
if (selectedItem != null
&& !selectedItem.isEmpty()
&& NEW_STATUS.equals(selectedItem.get(0))) {
// do work here.
}
As your type definitions may be different you may need to adapt the code above to fit your situation as required. If you still can't work it out, you will need to edit the question to provide an mcve to get further assistance.

XtraGrid set selection on match datamember

We want to set the selection on a XtraGrid based on a List collection from the same type as the datamember of an XtraGrid.
The way we do it now is to iterate the gridview rows.
private void SetSelectedRowsInternal(IList<StrongType> collecshung)
{
grdvSomeGrid.ClearSelection();
grdvSomeGrid.BeginSelection();
for (int i = 0;i < grdvSomeGrid.RowCount;i++)
{
StrongType _strongTyped = ((StrongType)grdvSomeGrid.GetRow(i));
if (collecshung.Where(x => x.Id == _strongTyped.Id).Count() == 1)
grdvSomeGrid.SelectRow(i);
}
grdvSomeGrid.EndSelection();
}
Is there a better way to do this?
It depends on how you fill your grid ... You can directly iterate on the BindingList for instance (using LINQ). But if you find that it is taking more time than needed, then it should be because LINQ functions can be hit more than 1 million time even for a small amount of data.
I recommend you to use a dictionary instead of a LINQed collection.

NumberFormat Columns of Datagrid in Flex

I am filling my datagrid using XML as datasource. I need to format the numeric columns with thousands of separators. Thus I have created a <mx:NumberFormatter id="setNumberFormat" .../>
But I fail to use it in the code below. (See the comment //Format Numbers using numberFormatter)
for each(var item in fieldsXMLListNew){
//Copy all columns in to a proxy
var dgColumns:Array = myGrid.columns;
//create a new dynamic Column
var aColumn:DataGridColumn = new DataGridColumn();
// add parameters
aColumn.dataField = '#' + item.attribute("field");
if (item.attribute("data_type") == "N"){ //Numeric Fields Only
//Sort Numeric Columns
aColumn.sortCompareFunction = xmlDataGridNumericSorter(item.attribute("field").toString());
//Format Numbers using numberFormatter
setNumberFormat.format(item.attribute("field").toString());
}
aColumn.headerText = item.attribute("header_text");
//add to proxy
dgColumns.push(aColumn);
//copy all in to Data Grid columns again.
myGrid.columns = dgColumns;
}
Any tweaking to this code so that it takes the number format?
Use labelFunction in each column that you want, assinging the labelFunction:
<s:GridColumn dataField="field" labelFunction="myLabelFunction"/>
The labelFunction:
private function myLabelFunction(item:Object, column:GridColumn):String
{
return setNumberFormat.format(item[column.dataField]);
}

Is swapping the columns and rows of a flex datagrid possible?

I have a 1 row, many column flex datagrid. I would like to turn the dataGrid on its side, so that the column headers become a single column running down and v.v.
Is there a way to do that in the DataGrid?
Or am I stuck manipulating the data presented to the grid? If so whats your recommendation?
The main idea here is I have an object like:
x=y
b=u
o=p
u=e
w=p
And I'd like a control that is visually similar to that. Currently the datagrid displays the object as:
x b o u w
y u p e p
Which is too horizontal for my case. Thx
I presume that you want to convert your columns in to a single column
this can be done by getting all the columns and put the in array as provide it as a dataprovider.
DataGrid.columns
will return the columns.
and you can do some think like this to create columns.
public function createColumns():Array{
var advancedDataGridColumn:AdvancedDataGridColumn;
var i:int;
var columnsArray:Array = new Array();
for(i=0;i<columns.length;i++){
advancedDataGridColumn=new AdvancedDataGridColumn();
advancedDataGridColumn.headerText=columns[i].dispheader.toString();
advancedDataGridColumn.dataField="#"+columns[i].name.toString();
advancedDataGridColumn.itemRenderer=new ClassFactory(Styler);
if(columns[i].descending!=undefined ){
if(columns[i].descending.toString()=="true")
sortField = new SortField("#"+columns[i].name.toString(),false,true,null);
else
sortField = new SortField("#"+columns[i].name.toString(),false,false,null);
}
if(advancedDataGridColumn.headerText == Constants.price||
advancedDataGridColumn.headerText == Constants.quantity||
advancedDataGridColumn.headerText == Constants.askPrice||
advancedDataGridColumn.headerText == Constants.bidPrice||
advancedDataGridColumn.headerText == Constants.netAmount||
advancedDataGridColumn.headerText == Constants.interestAmount||
advancedDataGridColumn.headerText == Constants.principalAmount||
advancedDataGridColumn.headerText == Constants.accruedInterestAmount){
var currencyFormattor:CurrencyFormatter = new CurrencyFormatter();
currencyFormattor.useThousandsSeparator=true;
currencyFormattor.currencySymbol="";
currencyFormattor.thousandsSeparatorFrom=",";
currencyFormattor.thousandsSeparatorTo=",";
advancedDataGridColumn.formatter=currencyFormattor;
}
columnsArray.push(advancedDataGridColumn);
}
return columnsArray;
}
sorry i just copied the code but i think it will help you.
Set the DataGrid to have only 2 columns and transform the original dataset to an array collection of {propName, propValue}.
Say you have:
var originalDataSet : ArrayCollection;
var dataSet : ArrayCollection;
var columnSet : ArrayCollection;
Once you have the original values, you'll do something like:
dataSet = new ArrayCollection();
for (var i : int; i < originalDataSet.length; i++)
{
dataSet.addItem({name : columnSet.getItemAt(i), value : originalDataSet.getItemAt(i)});
}
myDataGrid.dataProvider = dataSet;//set the data provider of the grid to the transformed data set.
To clarify:
{name : columnSet.getItemAt(i), value : originalDataSet.getItemAt(i)}
This creates a new instance of type Object and assigns the name and value dynamic properties to their respective values. Instead of this you might want to define your own class with bindable properties. Note that the property names are just for this example because I don't know what you're working with actually.
The data grid at that point should have two columns defined by you, with their dataField properties set accordingly. Also, this example assumes columnSet collection contains the "horizontal columns" that you want displayed vertically. If you can obtain these based on the values in the originalDataset, you might not even need columnSet.

Resources