简体   繁体   English

将xml(来自php脚本的响应)加载到datagrid中时出现问题-Flex

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

I'm trying to learn Flex. 我正在尝试学习Flex。 I have a question in loading the DataGrid from the XML response (from a PHP script). 我在从XML响应(从PHP脚本)加载DataGrid时遇到问题。

mxml code: mxml代码:

<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脚本:

<?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. 在e4x中,根节点的名称不是这样。

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: 我在没有服务器组件ant的情况下在这里这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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