Datagrid links (flex)

I wanted to ask how can I put the links in the datagrid. My dataProvider is the following xml

<xml>
 <item>
  <name>A name</name>
  <url>A url</name>
 </item>
 <item>
  <name>Another name</name>
  <url>Another url</name>
 </item>
</xml>

      

I'm sure there are a few more elements in it. Now I want to have a datagrid that displays the name as a label, and clicking on the row opens the url.

Can anyone help me with this? I know some things about render items, but I donā€™t know how I can provide a URL to the rendered item. Maybe with the class? But how can I control which url is passed to a particular rendered item?

Thank you in advance

Sebastian

+2


source to share


2 answers


Can you just do what you want with click event handlers?



<mx:Script>
    <![CDATA[
        import flash.net.navigateToURL;


        protected function datagrid1_clickHandler(event:MouseEvent):void
        {
            if(dg1.selectedItem)
            {
                var request:URLRequest = new URLRequest(dg1.selectedItem.url);
                navigateToURL(request);
            }
        }

        [Bindable]
        public var xml:XML = new XML(<xml>
        <item>
            <name>A name</name>
            <url>http://www.google.com</url>
        </item>
        <item>
            <name>Another name</name>
            <url>http://www.yahoo.com</url>
        </item>
    </xml>);
    ]]>
</mx:Script>
<mx:DataGrid id="dg1" editable="true" click="datagrid1_clickHandler(event)" dataProvider="{xml.children()}">
    <mx:columns>
        <mx:DataGridColumn dataField="name" />
    </mx:columns>
</mx:DataGrid>

      

+1


source


I think the best way to do this is to use an item renderer:

Set up your datagrid using the item renderer like this:

<mx:DataGrid id="flashcardSetGrid" width="80%" maxHeight="800" >
<mx:columns >
    <mx:DataGridColumn itemRenderer="com.jeshurunsoftware.DgLinkButton"/>
</mx:columns>

      



Then create an MXML component (in this example packaged in com.jeshurunsoftware.DgLinkButton.mxml):

<s:MXDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                      xmlns:s="library://ns.adobe.com/flex/spark" 
                      xmlns:mx="library://ns.adobe.com/flex/mx" 
                      focusEnabled="true">
      <mx:LinkButton id="lblData" label="{dataGridListData.label}" click="doSomething()"  />
</s:MXDataGridItemRenderer>

      

When you click on this element, your action will be performed.

+1


source







All Articles