1

I am using this example from blog.flexexamples.com.

I have a datagrid and trying to dynamically add button to one of datagrid column. But I want this Button as an ItemRenderer to be written in ActionScript and not MXML.

How can I do that ?

Thanks

ketan
  • 19,129
  • 42
  • 60
  • 98

2 Answers2

2

I think this is what you need.

ActionButtonItemRenderer.as :

package
{
    import flash.events.MouseEvent;

    import mx.controls.Alert;
    import mx.controls.Button;
    import mx.controls.listClasses.IDropInListItemRenderer;
    import mx.controls.listClasses.IListItemRenderer;
    import mx.core.IFlexDisplayObject;

    public class ActionButtonItemRenderer extends Button implements IFlexDisplayObject, IListItemRenderer, IDropInListItemRenderer
    {
        public var btn:Button;
        public function ActionButtonItemRenderer()
        {
            super();
            btn = new Button();
            btn.label = "Take Action";
            btn.addEventListener(MouseEvent.CLICK,clickHandler);
            btn.visible = btn.includeInLayout = true;

        }

        override protected function clickHandler(event:MouseEvent):void
        {
            super.clickHandler(event);
            Alert.show("Button clicked");

        }
    }
}

DynamicDataGridButton.mxml :

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600">
    <mx:ApplicationControlBar dock="true">
        <mx:Button label="Add column" click="init();" />
    </mx:ApplicationControlBar>
    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            [Bindable]
            private var arr:ArrayCollection = new ArrayCollection
                (
                    [
                        {fname:'A',lname:'B'},
                        {fname:'C',lname:'B'},
                        {fname:'D',lname:'B'}
                    ]           
                );

            private function addDataGridColumn(dataField:String):void {
                var dgc:DataGridColumn = new DataGridColumn(dataField);
                dgc.itemRenderer = new ClassFactory(ActionButtonItemRenderer);
                var cols:Array = dg.columns;
                cols.push(dgc);
                dg.columns = cols;
            }

            private function init():void {
                addDataGridColumn("Details");
            }
        ]]>
    </mx:Script>
    <mx:DataGrid id="dg" dataProvider="{arr}">
        <mx:columns>
            <mx:DataGridColumn id="dgc1" dataField="fname" headerText="First Name" 
                               width="75"/>
            <mx:DataGridColumn id="dgc2" dataField="lname" headerText=" Last Name" 
                               width="150"/>

        </mx:columns>
    </mx:DataGrid>

</mx:Application>
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
  • Not sure if something has changed since Oct 2011 (I am sure it has), but since you are extending Button, you do not need to implement those interfaces, or have a Button in `ActionButtonItemRenderer`. You can just do `this.label = "Take Action";`. And the Clickhandler is set in the Button class already also. Anyway, your answer pointed me in the right direction, so thanks for that. – Jacob Schoen Apr 10 '13 at 17:25
0

To dynamically create a button in AS3 syntax you would do this:

var button:Button = new Button();

Then after you've created the button, you can set it's properties like this:

button.label = "Click Me!";

And lastly you would add it to one of your items as such:

var dp:Array = [{label: "Button 1", button: button},{label: "Button 2", button: button}];
myDg.dataProvider = dp;

Then you would feed it to your datagrid that is laid out like this:

<mx:DataGrid id="myDG" variableRowHeight="true">
  <mx:columns>
    <mx:DataGridColumn dataField="label" headerText="Labels"/>
    <mx:DataGridColumn dataField="button" headerText="Buttons"/>
  </mx:columns>
</mx:DataGrid>

Not sure if that would actually work though, you may have to have an button itemRenderer on the entire column like this example.

Batkins
  • 5,547
  • 1
  • 28
  • 27
  • You need to be a little more specific with regards to your question if you want a good answer. You asked how to create a button in ActionScript, and I gave you that answer. What is it that you're trying to do? Perhaps provide a code block or two? – Batkins Oct 18 '11 at 16:53