Link to home
Start Free TrialLog in
Avatar of kristian_gr
kristian_grFlag for Norway

asked on

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

Avatar of kristian_gr
kristian_gr
Flag of Norway image

ASKER

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
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.
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

Ok.  Try this.

import mx.utils.ArrayUtil


then later

private function getCdPersonInvolvementHandler(event:ResultEvent):void
{
   this.cdPersonInvolvement_dp = new ArrayCollection(ArrayUtil.toArray( event.result ));
}
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
oops, I didn't read your code carefully enough...try

PersonInvolvement_dp = ArrayUtil.toArray(PersonInvolvement_dp);
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"
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...
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.
Have you tried the ArrayUtil.toArray() with your datafield set to firstName instead of person.firstName?
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
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.
ASKER CERTIFIED SOLUTION
Avatar of asaivan
asaivan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Thanks a lot for your time, and excellent answer
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/