Solved

ArrayCollection objects to DataGrid

Posted on 2008-10-04
16
971 Views
Last Modified: 2012-05-05
Hi.
I'm having a problem figuring out how to use an ArrayCollection of objects in a DataGrid.
Im having an ArrayCollection with objects - PersonInvolvement.
The PersonInvolvement object has the object Person and the Object InvolvementType.
Now I'd like to create a DataGrid that displays firstName from the object Person.

<mx:DataGrid width="725" height="197" dataProvider="{PersonInvolvement_List}">
    <mx:columns>
      <mx:DataGridColumn headerText="Fornavn" dataField="person.firstName"/>
    </mx:columns>
</mx:DataGrid>

With this I get nothing.
Using dataField="person" - I get Object.object - so the object is there.
Using the debugger I can see that the "PersonInvolvement_List" contains every thing I need.
Any ideas?
package entity { 
	import mx.collections.ArrayCollection;
	[Bindable]
	[RemoteClass(alias="entity.PersonInvolvement")]
	public class PersonInvolvement{
		public var id:int;
		public var person:Person;
		public var involvementType:InvolvementType;
         }
}
package entity { 
   import mx.collections.ArrayCollection;
   [Bindable]
   [RemoteClass(alias="entity.Person")]
   public class Person{
     public var id:int;
     public var firstName:String;
     public var lastName:String;
   }
}

Open in new window

0
Comment
Question by:kristian_gr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 8
16 Comments
 

Author Comment

by:kristian_gr
ID: 22640637
Sorry-
When I use :
<mx:DataGridColumn headerText="Fornavn" dataField="person">
The result is [object Person]

That should indicate that everything is correct.
The only thing I really need is to figure out how display Person.firstName instead of Person
0
 
LVL 9

Expert Comment

by:asaivan
ID: 22641293
You have to assign your resultset from the server or from where ever you're populating your getting your data.

e.g. if you're getting data from a server

var dp:ArrayCollection = new ArrayCollection(PersonInvolvement_List);

But that will vary depending on your set up, you should post some more of your code and I can help you out.
0
 

Author Comment

by:kristian_gr
ID: 22642000
The following code is pritty much whats interesting in the code.

But before you use to much time with it - it works.
The PersonInvolvement_dp is filled with PersonInvolvement objects.
In flex debuger - the PersonInvolvement_dp contains objects of class Person and InvolvementType.

Now, the only thing missing, is how to display: Person.firstName in a column

<mx:DataGridColumn headerText="Fornavn" dataField="person.firstName"/>
is the only part that does not work.
<mx:Script>
<![CDATA[
import mx.events.ListEvent;
import mx.managers.CursorManager;
import mx.managers.PopUpManager;
import mx.core.DragSource;
import mx.controls.Alert;
import mx.containers.Canvas;
import mx.collections.ArrayCollection;
import mx.binding.utils.BindingUtils;
import mx.messaging.Channel;
import mx.messaging.ChannelSet;
import mx.messaging.channels.AMFChannel;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;
import mx.events.DragEvent;
import mx.core.DragSource;
import mx.managers.DragManager;
import mx.controls.Alert;
import mx.containers.TitleWindow;
 
import entity.*;
			
	[Bindable]
	[ArrayElementType("PersonInvolvement")]
	private var PersonInvolvement_dp:ArrayCollection;
 
 
private function getPersonInvolvement():void {
  var ro_:RemoteObject = this.mySession.createNew();
  ro_.addEventListener(ResultEvent.RESULT, getCdPersonInvolvementHandler);
  ro_.getCdPersonInvolvementList(this.track);
}
			
private function getCdPersonInvolvementHandler(event:ResultEvent):void {
  this.cdPersonInvolvement_dp = event.result as ArrayCollection;
}
]]>
</mx:Script>
<mx:DataGrid width="725" height="197" dataProvider="{PersonInvolvement_dp}">
 <mx:columns>
  <mx:DataGridColumn headerText="Fornavn" dataField="person"/>
 </mx:columns>
</mx:DataGrid>

Open in new window

0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 9

Expert Comment

by:asaivan
ID: 22642013
Ok.  Try this.

import mx.utils.ArrayUtil


then later

private function getCdPersonInvolvementHandler(event:ResultEvent):void
{
   this.cdPersonInvolvement_dp = new ArrayCollection(ArrayUtil.toArray( event.result ));
}
0
 

Author Comment

by:kristian_gr
ID: 22644370
ok?
I've now got the same List as an ArrayCollection. But still the same result in my DataGrid?
I can not see the point in chaning it to an ArrayCollection, can you explain.

tnx
0
 
LVL 9

Expert Comment

by:asaivan
ID: 22644839
oops, I didn't read your code carefully enough...try

PersonInvolvement_dp = ArrayUtil.toArray(PersonInvolvement_dp);
0
 

Author Comment

by:kristian_gr
ID: 22644954
Then again why?
PersonInvolvement_dp is allready a fine, functional and correct ArrayCollection.

Do you mean that I should have an Array in stead of an ArrayCollection?
and then do:
<mx:DataGridColumn headerText="Fornavn" dataField="person.firstname">

I've tested that by creating
[Bindable]
private var personInvolvementArray:Array;
then
personInvolvementArray = ArrayUtil.toArray(PersonInvolvement_dp);
and finally:
<mx:DataGrid width="725" height="197" dataProvider="{personInvolvementArray}">
            <mx:columns>
                  <mx:DataGridColumn headerText="Fornavn" dataField="person.firstName"/>
            </mx:columns>
      </mx:DataGrid>

Still nothing is displayed in the DataGridColumn.
When I move the cursor over I see that there is data there. So my DataGrid's dataProvider is correct.
There have to be something with the way I do:
dataField="person.firstName"
0
 
LVL 9

Expert Comment

by:asaivan
ID: 22646089
Did you try the code I gave you?  I don't know why it works, I only know that it works. Try it with the code I gave you...
0
 

Author Comment

by:kristian_gr
ID: 22647354
Yes, I've tried it. But sorry, no change.
<mx:DataGridColumn headerText="Fornavn" dataField="person.firstName"/>
is still empty. Only change I see is that:<mx:DataGridColumn headerText="Fornavn" dataField="person"/>
is allso empty. It is still data in the DataGrid. But nothing in my Column.
0
 
LVL 9

Expert Comment

by:asaivan
ID: 22650942
Have you tried the ArrayUtil.toArray() with your datafield set to firstName instead of person.firstName?
0
 

Author Comment

by:kristian_gr
ID: 22652504
yes. I've tried both
this.PersonInvolvement_dpArray = ArrayUtil.toArray(this.PersonInvolvement_dp);
<mx:DataGrid width="725" height="197" id="personInvolvement" dataProvider="{PersonInvolvement_dpArray}" >
            <mx:columns>
                  <mx:DataGridColumn headerText="Fornavn" dataField="fornavn"/>
            </mx:columns>
      </mx:DataGrid>

Still no result
0
 
LVL 9

Expert Comment

by:asaivan
ID: 22652532
Ok, believe it or not, I've dealt with this problem often.  Can you post more of your code so I can take it and work it out in Flex and then post it back to you?  You can send it to me privately or post publicly if you want, I just need to be able to work on it first hand as there's often many factors that go into this kind of thing...my email is spiritualresponse@gmail.com.
0
 
LVL 9

Accepted Solution

by:
asaivan earned 500 total points
ID: 22653883
Ok, after looking over your code, I realize you are using nested data objects.  DataGrid, as far as I can tell, doesn't support nested data object this way.  A Tree control will, but not a DG.

What you need to do is keep that data in more or less of a flat format, like:

person.firstName = 'etc';
person.lastName = 'foo';
person.personInvolvement = 'whatever';

And have this data in one particular data object.  While you may want to keep your various data objects in a heirarchical format, you can not thus display that data in a datagrid, unless you make a item renderer for the data grid which contains a datagrid in itself.

To make things easier, I would suggest making another data object properties corresponding with the specific fields you want to retrieve for your datagrid, and put that specific data from your other data objects in the data provider.

Does that make sense?
0
 

Author Comment

by:kristian_gr
ID: 22656804
Well - it was not the answer I was looking for - of course - but it explains why it did not work.
Changing the object is not an option, so Ill give the Tree controller a chance.

Thanks a lot for your time, and excellent answer
0
 

Author Closing Comment

by:kristian_gr
ID: 31503004
Thanks a lot for your time, and excellent answer
0
 
LVL 9

Expert Comment

by:asaivan
ID: 22660110
If you want to really dig in, I'd suggest reading this article, which may help you to do what you want to be able to do with your data objects and the DataGrid, but it will take some effort:

http://blog.paranoidferret.com/index.php/2008/04/01/nested-data-in-flex-datagrid-by-extending-datagridcolumn/
0

Featured Post

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Is this doable in Flex/AIR - pointers? 5 285
Keep a player within a circular area 3 268
labelFunction in AdvancedDataGrid FLEX 3 2 2,820
Play RTMP without Flash 3 772
First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
Original post  on Monitis Blog. Web performance monitoring is broken into two camps: passive and active. Passive monitoring is defined as looking at real-world historical performance by monitoring actual log-ins, site hits, clicks, requests for…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question