10
Dec
08

Displaying dynamically loaded XML in a DataGrid control in Flex

The following example shows how you can display dynamically loaded XML data (with namespaces) in a Flex DataGrid control.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/12/10/displaying-dynamically-loaded-xml-in-a-datagrid-control-in-flex/ -->
<mx:Application name="DataGrid_XML_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        xmlns:net="flash.net.*"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.events.ListEvent;
            import mx.controls.dataGridClasses.DataGridColumn;

            public namespace sitemapNS = "http://www.google.com/schemas/sitemap/0.84";

            private function loadXML(targetURL:String):void {
                urlLdr.load(new URLRequest(targetURL));
                loadBtn.enabled = false;
            }

            private function urlLdr_complete(evt:Event):void {
                var xmlData:XML = new XML(URLLoader(evt.currentTarget).data);
                xmlListColl = new XMLListCollection(xmlData.children());
                dataGrid.enabled = true;
                loadBtn.enabled = true;
            }

            private function dataGrid_labelFunc(item:XML, col:DataGridColumn):String {
                var qN:QName = new QName(sitemapNS, col.dataField);
                return item[qN].text();
            }

            private function dataGrid_dateLabelFunc(item:XML, col:DataGridColumn):String {
                var qN:QName = new QName(sitemapNS, col.dataField);
                var value:String = item[qN].text();
                value = value.replace(/-/g, "/");
                value = value.replace("T", " ");
                value = value.replace("+00:00", "");
                return value;
            }

            private function dataGrid_itemDoubleClick(evt:ListEvent):void {
                use namespace sitemapNS;
                var url:String = evt.itemRenderer.data.loc;
                navigateToURL(new URLRequest(url), "_blank");
            }
        ]]>
    </mx:Script>

    <net:URLLoader id="urlLdr"
            complete="urlLdr_complete(event);" />
    <mx:XMLListCollection id="xmlListColl" />

    <mx:ApplicationControlBar dock="true">
        <mx:Button id="loadBtn"
                label="Load XML"
                click="loadXML('http://blog.flexexamples.com/sitemap.xml');" />
        <mx:Spacer width="100%" />
        <mx:ProgressBar id="progressBar"
                mode="event"
                source="{urlLdr}"
                labelPlacement="center" />
    </mx:ApplicationControlBar>

    <mx:DataGrid id="dataGrid"
            dataProvider="{xmlListColl}"
            doubleClickEnabled="true"
            itemDoubleClick="dataGrid_itemDoubleClick(event);"
            enabled="false"
            width="100%"
            height="100%">
        <mx:columns>
            <mx:DataGridColumn dataField="loc"
                    labelFunction="dataGrid_labelFunc"
                    itemRenderer="mx.controls.Label" />
            <mx:DataGridColumn dataField="lastmod"
                    labelFunction="dataGrid_dateLabelFunc"
                    width="150" />
            <mx:DataGridColumn dataField="changefreq"
                    labelFunction="dataGrid_labelFunc"
                    width="100" />
            <mx:DataGridColumn dataField="priority"
                    labelFunction="dataGrid_labelFunc"
                    width="100" />
        </mx:columns>
    </mx:DataGrid>

</mx:Application>

View source is enabled in the following example.


2 Responses to “Displaying dynamically loaded XML in a DataGrid control in Flex”


  1. 1 pete Feb 25th, 2009 at 7:39 am

    Hey there, does this only work with sitemaps? i wanted to use this with a project i’m working on, but when i pull in my xml data set it doesn’t show the data in the datagrid, but it is there as the double click works fine…
    i can upload a sample of my xml and the code how i’ve edited it if that would help, plus i can do a screen shot of the datagrid if needed?
    any ideas?
    cheers
    pete

  2. 2 ufuk Apr 16th, 2009 at 1:39 am

    your posts are great! thank you.. the codes work perfect..

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




Badge Farm

  • Powered by Redoable 1.2
  • Cornify
  • Feeds burnt by Feedburner
  • Feed