legion
legion

Reputation: 1

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

Upvotes: 0

Views: 1948

Answers (3)

Michael S
Michael S

Reputation: 425

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

Upvotes: 0

kbgn
kbgn

Reputation: 856

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

Upvotes: 0

Angelo
Angelo

Reputation: 1666

You set the dataProvider as {sampleXML.contact}

It should be {sampleXML.contacts}

Upvotes: 1

Related Questions