8 Replies Latest reply on Jan 30, 2008 9:09 AM by josh_aries

    XML and DataGrid

    josh_aries Level 1
      Hi,
      I am binding an xml document to the Datagrid and came across a situation where I have to bind as in the example below

      I have a node in the XML as follows

      <node attribute1="1" attribute2="2" />

      i need the attribute2 value as the datafield for my datagrid

      how do i use that attribute
        • 1. Re: XML and DataGrid
          dazweeja Level 1
          Something like this might work otherwise you need to use a labelFunction:

          <mx:DataGrid>
          <mx:columns>
          <mx:DataGridColumn dataField="@attribute2" />
          </mx:columns>
          </mx:DataGrid>
          • 2. XML and DataGrid
            josh_aries Level 1
            I have a new problem. I have several child nodes with the same name. How do I access them action_column. The LabelFunc gives the attributes only for the first child

            The complete code is as follows

            LabelFunc Script

            public function LabelFunc(item:Object, column:DataGridColumn): String
            {
            return (item["action_column"]["column_number"]);
            }


            <mx:DataGrid id="dgAction1" dataProvider="{HTTPTest.lastResult.action1.action2}" width="996">
            <mx:columns>
            <mx:DataGridColumn headerText="" dataField="action_number" width="30" />
            <mx:DataGridColumn editable="true" headerText="Actions" dataField="action_description" width="600" />
            <mx:DataGridColumn labelFunction="LabelFunc" />
            </mx:columns>
            </mx:DataGrid>

            Part of the XML file is as

            <action1>
            <action2>
            <action_number>1</action_number>
            <action_description>Action Desc</action_description>
            <action_column column_number="1" column_value="1" />
            <action_column column_number="2" column_value="2" />
            <action_column column_number="3" column_value="3" />
            </action2>
            <action2>
            ---------------
            </action2>
            </action1>

            Could someone please help me with this
            • 3. Re: XML and DataGrid
              VarioPegged Level 2
              If you set the resultFormat of the service that returns the XML to e4x (resultFormat="e4x"), then you can refer to your XML elements and attributes with dot syntax.

              Your LabelFunc would look like this:
              public function LabelFunc(item:Object, column:DataGridColumn): String
              {
              return (item.action_column.@column_number);
              }

              Then strip the root node element out of the service binding in the dataProvider:
              dataProvider="{HTTPTest.lastResult.action2}"

              Note that the column_number attributes will show up concatenated as "123" in your datagrid column with labelFunction="LabelFunc". I'm guessing that's what you want.

              TS
              • 4. Re: XML and DataGrid
                josh_aries Level 1
                Thanks for the immediate reply. I tried using e4x but its not working. I got to display 1, 2, 3 in separate columns.
                • 5. Re: XML and DataGrid
                  VarioPegged Level 2
                  Can you explain or give an example how you want your datagrid to look?

                  TS
                  • 6. XML and DataGrid
                    josh_aries Level 1
                    There are 5 Columns and the corresponding values are
                    action_number -------------------- 1
                    action_description ------------------ Action Desc
                    1 ---------------------1
                    2 ----------------------2
                    3 ----------------------3
                    • 7. Re: XML and DataGrid
                      dazweeja Level 1
                      You're almost there Josh ;)

                      Firstly, if you're manually defining DataGridColumns, you'll need one DataGridColumn for each column you want displayed, eg.

                      <mx:DataGrid id="dgAction1" dataProvider="{HTTPTest.lastResult.action2}" width="996">
                      <mx:columns>
                      <mx:DataGridColumn headerText="" dataField="action_number" width="30" />
                      <mx:DataGridColumn editable="true" headerText="Actions" dataField="action_description" width="60" />
                      <mx:DataGridColumn dataField="1" labelFunction="LabelFunc" />
                      <mx:DataGridColumn dataField="2" labelFunction="LabelFunc" />
                      <mx:DataGridColumn dataField="3" labelFunction="LabelFunc" />
                      </mx:columns>
                      </mx:DataGrid>

                      Also, VarioPegged is correct about skipping the root tag (action1) in your dataProvider.

                      The other thing is that if you're going to reuse the same labelFunction for each of your column_value columns, you want this function to return a different value depending on which column is calling the label function. Otherwise, the labelFunction will always return the same value regardless of the column. This is because the same 'item' is passed in to each column in a DataGrid row (use trace(item.toXMLString()) in your labelFunction to see what I mean). I like to use the dataField of the dataGridColumn so that column.dataField in my labelFunction can identify which column is calling the labelFunction. In this case, the labelFunction would be:

                      public function LabelFunc(item:Object, column:DataGridColumn): String
                      {
                      return item.action_column.(@column_number==column.dataField).@column_value;
                      }

                      Good luck,
                      Darren.
                      • 8. Re: XML and DataGrid
                        josh_aries Level 1
                        Thanks a ton to dazweeja,VarioPegged for your help. It worked. I really appreciate all your time.