tags:

views:

64

answers:

4
             for (iss = 0; iss < listOfProductIds2.length; iss++)
  {
      // Alert.show(listOfProductIds2[iss]);
                        var productMain:VBox=new VBox();
       var p1:HBox=new HBox();
     var l1:Label=new Label();
     var b1:Button=new Button();
     var spacer:Spacer=new Spacer();
     spacer.width=300;
     b1.label="Remove";
     b1.setConstraintValue("id","");
       b1.addEventListener(MouseEvent.CLICK,removeProduct); 
      l1.text="Product "+iss;
     p1.setActualSize(500,500);
      p1.addChild(l1);
     p1.addChild(spacer);
     p1.addChild(b1);
     productMain.addChild(p1); 
  }

          function removeProduct(event:MouseEvent):void
 { 
  // How do i know which button is clicked 
 }
A: 

event.target should point to the Button you clicked on, shouldn't it ? However you should probably give ids to the buttons to be able to differenciate them (since you create them dynamically.)

phtrivier
ok it's fine .. but how do i know that which id u have clicked
Tushar Ahirrao
A: 

Look at event.target.

Andrew Aylett
+2  A: 

Use event.currentTarget (instead of event.target) because event.target might be the Label component or some styling component within the button, but currentTarget is assured to be the object with which the listener was registered.

To get a handle to the button that was clicked you can just cast the currentTarget to a button.

function removeProduct(event:MouseEvent):void
{ 
    var b1:Button = Button(event.currentTarget);
}

The method setConstraintValue is for setting layout constraints, not setting id. The id property is used by mxml for creating variable names for objects. You can get/set id as you would get/set any other property (say width) - but neither have I seen anyone doing that nor do I see any need to do that in the first place.

Amarghosh
Thanks for the info about currentTarget. In this situation, setting the id could be interesting as the buttons are created dynamically, so for example : b1.id = "button_" + listOfProductIds2[iss];.. then later you can check the id of the button to see which one was clicked. (Although that's not very clean, since it mixes id with business-related info.)I suppose the problem here is to know the productId corresponding to the button. Lots of way to do it... One could have a ProductButton class with a product id as a public, know property ... or some maps between product Ids and Buttons ?
phtrivier
A: 

If ids are assigned dynamically as in the example given b1.id = "button_" + listOfProductIds2[iss] Then the function that processes the click event would look at the currenttarget, and what I usually do is do a string replace on the part of the id that you know is not dynamic like "button_" with "", which leaves you with the name of the product.

joyceschan