GWT CellList单击以切换选择(多选)

时间:2011-10-26 14:05:05

标签: gwt multi-select gwt-2.2-celltable gwt-2.4

我想设置一个CellList,以便点击一行将切换选择。这样就可以选择多行而不需要按住ctrl键。

我需要更改什么才能让它正常工作?

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>()));

2 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:3)

“无论您是否添加了复选框列,都必须add a cell preview handler。最简单的定义方法是使用DefaultSelectionEventManager,使用checkbox manager组合一个复选框列,或creating a custom one(您将点击事件映射到toggle action)。

您可以在GWT Showcase中看到它使用了复选框变体;它使用带有两个参数的setSelectionModel重载来同时添加CellPreviewEvent.Handler。“

(归功于this answer