Flex + Dynamic adding Data into List - apache-flex

I'm totally new to Flex (moving away from Java for now :().
I have an XML file, I read the data and want to add it to a List and then use the List to be displayed.
Something like this is what I want to do:
List data = new ArrayList();
data.add(item1);
data.add(item2);
How can I do this, also how will I display the List in a table!!

Your question is rather... vague.
In Flex there are multiple ways to read data from XML and display it.
One approach would be using a HttpService to read the XML.
Then you can loop through the data manually or serve it as a DataProvider for a DataGrid.
I suggest you take a look at the Flex documentation, flexexamples.com and the flex quickstart. Topics that could help you:
http://www.adobe.com/devnet/flex/quickstart/httpservice/
http://www.adobe.com/devnet/flex/quickstart/using_data_providers/
http://blog.flexexamples.com/2007/07/26/displaying-xml-data-in-a-datagrid/

Nerver mind folks, I figured it out myself :)
var hostList : Array=[];
var i: int = numberOfHosts;
var hostNames: XMLList = hostPointXMLList.elements("HostName");
for (i=0; i< hostNames.length(); i++) {
//Alert.show("num="+i+" Hostname="+hostNames[i]);
hostList[i] = hostNames[i];
}

Related

How to print flex spark datagrid by using the mx PrintDataGrid? Or is there other way to achieve this without using the mx PrintDataGrid?

this is my first time to ask here since my question is not answerable in the adobe flex forum. So at least finally, I've come to manage it here.
Well, Flex 4.5+ is I guess everyone is working on nowadays so I asked this question since I haven't yet find working both spark datagrid and the mx printdatagrid components together.
What I'm trying to achieve is to print all the data inside my spark datagrid. So when I do my research, we use the PrintJob or the FlexPrintJob classes. It works fine but when I need to print multiple pages since the data on my spark datagrid is quite long, I can't somehow find a way how to do it. The print output is only upto where the height of my spark datagrid. So in my research, they somehow managed it to use the mx:PrintDataGrid component. But sad to say, they did it with an mx:DataGrid also. Now here comes my problem - how to use the mx:PrintDataGrid along with s:DataGrid.
var printJob:PrintJob = new PrintJob();
if (printJob.start())
{
var printGrid:PrintDataGrid = new PrintDataGrid();
printGrid.width = printJob.pageWidth;
printGrid.height = printJob.pageHeight;
printGrid.columns = mySparkDataGrid.columns; // this line throws an exception
printGrid.dataProvider = mySparkDataGrid.dataProvider;
printGrid.visible = false;
FlexGlobals.topLevelApplication.addElement(printGrid);
while (printGrid.validNextPage)
{
printGrid.nextPage();
printJob.addPage(printGrid);
}
printJob.send();
parentApplication.removeElement(printGrid);
}
So please, can anyone here help me with this? If converting a spark datagrid columns to the mx datagrid columns is not possible, is there a way that I can print all the data on the spark datagrid using multiple pages?
Big thanks in advance.
-Ted
Try somethink like this(it works to me)
for (var i:int = 0; i < mySparkDataGrid.columns.length - 1; i++) {
var tmpColumn:DataGridColumn = new DataGridColumn();
tmpColumn.headerText = (mySparkDataGrid.columns.getItemAt(i) as GridColumn).headerText;
tmpColumns.push(tmpColumn);
}
printGrid.columns = tmpColumns;
Now i have to find way,how use ItemRender.My dataprovider has bigger object and I have to parse it for display correct data in grid.The same I have to do for print.

JSON.NET with GridView

I am trying to find a way bind Grid view to a JSON.NET parsed Linq to Json object
simply am calling
JObject yrs = SearchData("");
then use one property to be bound to Grid view
GridView1.DataSource = yrs["businesses"].Children();
I cant find any resources for something like that
neither binding directly work nor using Children, and seems like I can not find it in the documentation.
appreciate your help
I think I just needed to sleep :)
this is how it should go, if someone need that ,
var bsn =from c in yrs["businesses"].Children()
select new { Title = (string)c["Title"] , Type = (string)c["Type"] };
GridView1.DataSource = bsn;
if there are better ways please advice

Is there a API from Flex/Flash to list all the UIComponent?

I am trying to create an API utility using Adobe AIR where given a component name it should list all it methods, variables, and other properties of that component.
But I could not find an API to list all the UIComponents of Flex/Flash.
is there an API to lookup all the UIComponent or if there is no such API, can anyone tell me how do i lookup the components?
Note: I have tried using flash.utils.describeType() but it is not able to suit my requirement as the input to the method is passed dynamically using TextInput.
~Jegan
It sounds like you are asking two things:
How to list all of the properties/methods of a UIComponent.
How to list all of the UIComponents in the Flex SDK.
The first one can be done with describeType(). Given that the method is passed dynamically using TextInput, you need to just include all of the UIComponents into the SWF. You can do that with the following include statement for each component in the SDK (this example is with the Button):
include mx.controls.Button; Button;.
Then you can do something like this:
var className:String = myTextArea.text; // mx.controls::Button or mx.controls.Button
var clazz:Class = flash.utils.getDefinitionByName(className);
var methods:Array = getMethods(clazz);
var properties:Array = getProperties(clazz); // write a similar method for accessors and variables
/**
* Returns a list of methods for the class.
* Pass in the superclass depth for how many classes
* you want this to return (class tree). If it's -1, it will return the whole method list
*/
public static function getMethods(target:Object, superclassDepth:int = -1, keepGeneratedMethods:Boolean = false):Array
{
var description:XML = DescribeTypeCache.describeType(target).typeDescription;
var hierarchy:Array = getHierarchy(target);
var methodList:XMLList = description..method;
methodList += description.factory..method; // factory is required if target is Class
var methodName:String
var methods:Array = [];
var declaredBy:String;
var methodXML:XML;
var i:int = 0;
var n:int = methodList.length();
for (i; i < n; i++)
{
methodXML = methodList[i];
declaredBy = methodXML.#declaredBy;
methodName = methodXML.#name;
// we break because the xml list is orded hierarchically by class!
if (superclassDepth != -1 && hierarchy.indexOf(declaredBy) >= superclassDepth)
break;
// ignore methods that start with underscore:
if (methodName.charAt(0) == "_" && !keepGeneratedMethods)
continue;
methods.push(methodName);
}
// sort the method list, so there's some kind of order to the report:
methods.sort(Array.CASEINSENSITIVE);
return methods;
}
The second one, listing the UIComponents in the SDK, is a bit more complex because there's no stable list of components in the SDK (depends on version, etc.). What you can do, however, is get a list of every class included in the swf. You do this by generating a Link Report (which is used for helping out modules), which is just a compiler argument:
-link-report=my_report.xml
Then you can figure out a way to find the unique xml nodes in that file, and which ones extend UIComponent. I use ruby and the nokogiri xml parser for ruby to do that. There is also a neat AIR app to visualize the Link Report.
If you're doing the first case, that method should get you started on listing all properties/methods of a UIComponent. If you're doing the second, that will give you a list of all UIComponents included in the swf.
Let me know if that helps,
Lance
Do you need to do anything with the data, other than displaying it? If not, it might be easiest to pull up the right page from Adobe's online API docs.

Flex: Get an item from a AdvancedDataGrid given an index

I've got a subclass of AdvancedDataGrid showing a tree-like data structure. How can I, given the index returned by calculateDropIndex, get the item at that index?
After reading through reams of code, it seems like the least terrible way is:
var oldSelectedIndex:int = this.selectedIndex;
var mouseOverIndex:int = this.calculateDropIndex(event);
this.selectedItem = mouseOverIndex;
var item:* = this.selectedItem;
this.selectedIndex = oldSelectedIndex;
The other option seems to be tinkering around with the iterator property... But, judging by the way I've seen it used, that will get pretty harry pretty quickly too.
So, how can I get the item at a particular index in an advanced datagrid without going insane?
You could try:
// Get the dropIndex from the dragEvent
var index:int = this.calculateDropIndex(event);
// Get the itemRenderer from the index
var renderer:IListItemRenderer = this.indexToItemRenderer(index);
// Get your item from the data property of the itemRenderer
var item:Object = renderer.data;
this.dataProvider.getItemAt(calculateDropIndex(event));
Looking through the docs, it seems you may be able to use openNodes, which returns an Array of all open nodes, which should correspond with your index?
this.dataProvider.openNodes[calculateDropIndex(event)];

Flex: Thread style reply

One of the functionalities in my current flex application requires me to maintain a comments section. Here, the users can post the comments and the replies to existing comments. All I want is like the usual thread style commenting.
Let say, I am replying to someone else's comment, so, it will align my comment by a tab or so and people can easily see the comments and replies.
e.g.
USER ABC : COMMENT 1
-----USER XYZ: RE:COMMENT1
----------USER DEF: RE:RE:COMMENT1
and so on...
Can anyone suggest a way to do this?
Thanks :)
In your database you would store the parentID for each comment. You can query that data and loop through it creating new objects to display the comments however you want.
Store the results in an ArrayCollection
var tmpCanvas:Canvas;
for(var i:int = 0; i < ac.length; i++)
{
// display data
tmpCanvas = new Canvas();
tmpCanvas.x = ...
tmpCanvas.y = ...
// add items to the canvas
this.addChild(tmpCanvas);
}

Resources