tags:

views:

265

answers:

2

hi all,

i have a grid of data and buttons, how can i refer to the data in grid or the row that was click in the context of the button?

meaning: the button clickHandler receive a clickEvent object and nothing else. so how can i get to the table data from it?

thanks me

A: 

You need to store some sort of model your click handler will have access to. The model should allow you to correlate the data in the row with the button. There is not a built in way to do this in GWT.

Carnell
+1  A: 

There is not a built in way in GWT to do this, nor would it make a whole bunch of sense to put it in there. You mention a grid, so I'm guessing you have data that roughly approximates a matrix of some form, while making lots of assumptions, the rough technique you might want is something like this: (no compiler here warning)

final Map<Button,Object> buttonToCellMap = new HashMap<Button,Object>();
ClickHandler myClickHandler = new ClickHandler() {
  public void onClick(ClickEvent event){
    Object thingInCell = buttonToCellMap.get((Button)event.getSource());
    //do something with the thing in your grid here
  }
}


for( List yourRow : matrix ){
  for( Object yourObject : yourRow ){
    //logic to make your grid cell goes here

    Button aButton = new Button();
    buttonToCellMap.put(aButton,yourObject);
    aButton.addClickHandler(myClickHandler);        
  }
}

This will give you access to the object you care about in location x,y in the grid when the corresponding button has been clicked.

bikesandcode
That's pretty much exactly what I have done in the past for this same sort of issue. I probably should have put that in my original response.
Carnell