AS3 - 将变量传递给类函数(按钮到dataGrid)

时间:2013-07-12 15:09:07

标签: actionscript-3 flash class button datagrid

我正在尝试在Flash中的dataGrid中创建按钮。但是,我是一个初学者,我现在不知道如何使我的函数生成按钮列参数,我需要使用每个按钮。我需要每个按钮都有一个特定的标签,如果我点击该按钮,它会将googleURL复制到剪贴板。

我不知道如何将变量“buttonLabel”和“googleURL”传递给该函数......

这是代码的.fla部分:

        googleURL = "www.someadress.com";
        buttonLabel = "someLabel";

        var btnCol:DataGridColumn = new DataGridColumn("buttonColumnTitle");

        btnCol.cellRenderer = ButtonCell;
        btnCol.editable = false;
        tabulka.addColumn(btnCol);

这里是ButtonCell类:

package 
{
import flash.desktop.Clipboard;
import fl.controls.Button;
import fl.controls.listClasses.ICellRenderer;
import fl.controls.listClasses.ListData;
import flash.events.Event;
import flash.events.MouseEvent;


public class ButtonCell extends Button implements ICellRenderer
{
    private var _listData:ListData;
    private var _data:Object;

    public var _buttonLabel:String;
    public var _googleURL:String;


    public function ButtonCell()
    {
        super();

        //_buttonLabel = buttonLabel;
        //_googleURL = googleURL;

        this.label = _buttonLabel;

        addEventListener(MouseEvent.CLICK, onButtonClick);
    }

    public function set data(d:Object):void
    {
        _data = d;
    }

    public function get data():Object
    {
        return _data;
    }

    public function get listData():ListData
    {
        return _listData;
    }

    public function set listData(value:ListData):void
    {
        _listData = value;
    }

    override public function get selected():Boolean
    {
        return _selected;
    }

    override public function set selected(value:Boolean):void
    {

    }

    public function onButtonClick(event:MouseEvent)
    {
        System.setClipboard(_googleURL);
    }
}
}
#######################################

这是完美的,这就是问题所在。非常感谢!!但是还有一件事我不确定...... 当我想添加更多按钮时,它只显示最后一个...我不确定是什么问题。这是样本sript,它绘制了5个项目但是按钮 - 只有最后一个......

import ButtonCell;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.controls.listClasses.CellRenderer;
import fl.controls.DataGrid;
import fl.data.DataProvider;

var googleURL:String = "www";
var buttonLabel:String = "bt";

var provide = new DataProvider;

var tabulka:DataGrid = new DataGrid();
tabulka.x = 50;
tabulka.y = 50;
addChild(tabulka);
tabulka.setSize(400, 300);
tabulka.columns = ["Parcela","Odkaz"];
tabulka.columns[0].width = 50;
tabulka.columns[1].width = 350;
tabulka.selectable = false;
tabulka.dataProvider = provide;

var btnCol:DataGridColumn = new DataGridColumn("Copy");
tabulka.addColumn(btnCol);
for (var i = 0; i<5; i++){
    provide.addItem({Parcela:"Item "+i});
    btnCol.cellRenderer = new ButtonCell(buttonLabel = "bt"+i, googleURL = "www" + i);
}

1 个答案:

答案 0 :(得分:0)

首先,您需要将参数传递给构造函数

public function ButtonCell(buttonLabel:String, googleURL:String)
{
    super();

    _buttonLabel = buttonLabel;
    _googleURL = googleURL;

    this.label = _buttonLabel;

    addEventListener(MouseEvent.CLICK, onButtonClick);
}

然后在创建一个新的ButtonCell时,您只需要传递参数:

btnCol.cellRenderer = new ButtonCell(googleURL,buttonLabel);