• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 987
  • Last Modified:

ArrayCollection objects to DataGrid

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
kristian_gr
Asked:
kristian_gr
  • 8
  • 8
1 Solution
 
kristian_grAuthor Commented:
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
 
asaivanCommented:
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
 
kristian_grAuthor Commented:
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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
asaivanCommented:
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
 
kristian_grAuthor Commented:
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
 
asaivanCommented:
oops, I didn't read your code carefully enough...try

PersonInvolvement_dp = ArrayUtil.toArray(PersonInvolvement_dp);
0
 
kristian_grAuthor Commented:
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
 
asaivanCommented:
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
 
kristian_grAuthor Commented:
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
 
asaivanCommented:
Have you tried the ArrayUtil.toArray() with your datafield set to firstName instead of person.firstName?
0
 
kristian_grAuthor Commented:
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
 
asaivanCommented:
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
 
asaivanCommented:
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
 
kristian_grAuthor Commented:
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
 
kristian_grAuthor Commented:
Thanks a lot for your time, and excellent answer
0
 
asaivanCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now