3 Replies Latest reply on Jun 23, 2006 2:05 PM by ntsiii

    Using arrays in a Data model

    markive
      Hi,

      Im really struggling to use Arrays in a DataModel.

      At the moment I am able to populate a datagrid with my different arrays, into their corresponding columns. The problem is, these are output as a string that only fills the first row. I cant seem to get past this.

      I think it must be the datamodel.

      I can see in the Livedocs that in order to use an array as a datamodel, you need to declare it as a <mx:object> with an array inside.. But the example doesn't run, in fact pasting the example itself into my code brings up an error!

      Any help much appreciated..

      http://livedocs.macromedia.com/labs/1/flex20beta3/wwhelp/wwhimpl/common/html/wwhelp.htm?co ntext=LiveDocs_Parts&file=00001067.html

      <mx:Object id="model1">
      <employees>
      <mx:Object>
      <mx:Array>
      <name>
      <mx:Object>
      <first>
      <mx:String></mx:String>
      </first>
      <last>
      <mx:String></mx:String>
      </last>
      </mx:Object>
      </name>
      <department>
      <mx:String></mx:String>
      </department>
      <email>
      <mx:String></mx:String>
      </email>
      </mx:Object>
      </employees>
      </mx:Array>
      </mx:Object>

      ^ Causes an error because of how its formatted.. I haven't been able to get round this..


      Thanks for any help, much appreciated..

      Mark

        • 1. Re: Using arrays in a Data model
          leotemp Level 1
          This unfortunetly this seems to be the norm with the documentation, having been a flash virgin i find the documentation to be absolutley frustrating, rarely are the sample snippets even remotley formated correctly which is probably fine for alot of people that just need a gereral concept to move forward but some of use need actual working examples with the an actual example of the datasource. Anyways, try this:

          Im doing just what your describing using an external file that renders xml rather then an "model" but you should be able to apply the same logic, im using asp but it could be anything else even just a static xml file like in the following example:

          ----xml.xml-save-in-same-directory-as-application--
          <product>
          <category>Tile</category>
          <name>Angelic Peach</name>
          </product>
          <product>
          <category>Tile</category>
          <name>Cosmo</name>
          </product>
          <product>
          <category>Tile</category>
          <name>Durva</name>
          </product>
          <product>
          <category>Tile</category>
          <name>Isosilis</name>
          </product>
          <product>
          <category>Tile</category>
          <name>Magaloth</name>
          </product>
          <product>
          <category>Tile</category>
          <name>Trunklin</name>
          </product>
          <product>
          <category>Tile</category>
          <name>Googolfletch</name>
          </product>
          <product>
          <category>Tile</category>
          <name>Moskurbelf</name>
          </product>
          <product>
          <category>Tile</category>
          <name>DoChoaKoa</name>
          </product>
          ---xml.xml----------------------------

          ----products.mxml---------------------
          <?xml version="1.0" encoding="utf-8"?>
          <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="400" height="300" creationComplete="this.productStream.send();">
          <mx:Script>
          <![CDATA[
          import mx.utils.ArrayUtil;
          ]]>
          </mx:Script>
          <mx:HTTPService id="productStream"
          url="{xml.xml}"/>


          <mx:DataGrid id="prodList" dataProvider="{mx.utils.ArrayUtil.toArray(this.productStream.lastResult.product)}" columnWidth="200" width="200" left="0" top="55">
          <mx:columns>
          <mx:DataGridColumn headerText="Products" dataField="name"/>
          </mx:columns>
          /mx:DataGrid>


          </mx:Canvas>
          ----products.mxml---------------------

          I hope that works for you or helps point you in the right direction, i by no means claim to know what im doing here, in fact i need help populating a combobox with nested xml from my external file, all i get with my current method is "[object][object]" but the only examples i can find on accessing nested xml is the broken datagrid one :(
          • 2. Re: Using arrays in a Data model
            leotemp Level 1
            oh yeah, one more thing, if you are using a dynamic datasource like php or asp make sure you reference the file via a full url otherwise flash will try to parse the asp file rather then the xml it renders. Example:

            ---!!!BAD!!!----
            <mx:HTTPService id="productStream"
            url="{dynamicXml.asp}"/>

            ---!!!GOOOD!!!!!----
            <mx:HTTPService id="productStream"
            url="{ http://localhost/myWebApplication/dynamicXML.asp}"/>
            • 3. Re: Using arrays in a Data model
              ntsiii Level 3
              You only need to do that if there is only one element in your array. Also an alternative is to use the Model tag normally, and use mx.utils.ArrayUtil.toArray(myModelId) when you want to use it as a data provider.

              You should use that util any time a dp array might have only one element.

              Also, look at using ArrayCollection instead of Object, it also works well as a dataProovider.

              Tracy