Filling Datagrid from remote http service - apache-flex

I am trying my first flex application. And have a problems adding data from xml http service to datagid.
My xml file looks like this:
<players>
<player>
<name>test</name>
<status>F</status>
<claimed>1</claimed>
</player>
<player>
<name>meta</name>
<status>F</status>
<claimed>1</claimed>
</player>
</players>
First I tried to fill the data in a raw way, so created mxml tag for HTTP service, and added handlers.
But very soon I realized that main application file became unreadable (because of huge amount of code), so I decided to organize it some way.
So decided to replace services with a separate as classes.
My new code looks like this:
MXML:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" creationComplete="main()" height="756" borderColor="#FFFFFF" width="950" top="10" left="10" horizontalAlign="left" verticalAlign="top" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#FCFCFC, #FCFCFC]">
<mx:Panel width="900" height="727" layout="absolute" title="Игра ГО" horizontalAlign="center" horizontalCenter="0" top="10">
<mx:Script>
<![CDATA[
import goclient.ListOfPlayers;
import goclient.usersList;
import goclient.Tester;
import mx.controls.Alert;
// And makes periodical requests to the server
[Bindable]
public var users:ListOfPlayers;
[Bindable]
public var test:Tester;
public function main():void{
test = new Tester();
users = new ListOfPlayers();
}
]]>
</mx:Script>
<mx:DataGrid doubleClickEnabled="true" dataProvider="{users.getPlayersList()}"
x="10" y="157" width="860" height="520" id="userList">
<mx:columns>
<mx:DataGridColumn dataField="claimed" headerText="Was claimed" width="25"/>
<mx:DataGridColumn dataField="name" headerText="Name of the player" />
<mx:DataGridColumn dataField="status" headerText="Status (Free or Busy)" />
</mx:columns>
</mx:DataGrid>
And the service class:
ListOfPlayers.as
package goclient
{
import flash.utils.Timer;
import mx.controls.Alert;
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.mxml.HTTPService;
public class ListOfPlayers
{
public var usersListService:HTTPService;
private var minTimer:Timer = new Timer(100000, 0);
private var playersData:ArrayCollection;
private var person:currentPerson;
public function ListOfPlayers()
{
usersListService = new HTTPService();
usersListService.url = "http://127.0.0.1:8000/go/active/";
usersListService.addEventListener(ResultEvent.RESULT, resultHandler);
//Alert.show("Here");
sendData();
//minTimer.addEventListener(TimerEvent.TIMER, sendData);
//minTimer.start();
}
public function getResp():String
{
return "Resr";
}
public function resultHandler(event:ResultEvent):void
{
//person = new currentPerson(event.result.current.username, event.result.current.img, event.result.current.rank);
playersData = event.result.players.player;
Alert.show("resh");
}
public function sendData():void
{
usersListService.send();
}
public function getPlayersList():ArrayCollection
{
Alert.show(playersData.toString());
return playersData;
}
}
}
The problem is that nothing is shown in the datagrid
I am just a beginner, so please advice what did I wrong with the class

The result function (in ListOfPlayers class) should give the list of players and not the function that is calling the webservice.
What you could do is add this in server class:
[Bindable]
public var playersData:ArrayCollection;
and in your view add also this variable with the bindable tag and set the value add this line in main():
playersData = users.playersData;
then the datagrid dataprovider is "{playersData}"
this should work. But with XML list it is always a bit difficult to know how deep you are in the tree ;)

Related

Unable to retrieve property in main from child popup using event in FlashBuilder

I am using a component to display the popup and using an event listener to get popover properties and remove the popup in the Parent. The poup var, however, in the listeners popup var is nul so it throws an error.
Any suggestions would be greatly appreciated.
John
Here is my EditStudentLogInForm.mxml component..
<?xml version="1.0"?>
<!-- containers\layouts\myComponents\MyLoginForm.mxml -->
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="handleCreationComplete();">
<mx:Script>
<![CDATA[
import mx.managers.PopUpManager;
[Bindable] public var studentLoginEmail:String;
]]>
</mx:Script>
<mx:Form width="333">
<mx:FormItem label="Email">
<mx:TextInput id="username" width="207"/>
</mx:FormItem>
<mx:FormItem label="Password">
<mx:TextInput id="password"
width="205"/>
</mx:FormItem>
</mx:Form>
<mx:HBox>
<mx:Button id="okButton" label="OK"/>
<mx:Button id="cancelButton" label="Cancel" />
</mx:HBox>
</mx:TitleWindow>
Here is the Parent...
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:s="library://ns.adobe.com/flex/spark">
<mx:Script>
<![CDATA[
import flash.events.Event;
import mx.managers.PopUpManager;
import mx.core.IFlexDisplayObject;
import EditStudentLogInForm;
import mx.containers.TitleWindow;
public var helpWindow:EditStudentLogInForm;
public function showLogin():void {
// Create the TitleWindow container.
var helpWindow:EditStudentLogInForm = EditStudentLogInForm(
PopUpManager.createPopUp(this, EditStudentLogInForm, true));
helpWindow.username.text = "johnbdh#myserver.com";
helpWindow["cancelButton"].addEventListener("click", removeMe);
helpWindow["okButton"].addEventListener("click", submitData);
}
// OK button click event listener.
private function submitData(event:Event):void {
testText.text = helpWindow.username.text;
//*********helpWindow is nul*******
removeMe(event);
}
// Cancel button click event listener.
private function removeMe(event:Event):void {
PopUpManager.removePopUp(helpWindow);
}
]]>
</mx:Script>
</mx:Application>
When you do
public function showLogin():void {
var helpWindow:EditStudentLogInForm = ...
}
you're declaring and instantiating a new variable helpWindow inside the scope of the showLogin method. This means that the instance you assigned to this locally scoped variable can not be accessed outside the showLogin method.
You did declare another variable helpWindow on the class scope (your class being the main application in this case), but you're never assigning any instance to it (since you're assigning this popup instance to the helpWindow variable that lives only in showLogin.
Hence when you try to access this variable in another method, it's value is null.
The solution is simple enough: just assign the popup instance to the class-scoped variable:
public function showLogin():void {
helpWindow = EditStudentLogInForm(
PopUpManager.createPopUp(this, EditStudentLogInForm, true)
);
...
}
On a side note: if you have a variable of the same name on the class and inside a method, the most locally scoped one always takes precedence:
public var s:String = 'class';
public function myMethod():void {
var s:String = 'method';
trace(s); // prints method
trace(this.s); // prints class
}
public function myOtherMethod():void {
trace(s); // prints class
trace(this.s); // prints class
}

dispatchEvent and eventListener

MENU - RightNavigation
<fx:Metadata>
[Event(name="interval", type="flash.events.Event")]
<fx:Metadata>
[Bindable]
public var sInterval:String;
[Bindable]
public var sIntervalId:String;
protected function intervalSelected(event:MouseEvent):void
{
sInterval = intervalMenu.selectedItem.intervals_miles;
sIntervalId = intervalMenu.selectedItem.interval_id;
dispatchEvent(new Event("interval"));
}
MENU - RightNavigation = This are the buttons in the menu
<s:VGroup includeIn="iMenu" width="100%" height="100%" horizontalAlign="center" paddingTop="10">
<s:List id="intervalMenu" styleName="leftNavContent" creationComplete="miles_handler(event)"
itemRenderer="renderers.MilesItemRenderer" click="intervalSelected(event)" >
<s:AsyncListView list="{intervalsResult.lastResult}"/>
</s:List>
</s:VGroup>
MODULE - mcIntervals
initialize="init()"
import containers.RightNavigation;
import mx.binding.utils.ChangeWatcher;
import flash.events.*;
[Bindable]
public var interval:RightNavigation;
public function init():void
{
//addEventListener("interval", intervalServices);
ChangeWatcher.watch(interval, "sIntervalId", intervalServices);
}
protected function intervalServices(e:Event):void
{
Alert.show("test");
}
Application Setup
MainApp has two containers RightNavigation and MainContent
MainContent has a module called mcIntervals
So I'm trying to send value from RightNavigation to mcIntervals
This is a desktop application if this make any difference
This is not working I can see that is sending the value in debug mode but ChangeWatcher or evenListener are not detecting anything
Thanks, Robert

Displaying images/icons besides y-axis in Flex Bar charts

I am trying to place an image besides the label in y-axis. So I have created a custom label renderer(A HBox containing and ). The source for the image has to be set based on a property present in the data provider. The problem is, I am not able to access the BarSeriesItem in the fnSetSource() method. Any help or pointers is greatly appreciated.
Here's the entire code.
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
width="1280" height="750">
<mx:Script><![CDATA[
import mx.collections.ArrayCollection;
import mx.charts.series.items.PlotSeriesItem;
import mx.controls.Label;
import mx.controls.Image;
import mx.containers.HBox;
import mx.charts.series.items.BarSeriesItem;
import mx.charts.series.ColumnSeries;
import mx.charts.series.items.ColumnSeriesItem;
import mx.charts.chartClasses.Series;
import mx.charts.ChartItem;
[Bindable]
public var employeedetails:ArrayCollection = new ArrayCollection([
{rank:"10",emplName:"Peter",prevRank:"7",imgSource:"images/increase.png"},
{rank:"9",emplName:"Mark",prevRank:"3",imgSource:"images/decrease.png"},
{rank:"8",emplName:"Eric",prevRank:"8",imgSource:"images/decrease.png"}
]);
]]>
</mx:Script>
<mx:BarChart id="bar" height="100%"
paddingLeft="15" paddingRight="5"
showDataTips="true" width="847"
dataTipMode="multiple" >
<mx:verticalAxis>
<mx:CategoryAxis id="v1" categoryField="emplName" dataProvider="{employeedetails}"/>
</mx:verticalAxis>
<mx:verticalAxisRenderers>
<mx:AxisRenderer placement="left" axis="{v1}">
<mx:labelRenderer>
<mx:Component>
<mx:HBox width="100%" height="100%" minWidth="120" minHeight="20">
<mx:Image id="axisImage" height="16" width="16" source="fnSetSource()">
<mx:Script><![CDATA[
import mx.charts.chartClasses.Series;
import mx.charts.ChartItem;
import mx.charts.series.items.BarSeriesItem;
[Bindable]
public function fnSetSource(element : ChartItem, series : Series) : String
{
var data : BarSeriesItem = BarSeriesItem(element);
var imgSrc : String = "";
if (data.item.isIncrease)
{
imgSrc = "images/increase.png";
} else if (data.item.isDecrease)
{
imgSrc = "images/decrease.png";
}
else
{
imgSrc = "";
}
return imgSrc;
}
]]></mx:Script>
</mx:Image>
<mx:Label id="axisLabel" fontSize="12" width="100%" height="100%">
<mx:Script><![CDATA[
[Bindable]
override public function set data(value : Object) : void
{
if (value == null)
{
return;
}
var length : int = value.text.toString().length;
if (length > 15)
{
axisLabel.text = value.text.toString().substr(0, 15) + "...";
axisLabel.toolTip = value.text;
}
else
{
axisLabel.text = value.text;
}
}
]]>
</mx:Script>
</mx:Label>
</mx:HBox>
</mx:Component>
</mx:labelRenderer>
</mx:AxisRenderer>
</mx:verticalAxisRenderers>
<mx:series>
<mx:BarSeries id="bs2"
yField="emplName"
xField="rank"
displayName="Rank"
dataProvider="{employeedetails}"
/>
</mx:series>
</mx:BarChart>
</mx:Application>
I had a quick look at the code. The fnSetSource() function will not be called until it is placed inside curly brackets: source="{fnSetSource()}"
This will get the function to be called, but you will get errors because the fnSetSource() call does not have the 2 parameters that fnSetSource function requires. Make the first change I mentioned and you may be able to figure it out from there.
Could you not make an item renderer that is a seperate mxml component?
look like you are not passing any parameters that fnSetSource() needs so it will not work untill you pass those two parameters..
use your function this way
fnSetSource(value:Object,previousValue:Object,axis:IAxis):String

Dynamic Spark DropDownList ItemRenderer within Flex Datagrid

I have a datagrid which contains a Spark dropdownlist that needs to obtain dynamic data. The datagrid uses a separate dataProvider.
When I use a static ArrayCollection within my ItemRenderer, it works (please see listing 1).
However, when I use Swiz to mediate a 'list complete' event to load the ArrayCollection, the dropdownlist does not show the new data (please see listing 2).
Using the debugger, I inspected the dropdownlist ItemRenderer and have confirmed the new data is being loaded into the ArrayCollection. The new data is not shown in the UI control. I have tried invalidateProperties() + validateNow() and dispatching events on both the control and the renderer (this), but nothing seems to make the new data appear in the control on the datagrid.
Please help !!!
Listing 1: Datagrid and static ArrayCollection (this works):
<mx:DataGrid x="10" y="25" width="98%" id="dgInventory" paddingLeft="25" paddingRight="25" paddingTop="25" paddingBottom="25"
editable="true"
itemClick="dgInventory_itemClickHandler(event)" dataProvider="{acInventory}"
creationComplete="dgInventory_creationCompleteHandler(event)"
height="580">
<mx:columns>
<mx:DataGridColumn headerText="Item" dataField="itemName" itemRenderer="components.ItemRendererItem"
rendererIsEditor="true" editorDataField="selection" editable="true"/>
<mx:DataGridColumn headerText="Quantity Required" dataField="quantityReq" itemRenderer="components.ItemRendererQuantityRequired"
rendererIsEditor="true" editorDataField="selection" editable="true"/>
</mx:columns>
</mx:DataGrid>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import spark.events.IndexChangeEvent;
public var selection:int;
[Bindable]
protected var acItem:ArrayCollection = new ArrayCollection(
[ { itemName: "Item1"},
{ itemName: "Item2"},
{ itemName: "Item3"},
]);
//
protected function dropdownlist1_changeHandler(e:IndexChangeEvent):void
{
selection = e.newIndex;
}
]]>
</fx:Script>
<s:DropDownList id="ddlItem" prompt="Select Item" dataProvider="{acItem}" labelField="itemName"
selectedIndex="{int(dataGridListData.label)}"
change="dropdownlist1_changeHandler(event)"
width="80%" top="5" bottom="5" left="5" right="5"/>
Listing 2: Dynamic ArrayCollection (does not work):
<?xml version="1.0" encoding="utf-8"?>
<s:MXDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
focusEnabled="true">
<fx:Script>
<![CDATA[
import event.ItemEvent;
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
import spark.events.IndexChangeEvent;
public var selection:int;
//
[Bindable]
protected var acItem:ArrayCollection = new ArrayCollection();
//
protected function dropdownlist1_changeHandler(e:IndexChangeEvent):void
{
selection = e.newIndex;
}
//
protected function ddlItem_creationCompleteHandler(event:FlexEvent):void
{
var eve : ItemEvent = new ItemEvent( ItemEvent.LIST_ITEM_REQUESTED );
dispatchEvent( eve );
}
//
[Mediate( event="ItemEvent.LIST_ITEM_COMPLETE", properties="acItem" )]
public function refreshness( _acItem : ArrayCollection ):void
{
acItem.removeAll();
var len:int = _acItem.length;
if (len > 0)
{
for (var i:int=0; i < len; i++)
{
var newItem:Object = new Object;
newItem["itemName"] = _acItem[i].itemName;
acItem.addItem(newItem);
}
}
this.invalidateProperties();
this.validateNow();
//dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
}
]]>
</fx:Script>
<s:DropDownList id="ddlItem" prompt="Select Item" dataProvider="{acItem}" labelField="itemName"
selectedIndex="{int(dataGridListData.label)}"
creationComplete="ddlItem_creationCompleteHandler(event)"
change="dropdownlist1_changeHandler(event)"
width="80%" top="5" bottom="5" left="5" right="5"/>
</s:MXDataGridItemRenderer>
After re-reading Peter Ent's ItemRenderer series, this turned out to be quite simple.
I extended DataGrid to have the ArrayCollection property I needed, then added this to my renderer:
[Bindable]
protected var acItem:ArrayCollection = new ArrayCollection();
//
override public function set data( value:Object ) : void
{
super.data = value;
acItem = (listData.owner as MyDataGrid).itemList; // get the data from the renderer's container (by extending it to add a property, if necessary)
}

Dynamic datagrid Httpservice

I receive from the httpservice with a certain frequency a string like this:
1#3#234525234
where
row#column#value
I want to display into datagrid the above string at real time; at the moment my code displays the whole string, composed by many strings like the one above.
How can i solve the problem?
Thanks in advance
I have the following code
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
xmlns="*" creationComplete="srv.send()" >
<mx:Script>
<![CDATA[
import mx.effects.effectClasses.AddItemActionInstance;
import mx.effects.AddItemAction;
import mx.collections.ArrayCollection;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.events.*;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.InvokeEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.AsyncRequest;
import mx.rpc.AsyncResponder;
import mx.rpc.AsyncToken;
import mx.rpc.AbstractInvoker;
import mx.controls.Alert;
import mx.core.Container;
import mx.core.IDataRenderer;
import mx.controls.dataGridClasses.DataGridItemRenderer;
import mx.controls.DataGrid;
import flash.display.DisplayObject;
[Bindable]
public var i:Number;
public var source:String;
[Bindable]
public var row:Array;
public var column:Array;
public var value:Array;
public function cycle(source:String):void
{
var data:Array = source.split('#');
i=0;
for each(data in source)
{
row[i]=data[i]
column[i]=data[i+1]
value[i]=data[i+2]
i=i+3
}
}
]]>
</mx:Script>
<mx:HTTPService
id="srv"
url="http://10.15.20.75/server4flex/servlet/Datagen"
method="GET"
/>
<mx:TextArea text="{cycle(srv.lastResult.toString())}" x="10" y="50"
width="699" height="59"/>
<mx:AdvancedDataGrid dataProvider="{source}" liveScrolling="true" id="dg"
x="10" y="117" width="621">
<mx:columns>
<mx:AdvancedDataGridColumn dataField="{row[i]}"
headerText="Riga"/>
<mx:AdvancedDataGridColumn dataField="{column[i]}"
headerText="Colonna"/>
<mx:AdvancedDataGridColumn dataField="{value[i]}"
headerText="Valore"/>
</mx:columns>
</mx:AdvancedDataGrid>
First of all, the loop seems to be wrong, you iterate over a string source, not an array.
Secondly, your grid should bind to some properties of objects stored in a dataProvider (Array, ArrayCollection, etc.). So the data provider shouldn't be a string, as in your code.
Thridly, dataField is the name of the field of the object stored in the dataProvider, which value should be displayed in the grid cell.
Basicly, you need to parse your incoming string, store each row in an object and store all these objects in a collection.
So, the code should be something like:
[Bindable]
private var dataList:ArrayCollection;
public function cycle(source:String):void
{
var ac:ArrayCollection = new ArrayCollection();
for(var i:int = 0; i < data.length; i += 3) {
var dataObj:Object = {row: data[i], column: data[i+1], value: data[i+2]};
ac.addItem(dataObj);
}
dataList = ac;
}
<mx:AdvancedDataGrid dataProvider="{dataList}" liveScrolling="true" id="dg"
x="10" y="117" width="621">
<mx:columns>
<mx:AdvancedDataGridColumn dataField="row"
headerText="Riga"/>
<mx:AdvancedDataGridColumn dataField="column"
headerText="Colonna"/>
<mx:AdvancedDataGridColumn dataField="value"
headerText="Valore"/>
</mx:columns>
</mx:AdvancedDataGrid>

Resources