2
votes

I wanted to ask how I can put links into a datagrid. My dataProvider is the folling xml

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

sure with some more items in it. Now I want to have a datagrid that displays the name as a label and when clicking on the row the url is opened.

Can anyone help me with that thing? I know some stuff about item renderes but I don't know how I can give the url to the item renderer. Maybe with a classfactory? But how can I control which url is given to the specific item renderer?

Thanks in advance

Sebastian

2
ok ... I think this is trivial by using inline item renderers but any suggestions are still welcomeSebastian Müller
an item in the data provider contains a label and a data property, right ? can you not store the url in the data property ?George Profenza

2 Answers

1
votes

Can you just do what you want using the 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
votes

I think a better way to do this is to use an item renderer:

Configure your datagrid with an 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 item, your action will be performed.