开发者

Problem in loading xml (response from php script) into the datagrid - flex

开发者 https://www.devze.com 2023-02-27 01:35 出处:网络
I\'m trying to learn Flex. I have a question in loading the DataGrid from the XML response (from a PHP script).

I'm trying to learn Flex. I have a question in loading the DataGrid from the XML response (from a PHP script).

mxml code:

<mx:DataGrid id="dataGrid" x="69" y="250"> 
 <mx:columns>
     <mx:DataGridColumn headerText="Name" dataField="name"/> 
     &开发者_Go百科lt;mx:DataGridColumn headerText="Age" dataField="age"/> 
     <mx:DataGridColumn headerText="Location" dataField="location"/>
 </mx:columns>
</mx:DataGrid>

<mx:HTTPService resultFormat="e4x" result="getDataCallback(event)" id="getDataHttp" url="http://localhost/test/getData.php" method="POST"/>
<mx:Button click="getDataHttp.send();" label="Load Data" x="379" y="268"/>

<mx:Script>
<![CDATA[ 

    import mx.collections.ArrayCollection;
    import mx.rpc.events.ResultEvent;
    import mx.controls.Alert;

    [Bindable]
    public var mydata:XMLList = new XMLList();
    function getDataCallback(event:ResultEvent):void
    {
        mydata = event.result.data.info;
        dataGrid.dataProvider = mydata;
    }
]]>
</mx:Script>

PHP script:

<?php
$xml = "<?xml version=\"1.0\" ?><data>";
$xml .= "<info><name>name1</name><age>26</age><location>location1</location></info>";
$xml .= "<info><name>name2</name><age>27</age><location>location2</location></info>";
$xml .= "</data>";
header("content-type:text/xml");
echo $xml;

But the data is not being loaded into the datagrid. Can anyone help me out?


I think your problem is that you are calling on the "data" node. With e4x, the root node is not named like that.

Try this instead:

mydata = event.result.info;

Other than that, you have the right idea. I am doing it here without the server component ant it works:

<s:applicationComplete>
    <![CDATA[
        var result:XML = <data>
                        <info><name>name1</name><age>26</age><location>location1</location></info>
                        <info><name>name2</name><age>27</age><location>location2</location></info>
                      </data>;

        dataGrid.dataProvider = result.info;
    ]]>
</s:applicationComplete>
0

精彩评论

暂无评论...
验证码 换一张
取 消