9

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>()));
Nick Siderakis
  • 1,961
  • 2
  • 21
  • 39

2 Answers2

9
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);
Nick Siderakis
  • 1,961
  • 2
  • 21
  • 39
  • what does 'selectModel' definition look like? – Carl Apr 24 '13 at 09:13
  • I've tried this code with a CellList and it doesn't enable multiple items to be selected. Is there more to the implementation? – Carl Apr 24 '13 at 14:29
  • Good catch, using this selection model should enable multiple items to be selected. private final MultiSelectionModel selectModel = new MultiSelectionModel(); – Nick Siderakis Apr 28 '13 at 12:54
3

"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)

Community
  • 1
  • 1
Chris Cashwell
  • 22,308
  • 13
  • 63
  • 94
  • 1
    Thanks for the post. How does it apply to a CellList? Achieving Toggle-on-click behavior with a CellTable is straightforward, but I haven't been able to get it working with a CellList. – Nick Siderakis Oct 27 '11 at 15:59