将对象数组分配给DataGrid

将对象数组分配给DataGrid

本文介绍了将对象数组分配给DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当DataGrid的dataProvider是一个对象数组时,我该如何设置每个列的dataField为该对象的属性。



我有一个ArrayCollection a)每个项目是一个对象
例如 a [i] = data:Object
其中对象数据有一些子属性 - data.name data.title data.content / b>

我有一个DataGrid,我想在其中显示这个数据。

所以我把:

 < mx:DataGrid id =entriesdataProvider ={resultRSS}> 
< mx:columns>
< mx:Array>
< mx:DataGridColumn headerText =TitledataField =data.title/>
< mx:DataGridColumn headerText =DatedataField =data.created/>
< / mx:Array>
< / mx:columns>
< / mx:DataGrid>

这似乎不起作用。我得到一个空的DataGrid。我应该如何分配dataField属性,以便它正确显示?我也试过 {data.title}
$ b $ p

谢谢。

编辑:我的数据示例

   -  [] arraycollection 
- [0]
---- id =id1
----数据。
------ title =something
------ name =something
------ text =some html
- [1]
---- id =id2
----数据。
------ title =something2
------ name =something2
------ text =some html2

和表应该是

  | title | name | text | 
=================================
| something | something | some html |
| something2 | something2 | somehtml2 |


解决方案

这里是您的答案

 <?xml version =1.0encoding =utf-8?> 

< 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:columns>
< mx:AdvancedDataGridColumn headerText =textdataField =text/>
< mx:AdvancedDataGridColumn headerText =descdataField =desc/>
< / mx:columns>
< / mx:AdvancedDataGrid>

< / mx:应用程序>

edit - ok现在根据您的数据放弃我以前的答案试试这个

  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);
.push(oj1);


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

]]>
< / mx:Script>



< mx:columns>
< mx:AdvancedDataGridColumn headerText =COMPANIESdataField =datalabelFunction =some_labelFunc/>
< / mx:columns>
< / mx:AdvancedDataGrid>

< / mx:应用程序>

试试这个糟糕的代码抱歉


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 objectFor example a[i] = data:ObjectWhere 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|
解决方案

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

这篇关于将对象数组分配给DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:41