5 Replies Latest reply on Oct 21, 2008 8:53 AM by SiHoop

    xml

    SiHoop Level 1
      The code below works for the XMLList id="worker" with dataProvider="{worker} and dataField="name"
      1. What should I use for the dataProvider/ dataField to make XMLList id="workers" to show <last> in the datagrid?
      2. Inside the Script tag, how do I connect link my arrayCollection so I can use 'people' as my dataProvider? Again, I want to use <last> as the field that shows in the datagrid.
      Thank you!
        • 1. Re: xml
          m_hartnett Level 3
          One of the problems here is in the definition of the datasource. Notice that when this code runs that only one row (with nothing displaying in it) will show up in the datagrid. It is showing only the <employee> items and there is only one employee listing.

          You could change the dataprovider to be:
          <mx:DataGrid id="testgrid" width="350" rowHeight="26" height="447" dataProvider="{workers.children()}">

          However it will not support binding and if any changes occur to the data they will not be reflected in the grid.

          Another solution would be to do the following ( here is all the code, just notice the init function and the new XMLList property for workersX.

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
          layout="absolute"
          creationComplete="init()">
          <mx:Script>
          <![CDATA[
          import mx.collections.XMLListCollection;
          import mx.collections.ArrayCollection
          [Bindable] public var people:ArrayCollection
          [Bindable] public var workersX:XMLListCollection;

          public function init():void {
          workersX = new XMLListCollection(this.workers.children());

          }
          ]]></mx:Script>

          <mx:XMLList id="worker">
          <employee>
          <name>Christina</name>
          </employee>
          <employee>
          <name>Joanne</name>
          </employee>
          </mx:XMLList>
          <mx:XMLList id="workers">
          <employee>
          <name>
          <last>smith</last>
          </name>
          <name>
          <last>jones</last>
          </name>
          </employee>
          </mx:XMLList>

          <mx:DataGrid id="testgrid" width="350" rowHeight="26" height="447" dataProvider="{workersX}">
          <mx:columns >
          <mx:DataGridColumn width="85" headerText="lastName" dataField="last"/>
          </mx:columns>
          </mx:DataGrid>


          </mx:Application>


          • 2. Re: xml
            SiHoop Level 1
            That worked nicely-- thanks! However, in the code below when I changed
            workersX = new XMLListCollection(this.workers.children());
            to
            workersX = new XMLListCollection(this.worker.children());
            it does not produce anything. Why is that case different?
            • 3. Re: xml
              m_hartnett Level 3
              The call to children() in the workers XMLList results in the following:
              <name>
              <last>smith</last>
              </name>
              <name>
              <last>jones</last>
              </name>

              The call to children() in the worker results in
              <last>Christina</last>
              <last>Joanne</last>
              and ends up being the top node in the XMLList.

              You can see that the the above list itself contains the data and there is no node within each list item.

              In order to do this you would need to use a labelFunction such as
              public function nameFunction(data:Object,dgc:DataGridColumn):String {
              return data.toString();
              }
              The add the labelFunction to the datagridcolumn
              labelFunction="nameFunction"

              This would return the data that is associated with the top node.
              However I never structure my xmllistcolleciton data like the above. I always make sure I have a top node and it has nodes below it.



              • 4. Re: xml
                SiHoop Level 1
                Great-- that helps a lot. One more question: Suppose I want to use an arrayCollection to store my data. How do I put the xml into the arrayCollection. My problem below is with
                people = myXml.bloggers.blogger;
                How should I refer to my xml to allow me to access <name>xxx</name>?
                • 5. Re: xml
                  SiHoop Level 1
                  people = myXml as ArrayCollection;