0

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>
    </fx: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>
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
legion
  • 1
  • 1

3 Answers3

1

You set the dataProvider as {sampleXML.contact}

It should be {sampleXML.contacts}

Angelo
  • 1,578
  • 3
  • 20
  • 39
0

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

kbgn
  • 856
  • 1
  • 7
  • 16
0

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

Michael S
  • 425
  • 3
  • 12