Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT CellList Click to Toggle selection (Multi-Selection)

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>()));
like image 707
Nick Siderakis Avatar asked Feb 03 '23 12:02

Nick Siderakis


2 Answers

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);
like image 88
Nick Siderakis Avatar answered Feb 20 '23 01:02

Nick Siderakis


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

like image 32
Chris Cashwell Avatar answered Feb 20 '23 00:02

Chris Cashwell