1 Reply Latest reply on Dec 18, 2009 7:21 AM by saisri2k2

    Bind datagrid columns to XML attributes

    snafu7x7

      As usual, every example you will see on binding a datagrid involves the most simplistic XML imaginable (cause you know, in the real world that's always what we get to work with, right?). Is there a way to bind the datagrid columns by attribute rather than element? In otherwords, if I have XML that looks like this, how would I bind it to my grid?

       

      <root>

       

           <data>

                <field name="id">123</field>

                <field name="name">Me</field>

                <field name="age">30</field>

           </data>

       

           <data>

                <field name="id">456</field>

                <field name="name">You</field>

                <field name="age">31</field>

           </data>

      </root>

        • 1. Re: Bind datagrid columns to XML attributes
          saisri2k2 Level 4

          For me there is no direct way to bind it, but work around for it is copy the XML into an array Collection and bind the array collection to the datagrid like i've done below.

           

          <mx:Script>

          <![CDATA[

          import mx.collections.ArrayCollection;

          [Bindable] var acList:ArrayCollection = new ArrayCollection();

          [Bindable] public var xData:XML =  <root><data>

                    <field name="id">123</field>

                    <field name="name">Me</field>

                    <field name="age">30</field>

               </data>

               <data>

                    <field name="id">456</field>

                    <field name="name">You</field>

                    <field name="age">31</field>

               </data></root>;

             

               for each(var item:Object in xData.data){

               acList.addItem({id:item.field.@id,name:item.field.@name,age:item.field.@age});

               }

             

          ]]>

          </mx:Script>

          <mx:DataGrid dataProvider="{acList}">

          <mx:columns>

          <mx:DataGridColumn dataField="id">

           

          </mx:DataGridColumn>

          <mx:DataGridColumn dataField="name">

           

          </mx:DataGridColumn>

          <mx:DataGridColumn dataField="age">

           

          </mx:DataGridColumn>

          </mx:columns>

          </mx:DataGrid>