3 Replies Latest reply on Apr 23, 2009 4:50 AM by salut

    XML and DataGrid

    salut Level 1

      I'm struggling with XML in a DataGrid. Can you someone tell me how to populate the dataField for firstName, lastName, and userId, given the XML below? Thanks for your help.

       

      <?xml version="1.0"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

       

          <mx:XMLList id="userData">
             <users>
                     <user userId="fxxxxx" firstName="Tata" lastName="Hope">
                         <publication pubId="9" pubTitle="Daily News">
                             <policy policyId="9" policyDescription="Deliver Issues" policyname="DeliverIssues" />
                         </publication>
                     </user>
                     <user userId="fyyyyy" firstName="Lele" lastName="Herr">
                         <publication pubId="9" pubTitle="Daily News">
                             <policy policyId="1" policyDescription="Manage Groups" policyname="ManageGroups" />
                         </publication>
                     </user>
               </users>
          </mx:XMLList>

       

          <mx:Panel title="DataGrid Control Example" height="100%" width="100%"
              paddingTop="10" paddingLeft="10" paddingRight="10">

       

              <mx:DataGrid id="dg" width="100%" height="100%" rowCount="5" dataProvider="{userData}">
                  <mx:columns>
                      <mx:DataGridColumn dataField="firstName" headerText="firstName"/>
                      <mx:DataGridColumn dataField="lastName" headerText="lastName"/>
                      <mx:DataGridColumn dataField="userId" headerText="Email"/>
                  </mx:columns>
              </mx:DataGrid>

       

              <mx:Form width="100%" height="100%">
                  <mx:FormItem label="firstName">
                    <mx:Label text="{dg.selectedItem.firstName}"/>
                  </mx:FormItem>
                   <mx:FormItem label="lastName">
                      <mx:Label text="{dg.selectedItem.lastName}"/>
                  </mx:FormItem>
                  <mx:FormItem label="userId">
                      <mx:Label text="{dg.selectedItem.userId}"/>
                  </mx:FormItem>       
              </mx:Form>     
          </mx:Panel>
      </mx:Application>      

        • 1. Re: XML and DataGrid
          -Hob Level 1

          There are a couple things throwing you off:

           

          1)  Remove the root node from your xml (<user>)

          2)  Since your dataProvider is XML, your dataField is referencing xml attributes which means you need to use standard e4x notation.  So, instead of "firstName" as your dataField, you would use "@firstName".

           

          The same thing is going to be true for your form at the bottom. eg:

           

                      <mx:FormItem label="firstName">
                        <mx:Label text="{dg.selectedItem.@firstName}"/>
                      </mx:FormItem>

          1 person found this helpful
          • 2. Re: XML and DataGrid
            Gregory Lafrance Level 6

            Just in case you are still unclear:

             

             

            <?xml version="1.0"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
              <mx:XMLList id="userData">
                <user userId="fxxxxx" firstName="Tata" lastName="Hope">
                  <publication pubId="9" pubTitle="Daily News">
                    <policy policyId="9" policyDescription="Deliver Issues" policyname="DeliverIssues" />
                  </publication>
                </user>
                <user userId="fyyyyy" firstName="Lele" lastName="Herr">
                  <publication pubId="9" pubTitle="Daily News">
                    <policy policyId="1" policyDescription="Manage Groups" policyname="ManageGroups" />
                  </publication>
                </user>
              </mx:XMLList>
              <mx:Panel title="DataGrid Control Example" height="100%" width="100%"
                paddingTop="10" paddingLeft="10" paddingRight="10">
                <mx:DataGrid id="dg" width="100%" height="100%" rowCount="5" dataProvider="{userData}">
                  <mx:columns>
                    <mx:DataGridColumn dataField="@firstName" headerText="firstName"/>
                    <mx:DataGridColumn dataField="@lastName" headerText="lastName"/>
                    <mx:DataGridColumn dataField="@userId" headerText="Email"/>
                  </mx:columns>
                </mx:DataGrid>
                <mx:Form width="100%" height="100%">
                  <mx:FormItem label="firstName">
                    <mx:Label text="{dg.selectedItem.@firstName}"/>
                  </mx:FormItem>
                  <mx:FormItem label="lastName">
                    <mx:Label text="{dg.selectedItem.@lastName}"/>
                  </mx:FormItem>
                  <mx:FormItem label="userId">
                    <mx:Label text="{dg.selectedItem.@userId}"/>
                  </mx:FormItem>       
                </mx:Form>     
              </mx:Panel>
            </mx:Application> 

            • 3. Re: XML and DataGrid
              salut Level 1

              Thank you so much for your help!!!!