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

    binding complex xml to a datagrid

      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:
      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">
      function myLabelFunc(item):String
      if (item.address.number==undefined)
      {return null;}
      {return item.address.number + " " + item.address.street;}
      <mx:Model id="thePersonModel">

      <mx:DataGrid id="myDataGrid2"
      <mx:DataGridColumn dataField="name" headerText="Name"/>
      <mx:DataGridColumn dataField="age" headerText="Age"/>
      <mx:DataGridColumn headerText="Address" labelFunction="myLabelFunc"/>

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

          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.

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