Expand my Community achievements bar.

Datagrid not populated programatically

Avatar

Level 2
Hello,



I am trying to populate a datagrid in ActionScript, but am
unable to do. However, if I do it in mxml, I am able to see the
data.



Here is my relevant mxml code.

public var players:ArrayCollection;



private function fillPlayers(event: ArrayCollection):void {

players = event;

mainTxt.text = "Getting filled later";

}



private function displayRackInfo():void {

playMe.getPlayersAsList(); // This fills the players
collection.



var rackInfoGrid:DataGrid = new DataGrid();

rackInfoGrid.dataProvider = players;



var firstName:DataGridColumn = new
DataGridColumn("firstName");

var lastName:DataGridColumn = new
DataGridColumn("lastName");

var rackColumns:Array = new Array(firstName, lastName);



rackInfoGrid.columns = rackColumns;



if (players == null) {

mainTxt.text = "NULL"; // Print NULL in TextArea box

}



if (players != null) {

mainTxt.text = "NOT NULL"; // Print NOT NULL in TextArea box

}



rackInfoPanel.addChild(rackInfoGrid);

PopUpManager.addPopUp(rackInfoPanel, this, true);

}

]]>

</mx:Script>



<mx:RemoteObject id="playMe"
destination="PlayerService">

<mx:method name="getPlayerList"
result="fillPlayers(event.result as ArrayCollection)"/>

</mx:RemoteObject>





PlayerService code is below :



public List getPlayersAsList() {

System.out.println("List method called");

List players = new ArrayList();

Player michael = new Player("Michael","Jordan");

Player scottie = new Player("Scottie","Pippen");

players.add(michael);

players.add(scottie);

return players;

}



The datagrid shows headers, but no data. Also my print
messages show that the remote service is being called. Also in my
action script method, once I get the 'NULL' message and another
time, I get the 'NOT NULL' message.



If I take the datagrid out of ActionScript and populate it in
mxml, it works just fine. I see the entire data.



<mx:DataGrid dataProvider="{players}" height="100"
width="100%">

<mx:columns>

<mx:DataGridColumn dataField="firstName"
headerText="FirstName"/>

<mx:DataGridColumn dataField="lastName"
headerText="LastName"/>

</mx:columns>

</mx:DataGrid>



The reason I am trying to do in ActionScript is because i
want the datagrid to open only when a user event happens. User
clicks a button, and then the datagrid pops up. If I do it in mxml,
I am unable to get that behaviour. Datagrid is visible right from
the start of the application.



Please help.



Thanks.
2 Replies

Avatar

Level 3
I suspect what is happening is that event.result is coming
back as an Array rather than an ArrayCollection. In this case,
event.result as ArrayCollection would return null.



Try taking the "as ArrayCollection" away and in your debugger
take a look at what event.result is coming back as.



Assuming it is an Array, you can convert to to an
ArrayCollection like so:

var collection:ArrayCollection = new
ArrayCollection(event.result);



HTH,

Seth