Pergunta


For loop generates Button


I wanted to generate dynamic amount button. And wanted to recognize which one was clicked. This is real code...

for(var col:int = 1; col <= MaxColumn; col++){
  for(var row:int = 1; row <= MaxRow; row++){
    makeGrid(col, row);
    Button["btn_" + col + "_" + row] = new Button();
    Button["btn_" + col + "_" + row].label = col + "行" + row + "列" ;
    Button["btn_" + col + "_" + row].setStyle("fontSize", 10);
    Button["btn_" + col + "_" + row].setStyle("cornerRadius", 0);
    Button["btn_" + col + "_" + row].x = 1150 - 500/MaxRow * (row - 1);
    Button["btn_" + col + "_" + row].y = 150 + 500/MaxColumn * (col - 1);
    Button["btn_" + col + "_" + row].width = 500/MaxRow;
    Button["btn_" + col + "_" + row].height = 500/MaxColumn;
    Button["btn_" + col + "_" + row].setStyle("color", 0x191970);
    Button["btn_" + col + "_" + row].setStyle("fontSize", 7);
    Button["btn_" + col + "_" + row].addEventListener(MouseEvent.CLICK, onGridClick(col, row));
    rootPanel.addElement(Button["btn_" + col + "_" + row]);
  }
}

private function onGridClick(col, row):void{
  makeGridId(col, row);
  return function(event:Event):void{
    var createGridDialog:CreateGridDialog = PopUpManager.createPopUp(this, CreateGridDialog, true) asCreateGridDialog;
    PopUpManager.centerPopUp(createGridDialog);
    createGridDialog.setGridId(globalGridId);
    createGridDialog.setMaxRow(MaxRow);
    createGridDialog.addEventListener(CreateGridEvent.GRID_SUBMIT, tmpSave);
  }
}

public function makeGridId(col:int, row:int):void{
  globalGridId = (col - 1) * MaxRow + row;
}    

addEventListener's callback function is difficult


Because, in

private function onGridClick(col, row):void{

,

  makeGridId(col, row);

repeated in each for blocks, so globalGridId is overwritten in the end after compile.(row = MaxRow,col = MaxColumn) If the callback function could accept col&row directly, this issue would close. But addEventListener's callback is difficult...

addEventListener(MouseEvent.CLICK, **onGridClick**)

The callback basically accept only one argument:MouseEvent directly, and only return void type.

I want to make globalGridId when I click, and I want to send it to where I click.

How should I do?


I Notice


please see this...

<s:Button id="Grid1" x="220" y="0" width="80" height="80" click="onSubClick(event, 0)"/>

private function onSubClick(event:MouseEvent, division:int):void {
  var subSymbolDialog:SubSymbolDialog = PopUpManager.createPopUp(this, SubSymbolDialog, true) as SubSymbolDialog;
  PopUpManager.centerPopUp(subSymbolDialog);
  subSymbolDialog.setDivision(division);
  subSymbolDialog.addEventListener(SubSymbolEvent.SUB_CLOSE, onSubClose);
}

this is weird.

click="onSubClick(event, 0)"

Why this is working?

Foi útil?

Solução

you cannot pass arguments to listeners callback functions. Try extends the buttonClass as follow:

package
{
    public class MyButton extends Button
    {

        public var row:int;
        public var col:int;

        public function MyButton()
        {
            super();
        }
    }
}

After that you just have to fill the row and col property for the buttons.

for(var col:int = 1; col <= MaxColumn; col++){
    for(var row:int = 1; row <= MaxRow; row++){
        makeGrid(col, row);
        Button["btn_" + col + "_" + row] = new MyButton();
        Button["btn_" + col + "_" + row].col = col;
        Button["btn_" + col + "_" + row].row = row;
        //...

on the callback function you can retrieve it as follow:

private function onGridClick(e:Event):void
{
    var b:MyButton = event.target as MyButton;

    makeGridId(b.col, b.row);

    var createGridDialog:CreateGridDialog = PopUpManager.createPopUp(this, CreateGridDialog, true) asCreateGridDialog;
    PopUpManager.centerPopUp(createGridDialog);
    createGridDialog.setGridId(globalGridId);
    createGridDialog.setMaxRow(MaxRow);
    createGridDialog.addEventListener(CreateGridEvent.GRID_SUBMIT, tmpSave);
}

PS: I warn you about the use of your array named Button exactly the same a s the class Button.

If you can use upercase first letters only for class name and keep lowercase first letter for variable, it's better (ex: MyClassName, myVariableName) ;)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top