简体   繁体   中英

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

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"/> 
     <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM