Hassen Ch.
Hassen Ch.

Reputation: 1751

GWT DataGrid Selection Handler

I have a DataGrid, and I want to get the selected row when it is clicked: I want to get its content and its index. I have some sort of a form under the DataGrid, and when one row is selected, the form will be filled with the data in the selected row above! Then when I click a button a DialogBox should be shown with the data in the selected row!

I have searched but there is no clear explanation about how to do it. Thank you

Upvotes: 1

Views: 5654

Answers (2)

PVR
PVR

Reputation: 2524

DataGrid table = new DataGrid();

final SingleSelectionModel<Contact> selectionModel =
                                           new SingleSelectionModel<Contact>();
table.setSelectionModel(selectionModel); 

Button clickBtn = new Button("Click Button");
clickBtn.addClickHandler(new ClickHandler(){
Contact selectedContact = ((SingleSelectionModel)table.getSelectionModel()).getSelectedRecord();
setDataInForm(selectedContact);
});

Upvotes: 3

Suresh Atta
Suresh Atta

Reputation: 121998

In API of the Gwt DataGrid, there is one example on how to use GWT DataGrid and selection model.

In that example:

// Add a selection model to handle user selection. 


final SingleSelectionModel<Contact> selectionModel =
                                           new SingleSelectionModel<Contact();
table.setSelectionModel(selectionModel); 
selectionModel. addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
 public void onSelectionChange( SelectionChangeEvent event) { 
 Contact selected = selectionModel. getSelectedObject();
 if (selected != null) {
 Window.alert("You selected: " + selected.name); }
 } });

Upvotes: 3

Related Questions