Solved

ArrayCollection objects to DataGrid

Posted on 2008-10-04
16
965 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
  • 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now