The key is to use the "attribute" method instead of the "@" symbol to reference the attributes.
Tracy
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
creationComplete="initApp()">
<mx:Script><![CDATA[
import mx.controls.dataGridClasses.DataGridColumn;
private var gXMLDoc:XML;
private function initApp():void
{
var sXMLDoc:String =
'<records>' +
'<record>' +
'<data @name="firstname" @value="My_FirstName1" />' +
'<data @name="lastname" @value="My_LastName1" />' +
'</record>' +
'<record>' +
'<data @name="firstname" @value="My_FirstName2" />' +
'<data @name="lastname" @value="My_LastName2" />' +
'</record>' +
'</records>'
gXMLDoc = XML(sXMLDoc);
dg1.dataProvider = gXMLDoc.record;
}//initApp
private function lfnGeneral(xmlItem:XML, column:DataGridColumn):String
{
var sLabel:String = "";
var sHeaderText:String = column.headerText;
switch (sHeaderText) {
case "First Name":
sLabel = xmlItem.data[0].attribute("@value");
break;
case "Last Name":
sLabel = xmlItem.data[1].attribute("@value");
break;
}
return sLabel;
}//lfGeneral
]]></mx:Script>
<mx:DataGrid id="dg1" labelFunction="lfnGeneral">
<mx:columns>
<mx:Array>
<mx:DataGridColumn headerText="First Name" />
<mx:DataGridColumn headerText="Last Name" />
</mx:Array>
</mx:columns>
</mx:DataGrid>
</mx:Application>
-----Original Message-----
From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of flexnewbie
Sent: Thursday, May 25, 2006 5:39 PM
To: [email protected]
Subject: [flexcoders] Re: How to populate an attribute value in DataGrid control?
Tracy,
Thanks for the feedback.
Unfortunately I cannot change the XML, the data is from third party
provider. I tried it with labelFunction and @@, it does not work :-(
var sFirstName:String = xmlRecord.data[0].(@@name ==
"lastname").@@value;
--- In [email protected], "Tracy Spratt" <[EMAIL PROTECTED]> wrote:
>
> First, the "@" in fornt of all the xml attributes might cause a
problem.
>
> Second, The name/value pair structure of your xml is going to be
> difficult to work with. Are you stuck with that structure?
>
> If so you will need to use a labelFunction(). In that you will do
> something like:
>
> var sFirstName:String = xmlRecord.data[0].(@@name ==
> "lastname").@@value; //that is where those @ signe might cause a
> problem.
>
> If you can change the xml format this can be much easier.
>
> Below is a sample app that uses labelFunction on fairly complex xml.
>
> Tracy
>
> <?xml version="1.0" encoding="utf-8"?>
> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
> layout="absolute"
> creationComplete="initApp()">
>
> <mx:Script><![CDATA[
> import mx.controls.dataGridClasses.DataGridColumn;
> private var gXMLDoc:XML;
> private function initApp():void
> {
> gXMLDoc =
> <users>
> <user>
> <username gender="male">Joe</username>
> <useremail>[EMAIL PROTECTED]</useremail>
> <location id="001">
> <city>Bedford</city>
> <state>MA</state>
> </location>
> </user>
> <user>
> <username gender="female">Maggie</username>
> <useremail>[EMAIL PROTECTED]</useremail>
> <location id="002">
> <city>Lexington</city>
> <state>MA</state>
> </location>
> </user>
> </users>
> dg1.dataProvider = gXMLDoc.user;
> }//initApp
>
> private function lfnGeneral(item:Object,
> column:DataGridColumn):String
> {
> var sLabel:String = ""
> var sHeaderText:String = column.headerText
> switch (sHeaderText) {
> case "Name":
> sLabel = item.username;
> break;
> case "Email":
> sLabel = item.useremail;
> break;
> case "City":
> sLabel = item.location.city;
> break;
> case "State":
> sLabel = item.location.city;
> break;
> case "Gender":
> sLabel = [EMAIL PROTECTED];
> break;
> }
>
> return sLabel;
> }//lfGeneral
>
> ]]></mx:Script>
>
> <mx:DataGrid id="dg1" labelFunction="lfnGeneral">
> <mx:columns>
> <mx:Array>
> <mx:DataGridColumn headerText="Name" />
> <mx:DataGridColumn headerText="Email" />
> <mx:DataGridColumn headerText="City" />
> <mx:DataGridColumn headerText="State" />
> <mx:DataGridColumn headerText="Gender" />
> </mx:Array>
> </mx:columns>
> </mx:DataGrid>
> </mx:Application>
>
>
>
> -----Original Message-----
> From: [email protected]
[mailto:[EMAIL PROTECTED] On
> Behalf Of flexnewbie
> Sent: Thursday, May 25, 2006 2:47 PM
> To: [email protected]
> Subject: [flexcoders] How to populate an attribute value in DataGrid
> control?
>
> I can't figure out how to populate the "First Name" and "Last
Name",
> please help!
>
> For Example,
> XML:
> <records>
> <record>
> <data @name="firstname" @value="My_FirstName1">
> <data @name="lastname" @value="My_LastName1">
> </record>
> <record>
> <data @name="firstname" @value="My_FirstName2">
> <data @name="lastname" @value="My_LastName2">
> </record>
> </records>
>
> Source Code:
> <mx:DataGrid id="feedRequest"
> dataProvider="{feedRequest.result.records.record}">
> <mx:DataGridColumn headerText="FirstName" dataField="" />
> <mx:DataGridColumn headerText="LastName" dataField="" />
> </mx:DataGrid>
>
>
>
>
>
>
>
>
>
> --
> Flexcoders Mailing List
> FAQ:
http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
> Search Archives:
> http://www.mail-archive.com/flexcoders%40yahoogroups.com
> Yahoo! Groups Links
>
--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com
Yahoo! Groups Links
--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com
SPONSORED LINKS
| Web site design development | Computer software development | Software design and development |
| Macromedia flex | Software development best practice |
YAHOO! GROUPS LINKS
- Visit your group "flexcoders" on the web.
- To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
- Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.

