2 Replies Latest reply on Aug 25, 2007 5:37 PM by johnDschrock

    binding complex xml to a datagrid

    johnDschrock
      I am having trouble making a small flex app work. It's a tutorial on a blog that was created several years ago. The location of the blog article is:
      http://www.bpurcell.org/blog/index.cfm?mode=entry&entry=1020
      The article states: The datagrid in Flex works very well with flat XML. But what if you have a more complex XML packet that you need to bind to a DataGrid?

      Code I have changed to get things working:
      I changed the xmlns from macromedia to adobe and got that working.
      I also added <root> and </root> within the Model tag to make it work.
      I changed columnName in the DatagridColumn tag to dataField
      These changes allows the app to compile and run but I get an error as follows:

      ArgumentError: Error #1063: Argument count mismatch on BindingComplexXMLToDatagrid/::myLabelFunc(). Expected 1, got 2.at mx.controls.dataGridClasses::DataGridColumn/itemToLabel()

      If I remove the last datagridcolumn tag, the app runs ok but obviously that is the one I need to get working.

      Here is my complete app code:
      I would think it should be fairly simple but I have not been able to make it work.
      Any thoughts on how this can be made to work.

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" width="600" height="450">
      <mx:Script>
      <![CDATA[
      function myLabelFunc(item):String
      {
      if (item.address.number==undefined)
      {return null;}
      else
      {return item.address.number + " " + item.address.street;}
      }
      ]]>
      </mx:Script>
      <mx:Model id="thePersonModel">
      <root>
      <name>Brandon</name>
      <age>30</age>
      <address>
      <street>foobar</street>
      <number>19</number>
      </address>
      </root>
      </mx:Model>

      <mx:DataGrid id="myDataGrid2"
      dataProvider="{mx.utils.ArrayUtil.toArray(thePersonModel)}">
      <mx:columns>
      <mx:Array>
      <mx:DataGridColumn dataField="name" headerText="Name"/>
      <mx:DataGridColumn dataField="age" headerText="Age"/>
      <mx:DataGridColumn headerText="Address" labelFunction="myLabelFunc"/>
      </mx:Array>
      </mx:columns>
      </mx:DataGrid>
      </mx:Application>


        • 1. Re: binding complex xml to a datagrid
          chris.huston.t10 Level 3
          John,

          The DataGrid labelFunction takes two parameters:

          labelFunction(item:Object, column:DataGridColumn):String

          So change your code to something like this:

          private function myLabelFunc(item:Object, col:DataGridColumn):String

          The col parameter is not actually needed in your function but you need to list it in your function parameters. Also, notice that you should specify the variable type of item as Object and set the scope of the function to private or public.

          Another change would be to simplify the dataProvider binding to:
          <mx:DataGrid id="myDataGrid2" dataProvider="{thePersonModel}">

          Converting the XML model to an array doesn't seem to be necessary here, but it will work either way.

          Vygo
          • 2. Re: binding complex xml to a datagrid
            johnDschrock Level 1
            thanks so much. that did it.