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

发布于 2024-12-11 18:17:11 字数 768 浏览 2 评论 0原文

我想设置一个 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>()));

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白云悠悠 2024-12-18 18:17:11
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);
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);
箹锭⒈辈孓 2024-12-18 18:17:11

“无论您是否添加复选框列,您都必须添加单元格预览定义处理程序的最简单方法是使用 DefaultSelectionEventManager,可以使用 复选框管理器与复选框列组合,或创建自定义事件(您可以将点击事件映射到 切换操作)。

您可以在 GWT Showcase;它使用带有两个参数的 setSelectionModel 重载来同时添加 CellPreviewEvent.Handler。”

(来源 这个答案

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文