Reputation: 1961
I'd like to setup a CellList so that clicking a row will toggle the selection. Such that multiple rows can be selected with out the need for holding the ctrl key.
What do I need to change to get it working?
class ToggleEventTranslator<T> implements DefaultSelectionEventManager.EventTranslator<T> {
@Override
public boolean clearCurrentSelection(final CellPreviewEvent<T> event) {
return false;
}
@Override
public SelectAction translateSelectionEvent(final CellPreviewEvent<T> event) {
return SelectAction.TOGGLE;
}
}
MultiSelectionModel<ObjProxy> multiSelectionModel = new MultiSelectionModel<ObjProxy>();
ocjCellList.setSelectionModel(multiSelectionModel, DefaultSelectionEventManager
.<ObjProxy> createCustomManager(new ToggleEventTranslator<ObjProxy>()));
Upvotes: 9
Views: 7761
Reputation: 1961
list.addCellPreviewHandler(new Handler<T>() {
@Override
public void onCellPreview(final CellPreviewEvent<T> event) {
if (BrowserEvents.CLICK.equals(event.getNativeEvent().getType())) {
final T value = event.getValue();
final Boolean state = !event.getDisplay().getSelectionModel().isSelected(value);
event.getDisplay().getSelectionModel().setSelected(value, state);
event.setCanceled(true);
}
}
});
private final MultiSelectionModel<T> selectModel = new MultiSelectionModel<T>();
final Handler<T> selectionEventManager = DefaultSelectionEventManager.createCheckboxManager();
list.setSelectionModel(selectModel, selectionEventManager);
Upvotes: 9
Reputation: 22859
"Whether you add a checkbox column or not, you'll have to add a cell preview handler. The easiest way to define one is to use DefaultSelectionEventManager, either using a checkbox manager in combination with a checkbox column, or creating a custom one (you'd map a click event into a toggle action).
You can see it used, the checkbox variant, in the GWT Showcase; it uses the setSelectionModel
overload with two arguments to add the CellPreviewEvent.Handler
at the same time."
(Credit to this answer)
Upvotes: 3