Search code examples
xmlactionscriptadvanceddatagrid

AS: getting XML attribute of selected row in AdvancedDataGrid


I populate an AdvancedDataGrid with XML Data, this is an example of the XML data I use :

<list>
  <root>
    <item text="Folder" id="1" isBranch="true" classes="folder">
      <item text="SubFolder" id="2" isBranch="true" classes="folder">
        <item text="Item" id="3" isBranch="false" classes="item" col1="1" col2="2" />
      </item>
      <item text="Folder" id="4" isBranch="true" classes="folder">
        <item text="Item" id="5" isBranch="false" classes="item" col1="3" col2="4"/>
      </item>
    </item>
  </root>
</list>

After the user then clicks on a cell, I would like to retrieve the attributes of the node he clicked on.

So, for example, if the user clicks on column 1 of the Item with ID 3, I need to access the attributes of this XML node :

<item text="Item" id="3" isBranch="false" classes="item" col1="1" col2="2" />

So I can find out the ID of this item (in this case 3).

Note that this ID is never displayed in the AdvancedDataGrid.


Solution

  • I don't know if this is what you want but here it goes.

    <controls:AdvancedDataGrid dataProvider="{ new HierarchicalData(list) }"
                               itemClick="{ idLabel.text = event.itemRenderer.data.@id }">
    
        <controls:columns>
            <!-- some sample columns -->
            <advanceddatagridclasses:AdvancedDataGridColumn dataField="@text"/>
            <advanceddatagridclasses:AdvancedDataGridColumn dataField="@isBranch"/>
            <advanceddatagridclasses:AdvancedDataGridColumn dataField="@col1"/>
            <advanceddatagridclasses:AdvancedDataGridColumn dataField="@col2"/>
        </controls:columns>
    
    </controls:AdvancedDataGrid>
    
    <s:Label id="idLabel" />
    

    The important thing to retain is this line:

    event.itemRenderer.data.@id
    

    Whenever you click on a AdvancedDataGrid item, the "itemClick" event will be dispatched. This event contains a reference to the itemRenderer which by its turn contains a reference to the data being displayed on that row. In this case, to access the id attribute you just need to use data.@id.

    If you want, you can have something like this:

    <controls:AdvancedDataGrid dataProvider="{ new HierarchicalData(list) }"
                               itemClick="{ doSomething(event.itemRenderer.data) }">
    

    and then create a doSomething method which will receive the "clicked" XML node as param.