尝试将xml分配到特定的datagrid数据字段中

时间:2011-03-26 20:35:37

标签: flex actionscript-3 flash-builder

在暂时不使用它之后返回flex,尝试使用dataprovider将一些xml(由php生成)加载到数据网格中,以创建一个简单的网格,显示一周内的每日高温和低温。我可以获取数据显示在我的数据网格中,但列是按随机顺序加载的。 (所以星期五在星期一之前到来等等)所以我只想弄清楚如何将特定数据分配给特定的数据域。

这是flex代码; (更新为工作代码。请注意,dataFields只包含xml标记的名称和/或这些标记内的标记名称)

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

<fx:Declarations>
    <s:HTTPService id="weather"  url="http://localhost/weather/index.php" result="weather_resultHandler(event)" fault="weather_faultHandler(event)" />
</fx:Declarations>


<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.controls.Alert;
        import mx.rpc.events.FaultEvent;
        import mx.rpc.events.ResultEvent;

        //Setup array making sure to import Array Collection
        [Bindable]
        public var mydata:ArrayCollection = new ArrayCollection();

        protected function weather_resultHandler(event:ResultEvent):void
        {
            trace(event.result.weather.city);
            //mydata is our array and we are adding the info to it.  Show the xml file.
            mydata = event.result.weather.city;             
        }

        protected function weather_faultHandler(event:FaultEvent):void
        {
            //import Alert so we can use this class
            Alert.show("There is a problem with the data connection");
        }
    ]]>
</fx:Script>


<mx:DataGrid x="49" y="226" id="weatherGrid" dataProvider="{mydata}">
    <mx:columns>
        <mx:DataGridColumn headerText="City" dataField="name"/> 
        <mx:DataGridColumn width="40" headerText="High" dataField="mon.high"/>
        <mx:DataGridColumn width="40" headerText="Low" dataField="mon.low"/>                
        <mx:DataGridColumn width="40" headerText="High" dataField="tues.high"/>
        <mx:DataGridColumn width="40" headerText="Low" dataField="tues.low"/>
        <mx:DataGridColumn width="40" headerText="High" dataField="wed.high"/>
        <mx:DataGridColumn width="40" headerText="Low" dataField="wed.low"/>
        <mx:DataGridColumn width="40" headerText="High" dataField="thurs.high"/>
        <mx:DataGridColumn width="40" headerText="Low" dataField="thurs.low"/>
        <mx:DataGridColumn width="40" headerText="High" dataField="fri.high"/>
        <mx:DataGridColumn width="40" headerText="Low" dataField="fri.low"/>
        <mx:DataGridColumn width="40" headerText="High" dataField="sat.high"/>
        <mx:DataGridColumn width="40" headerText="Low" dataField="sat.low"/>
        <mx:DataGridColumn width="40" headerText="High" dataField="sun.high"/>
        <mx:DataGridColumn width="40" headerText="Low" dataField="sun.low"/>
    </mx:columns>
</mx:DataGrid>

php生成的xml是这个; (根据评论编辑)

<?xml version="1.0" encoding="utf-8" ?><weather><city>
    <name>London</name>
    <mon>
        <high>-2</high>
        <low>-10</low>
    </mon>
    <tues>
        <high>-1</high>

        <low>-5</low>
    </tues>
    <wed>
        <high>0</high>
        <low>-2</low>
    </wed>
    <thurs>

        <high>3</high>
        <low>0</low>
    </thurs>
    <fri>
        <high>5</high>
        <low>2</low>
    </fri>

    <sat>
        <high>8</high>
        <low>4</low>
    </sat>
    <sun>
        <high>10</high>
        <low>6</low>

    </sun>
</city><city>
    <name>Toronto</name>
    <mon>
        <high>-5</high>
        <low>-10</low>
    </mon>

    <tues>
        <high>-8</high>
        <low>-15</low>
    </tues>
    <wed>
        <high>-2</high>
        <low>-7</low>

    </wed>
    <thurs>
        <high>3</high>
        <low>-3</low>
    </thurs>
    <fri>
        <high>6</high>

        <low>0</low>
    </fri>
    <sat>
        <high>7</high>
        <low>3</low>
    </sat>
    <sun>

        <high>9</high>
        <low>4</low>
    </sun>
</city></weather>

1 个答案:

答案 0 :(得分:1)

我给你的建议是迭代xml并将其转换为数据模型,以便更容易使用。像

这样的东西
WeatherModel
   monday
      high
      low
   tuesday
      high
      low
   wednesday
      high
      low
   thursday
      high
      low
   friday
      high
      low
   saturday
      high
      low
   sunday
      high
      low

这样您就可以将实际日期添加到列数据字段中。你不需要使用直接的xml,这对于使用anywho很烦人。只需要将它映射到一个对象。您可以使用Spicefactory's XML object mapper来完成工作。