GWT CellList Click to Toggle selection (Multi-Selection)
Asked Answered
I

2

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>()));
Idiomorphic answered 26/10, 2011 at 14:5 Comment(0)
I
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);
Idiomorphic answered 12/12, 2011 at 18:36 Comment(3)
what does 'selectModel' definition look like?Triage
I've tried this code with a CellList and it doesn't enable multiple items to be selected. Is there more to the implementation?Triage
Good catch, using this selection model should enable multiple items to be selected. private final MultiSelectionModel<T> selectModel = new MultiSelectionModel<T>();Idiomorphic
D
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)

Dressingdown answered 27/10, 2011 at 1:52 Comment(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.Idiomorphic

© 2022 - 2024 — McMap. All rights reserved.