Object to arraycollection then to datagrid - apache-flex

Say, linedataColl is an AC that contains 200+ of rows extract from CSV and in my design, I wish to additem into SuperDataCollection object by object but the only problem was I'm unable to see any data display in "S" which is a datagrid. What wrong with my code?
var superDataCollection:ArrayCollection = new ArrayCollection();
var dc:ArrayCollection = new ArrayCollection();
var di:Object = new Object();
for(var aa:int=0; aa<5;aa++){
di.username = linedataColl[aa].username;
di.email = linedataColl[aa].email;
dc.addItem(di);
superDataCollection.addItem(dc);
s.dataProvider = dc;
}

Don't set the dataProvider within the for loop. You only need to set it once, and the datagrid will detect changes to the ArrayCollection you specify as a dataProvider.
The best thing to do is set it after you completely build up the ArrayCollection 'dc'.
Perhaps your problem will be fixed by this...
}
s.dataProvider = dc;

Related

DataGrid not appropriate dataProvider value

I have an ArrayCollection filled with 'o' objects. This AC should be the dataProvider of a DataGrid. After setting the dp: dgMT.dataProvider=acDataGrid the DataGrid contains only the last item of the arrayCollection.
Code:
[Bindable]
public var acDataGrid:ArrayCollection = new ArrayCollection();
protected function ddlLanguage_changeHandler(event:IndexChangeEvent):void{
gcTranslate.headerText=Globals.acLanguages.getItemAt(ddlLanguage.selectedIndex,0).toString();
Globals.acActValues=convertXmlToArrayCollection(File.applicationDirectory.resolvePath("xmls"+File.separator+Globals.acFileNames.getItemAt(ddlLanguage.selectedIndex,0)));
Globals.acDataGrid.removeAll();
var o:DataGridObject = new DataGridObject();
var i:int=0;
var angol:Object;
for each(angol in Globals.acValues){
o.en=angol.value;
o.name=angol.name;
if(i<Globals.acActValues.length && o.name==Globals.acActValues.getItemAt(i,0).name){
o.translation=Globals.acActValues.getItemAt(i,0).value;
}
else{
o.translation="";
Globals.acActValues.addItemAt("",i);
}
acDataGrid.addItemAt(o,i);
trace("NAME: "+acDataGrid.getItemAt(i,0).name+" VAL:"+acDataGrid.getItemAt(i,0).en+"TRANS: "+acDataGrid.getItemAt(i,0).translation);
// the values are different!
i++;
}
dgMT.dataProvider=acDataGrid;//setting the dataProvider
}
How could I reach that the DataGrid's rows are filled with the correct values?
Thank you!
You're instantiating o just once, outside of the for loop. Which means your just changing the values of that instance's properties and adding that same instance to the dataprovider over and over. You should instead create a new instance on every iteration.
To fix this, simply move the instantiation of o inside the for loop:
for each(angol in Globals.acValues){
var o:DataGridObject = new DataGridObject();
...
}

How to covert a flex datagrid with XMLList dataprovider to pdf using AlivePDF?

My flex data grid has a XMLListCollection as its data provider.
AlivePDF's Grid expects data grid's data provider to be an array.
Is there a way to PDF this table without going through each and every XML node and populating an array of objects to feed to AlivePDF's Grid?
Are there any other better solutions that I can try out to covert this table to PDF?
Thanks!
ListCollectionView (XMLListCollection base class) has toArray() method
So, just new Grid(dataProvider.toArray(),...)
Here is a sample code snippet that may help you :
Here dataGrid has 2 columns. And chilledWaterLoopXml is the xml containing the data..
var gridColumnFirstcol:GridColumn = new GridColumn('Design ΔT','designDelta', 40, Align.LEFT, Align.LEFT);
var gridColumnSecondcol:GridColumn = new GridColumn('Chilled Water', 'chilledWtrPump', 40, Align.LEFT, Align.LEFT);
var result:ArrayCollection=new ArrayCollection();
for(var i:int =0;i<chilledWaterLoopXml.children().length();i++)
{
var resultant:Object = new Object();
resultant["designDelta"]=chilledWaterLoopXml.child(i).designDelta;
resultant["chilledWtrPump"]=chilledWaterLoopXml.child(i).chilledWtrPump;
result.addItem(resultant);
}
var grid:org.alivepdf.data.Grid = new org.alivepdf.data.Grid( result.toArray() , 100,50, new RGBColor(0xD4CCC5), new RGBColor(0x66C66C),0, new RGBColor(0x000000),1,Joint.ROUND);
grid.columns = columns;
pdf.addGrid(grid);

datagrid cannot be found

I create each datagrid to be added to the NavigatorConent(), however, how do I retrieve the datagrid by ID so that I can point the ArrayCollection to datagrid's dataprovider?
private var pdg:String;
private function stabAdd():void {
var dg1:DataGrid = new DataGrid();
var cn:NavigatorContent = new NavigatorContent();
stab.addElement(cn);
cn.name = "nc"+nu;
dg1.id = "nc"+nu;
pdg = dg1.id;
dg1.addEventListener(MouseEvent.CLICK,cc);
nu++;
This will throw an error which pdg cannot be found, I wonder why:
trace(DataGrid(pdg));
The purpose of nu++ is to assign a
unique name (dg1, dg2, etc) to each
datagrid so that I can assign AC to
that datagrid's dataprovider
I can respect the need to give every component a unique name. The appropriate way to do that in ActionScript is not to specify the id/name field of the component, but rather to create an instance of the component as a variable. Something like this:
protected var myGrid : DataGrid;
And you can now access myGrid anywhere in the component, or in it's children, without creating some complicated scheme. If you need multiple DataGrid's you can store them in an array:
protected var myGridArray : Array = new Array();
And somewhere later in your code--probably createChildren() do something like this:
loop
var newGrid : DataGrid = new DataGrid()
myGridArray.push(newGrid);
end loop
For the most part, this is how all the Flex list based components do it with itemRenderers. They have an array of visible renderers.
As stated in #J_A_X_ comments, you are trying to convert pdg--a string--into a DataGrid. I would expect that to return a null value, as Flex casts tend to fail quietly.
If you want more help, you'll have to tell us the explicit error that you're receiving, possibly with line numbers and more code.

Problem converting Array to Arraylist for dataProvider

private var MealsListResult:ArrayList = new ArrayList;
protected var _data:resultData = new resultData;
private function resultHandler():void
{
var Meals:Array = _data.Meals;
MealsListResult = _data.Meals as ArrayList;
MealDataGrid.dataProvider = Meals;
MealListView.dataProvider = MealsListResult;
}
Should this be working? the MealDataGrid is populating based on the array, but I am debugging and MealsListResult is null. but _data.Meals is not and I dunno if I'm missing something simple.
I can get it to work by doing it like: var MealsListResult2:ArrayList = new ArrayList(Meals); but I feel as though the first method should be working as well!
(there's mxml list and datagrid and such not shown here of course)
if _data.Meals is its runtime type is an array then _data.Meals as ArrayCollection will failed. but, new ArrayCollection(_data.Meals as Array) will working fine.
CMIIW
i guess your problem is you can't use single object as 2 or more different ui dataprovider.
try to use
MealDataGrid.dataProvider = _data.Meals;
MealListView.dataProvider = ObjectUtils.clone(_data.Meals);
UPDATE:
sorry i miss readed, i though it was ArrayColletion. but all you need to do is the same like ArrayCollection

Unwanted binding

The situation is simple. I have a datagrid that gets its data from a webservice.
When data from the webservice is retrived it calls the following function:
private function onListReg():void
{
arrRegOld = WSAutoreg.list.lastResult as ArrayCollection;
arrReg = WSAutoreg.list.lastResult as ArrayCollection;
dgReg.dataProvider = autoreglist;
}
dgReg is the Datagrid. the arr variables are ArrayCollections defined like so:
private var arrRegOld:ArrayCollection = new ArrayCollection;
[Bindable]
private var arrReg:ArrayCollection = new ArrayCollection;
The intent is when I hit a update button, it compares arrRegOld with arrReg and see if any values have changes. The problem is whenever I change values on the Datagrid it changes on both the dataProvider and on both ArrayCollections.
Does anyone know why is this happening? What should I do so that the binding applies only to one ArrayCollection?
Appreciate any tip.
- Mike
Your lists are sharing the same objects, if you modify the first element from arrReg you will see the modification also in arrRegOld - it is not related to binding. You need to clone the objects. You have several choices:
a) Implement a clone method for your objects (recommended)
b) Use a generic method like this one:
private function clone(source:Object):*
{
var array:ByteArray=new ByteArray();
array.writeObject(source);
array.position=0;
return(array.readObject());
}
and call arrRegOld = clone (arrReg); after arrReg = WSAutoreg.list.lastResult as ArrayCollection;

Resources