tags:

views:

291

answers:

1

When the dataProvider for an DataGrid is an array of objects, how do I set each column's dataField to a property of the object.

I have an ArrayCollection (say a) where each item is an object For example a[i] = data:Object Where the object data has some subproperties - data.name, data.title, data.content etc.

I have a DataGrid in which I want to display this data.

So I put:

<mx:DataGrid id="entries" dataProvider="{resultRSS}">
  <mx:columns>
<mx:Array>
  <mx:DataGridColumn headerText="Title" dataField="data.title"/>
  <mx:DataGridColumn headerText="Date" dataField="data.created"/>
</mx:Array> 
  </mx:columns>
</mx:DataGrid>

This doesn't seem to work at all. I get an empty DataGrid. How should I assign the dataField property, so that it shows up properly? I've tried {data.title} too.

Thanks.

Edit: sample of my data

-[]arraycollection
--[0]
----id="id1"
----data.
------title="something"
------name="something"
------text="some html"
--[1]
----id="id2"
----data.
------title="something2"
------name="something2"
------text="some html2"

and table should be

 |title     |name      |text     |
 =================================
 |something |something |some html|
 |something2|something2|somehtml2|
+2  A: 

here is your answer

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="initialize()">

<mx:Script>
    <![CDATA[
     import mx.collections.HierarchicalData;

     var a:Array = new Array();
     var o:Object = {};
     private function initialize():void{
       o["text"]="hello";
                  o["desc"]="Rahul";
       a.push(o);
     }

    ]]>
</mx:Script>


    <mx:AdvancedDataGrid width="100%" height="100%" sortExpertMode="true" id="adg1" designViewDataType="tree" dataProvider="{new HierarchicalData(a)}">
     <mx:columns>
      <mx:AdvancedDataGridColumn headerText="text" dataField="text"/>
      <mx:AdvancedDataGridColumn headerText="desc" dataField="desc"/>
     </mx:columns>
    </mx:AdvancedDataGrid>

</mx:Application>

edit - ok now discard my previous answer according to your data try this

var a:Array = new Array();
     var o:Object = {};  
     private function stringArrayToObjectArray():void{
      o["id"]="mauj";
      var oj:Object=new Object();
      oj["title"]="aaa";
      o["data"]=oj;

      var oj1:Object=new Object();
      oj1["id"]="mauj2";
      var oj2:Object=new Object();
      oj2["title"]="qqqq";
      oj1["data"]=oj2;


         a.push(o);  
         a.push(oj1);  
     }

      private function some_labelFunc(item:Object,th:Object):String {
                return item.data.title;
            }

    ]]>
</mx:Script>



    <mx:AdvancedDataGrid  width="100%" height="100%" sortExpertMode="true" id="adg1" dataProvider="{a}">
     <mx:columns>
      <mx:AdvancedDataGridColumn headerText="COMPANIES" dataField="data" labelFunction="some_labelFunc"/>
     </mx:columns>
    </mx:AdvancedDataGrid>

</mx:Application>

try this sorry for such a bad code

Rahul Garg
But the thing is I don't want to display it as a tree. I want to display it as a normal table. Only thing is, the data for the dataField is one object deeper than usual.
trex279
for removing tree type just remove HierarchicalData and add your collection and for data can you provide a sample of your data
Rahul Garg
I have added a sample of my data.
trex279
Thanks. Got it working.
trex279
hey welcome njoy
Rahul Garg