开发者

xml attribute datafield datagrid

Why cannot I bind xml attribute for datafield? (flex 4)

<fx:Model id="sampleXML">
        <contacts>
            <contact firstName="Joe" lastName="Smith" emailAddress="joe@smith.com" />
            <contact firstName="Sally" lastName="Lally" emailAddress="sally@lally.com" />
            <contact firstName="Albert" lastName="Rigdon" emailAddress="albert@rigdon.com" />
        </contacts>
    </f开发者_Go百科x:Model>
<mx:DataGrid dataProvider="{sampleXML.contact}" id="dg">
    <mx:columns>
        <mx:DataGridColumn headerText="First Name" dataField="@firstName"  />
        <mx:DataGridColumn headerText="Last Name" dataField="@lastName" />
        <mx:DataGridColumn headerText="Email Address" dataField="@emailAddress" />
    </mx:columns>
</mx:DataGrid>


You set the dataProvider as {sampleXML.contact}

It should be {sampleXML.contacts}


You can also try to use XMLListCollection as shown below and provide 'contactsList' as dataprovider to the datagrid.

<mx:XML id="tempXML"
        source="assets/contacts.xml" />

<mx:XMLListCollection id="contactsList"
        source="{tempXML.contacts}" />

Assumption : xml is stored in assets folder and xml name is contacts.xml


Try changing your fx:Model to fx:XML if you want to use the same @ notation. Model deserialises the XML into an object which means your @notation will not give the desired outcome. The following example works:

<fx:XML id="sampleXML">
   <contacts>
      <contact firstName="Joe" lastName="Smith" emailAddress="joe@smith.com" />
      <contact firstName="Sally" lastName="Lally" emailAddress="sally@lally.com" />
      <contact firstName="Albert" lastName="Rigdon" emailAddress="albert@rigdon.com"/>
   </contacts>
</fx:XML>

<mx:DataGrid dataProvider="{sampleXML.contact}" id="dg">
   <mx:columns>
      <mx:DataGridColumn headerText="First Name" dataField="@firstName"  />
      <mx:DataGridColumn headerText="Last Name" dataField="@lastName" />
      <mx:DataGridColumn headerText="Email Address" dataField="@emailAddress" />
   </mx:columns>
</mx:DataGrid>

If you want to use fx:Model then drop the "@" from in front of your dataField names

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜