将 clickHandler 添加到 GWT 中 CellTable 的行?

发布于 2024-11-03 10:14:44 字数 502 浏览 0 评论 0原文

我创建了一个基本的 CellTable 并填充了一些数据。现在我想向每一行添加一个 clickHandler 但我不知道如何执行此操作。我已经为整个表创建了一个 clickEvent,但我想要为每一行创建一个 clickEvent。

    table.sinkEvents(Event.ONCLICK);
    table.setTitle("Click me");
    table.setSize("600px", "600px");
    table.addDomHandler(new ClickHandler()
    {
        @Override
        public void onClick(ClickEvent event)
        {
            Window.alert("You clicked!" +);

        }
    }, ClickEvent.getType());

我可以做类似的事情来为每一行添加 clickEvent 吗?

I have created a basic CellTable and filled it with some data. Now I want to add a clickHandler to each row but I'm not sure how to do this. I've created a clickEvent for the whole table but I want one for each row.

    table.sinkEvents(Event.ONCLICK);
    table.setTitle("Click me");
    table.setSize("600px", "600px");
    table.addDomHandler(new ClickHandler()
    {
        @Override
        public void onClick(ClickEvent event)
        {
            Window.alert("You clicked!" +);

        }
    }, ClickEvent.getType());

Can I do something similar to add clickEvent for each row?

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

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

发布评论

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

评论(2

烟凡古楼 2024-11-10 10:14:44

CellTable 内置了对处理单击事件的支持。您可以添加一个 CellPreviewHandler,当单击一行时将调用该单元。它将接收事件中的许多项目,例如本机事件、单元格和数据行值。因为它不仅会触发单击事件,所以您需要检查单击事件是否被触发。只需测试传递的事件:boolean isClick = "click".equals(event.getNativeEvent().getType())。

另一种选择是扩展受保护的方法 doSelection,但它已被弃用,并且您需要确保设置了正确的 KeyboardSelectionPolicy 以确保在单击时调用它完毕。请参阅接口 KeyboardSelectionPolicy 的 JavaDoc 中的后者。

A CellTable has built in support for handling click events. You can add a CellPreviewHandler that will be called among others when a row is clicked. It will receive a number of items in the event like the native event, cell, and data row value. Because it fires not only for click events you need to check if the click event was fired. Simply test the event passed: boolean isClick = "click".equals(event.getNativeEvent().getType()).

Another option is to extend the protected method doSelection, but it's deprecated and in you need to als make sure you have the right KeyboardSelectionPolicy set to make sure it's called when a click is done. See for the latter in the JavaDoc of the interface KeyboardSelectionPolicy.

傲世九天 2024-11-10 10:14:44

选择单元格的另一种方法可以使用 NoSelectionModel 并将其添加到表中:

//EDIT: this is a field, not a local variable
TheCellObject clickedObject; //the object selected by selectionModel

final NoSelectionModel<TheCellObject> selectionModel = new NoSelectionModel<TheCellObject>();

    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

                @Override
                public void onSelectionChange(SelectionChangeEvent event) {
                    clickedObject = selectionModel.getLastSelectedObject();
                }
            });
cellTable.setSelectionModel(selectionModel); //add selection model to your celltable

Another way to have a cell selected can be made using a NoSelectionModel and add it to the table:

//EDIT: this is a field, not a local variable
TheCellObject clickedObject; //the object selected by selectionModel

final NoSelectionModel<TheCellObject> selectionModel = new NoSelectionModel<TheCellObject>();

    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

                @Override
                public void onSelectionChange(SelectionChangeEvent event) {
                    clickedObject = selectionModel.getLastSelectedObject();
                }
            });
cellTable.setSelectionModel(selectionModel); //add selection model to your celltable
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文