Blessed Geek
Blessed Geek

Reputation: 21614

GXT 3: GridSelectionModel SelectionEvent/SelectionChangedEvent not firing

GXT 3 question:

Does anyone have any idea why these two handlers are not triggered when I select any row on the grid? SelectionModel is GridSelectionModel, set to single selection.

Are there any further declaration to be done? It is as simple as this, isn't it. I had no problem adding event listeners for GXT 2.2, but there are some changes in GXT 3. These events are for detecting row selections, aren't they?

SelectionChangedHandler<Summary> gridSelectionChangedHandler = 
  new SelectionChangedHandler<Summary>() {
    public void onSelectionChanged(
      SelectionChangedEvent<Summary> event) {
        Summary rec = event.getSelection().get(0);
        Window.alert("Row = "+ rec.getId());    
    }
  };

SelectionHandler<Summary> gridSelectionHandler =
  new SelectionHandler<Summary>() {
    public void onSelection(SelectionEvent<Summary> event) {
      Summary rec = event.getSelectedItem();
      Window.alert("Row = "+ rec.getId());    
    }
  };

public SummaryViewImpl() {
  uiBinder.createAndBindUi(this);
  this.grid.addSelectionChangedHandler(gridSelectionChangedHandler);
  this.grid.addSelectionHandler(gridSelectionHandler);
}

However, I have no problem with RowClickEvent, as the following is firing:

@UiHandler({ "grid" })
void onRowClick(RowClickEvent event){
    int row = event.getRowIndex();
    Window.alert("Row = "+ row);        
}

grid is an instance of SummaryGrid:

public class SummaryGrid
extends Grid<Summary>{


  {
    this.sm = new GridSelectionModel<Summary>();
    this.sm.setSelectionMode(SelectionMode.SINGLE);
  }


  blah ... blah ...

  public HandlerRegistration addSelectionChangedHandler(SelectionChangedHandler<Summary> handler){
    return this.getSelectionModel().addSelectionChangedHandler(handler);
  }

  public HandlerRegistration addSelectionHandler(SelectionHandler<Summary> handler){
    return this.getSelectionModel().addSelectionHandler(handler);
  }

  blah ... blah ...
}

Upvotes: 0

Views: 3245

Answers (2)

Blessed Geek
Blessed Geek

Reputation: 21614

Never mind! Made the mistake of accessing local private variable rather than accessing property get/set methods.

In the extension class, do this:

this.setSelectionModel(selectionModel);
this.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);

rather than doing this:

this.sm =selectionModel;
this.sm.setSelectionMode(SelectionMode.SINGLE);

because, the setter has to bind the selection model which is not performed when I access sm variable directly.

Lesson reinforced: Should access setters/getters rather than accessing their associated local variables, when extending classes.

Upvotes: 0

user1258245
user1258245

Reputation: 3639

try this grid.getSelectionModel().addSelectionChangedHandler

not sure if you need to set the selection mode first or not but my working code is like:

grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);

grid.getSelectionModel().addSelectionChangedHandler(new SelectionChangedHandler<Summary>() {
...
}

Upvotes: 3

Related Questions