1

使用 Vaadin 的 Table 类可以将动作处理程序添加到表中。例如,在以前的 Vaadin 版本中,当用户在表格区域内右键单击时,屏幕上可能会显示以下 2 个选项:

Table aTable=new Table();
aTable.addActionHandler(new Action.Handler(){

public Action[] getActions(Object target, Object sender)
  {                           
  //example, that shows 2 options
  return new Action[] {new Action("Option 1"), new Action("Option 2")};

public void handleAction(Action action, Object sender, Object target)
  {//just prints action name for this example
   System.out.println("Action:"+action);
   }
});     

Action.Handler 存在于 Vaadin 8 中,但是无法将 Action.Handler 添加到 Vaadin 8 中的网格中,我也没有找到任何其他方法来创建上下文菜单。

在网格中使用动作框架的方法是什么?Grid 还有其他创建上下文菜单的方法吗?换句话说,上面的例子怎么写。

现有文章和答案(例如Vaadin Grid vs Table)未涵盖上述主题,并且未在 Vaadin 文档中记录(https://vaadin.com/docs/-/part/framework/components/components-grid.html)。

4

1 回答 1

7

您可以使用自 Vaadin 7.6 以来引入的vaadin-context-menu 插件在线演示github 源代码)。理论上它可以支持任何组件,但是对于网格,我们将使用专用的(记得重新编译您的小部件集)。GridContextMenu

依赖:

<dependency>
   <groupId>com.vaadin</groupId>
   <artifactId>vaadin-context-menu</artifactId>
   <version>2.0.0</version>
</dependency>

执行:

import com.vaadin.contextmenu.GridContextMenu;
import com.vaadin.icons.VaadinIcons;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Notification;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

public class MyUi extends UI {
    @Override
    protected void init(VaadinRequest request) {
        // basic grid setup
        Grid<Person> grid = new Grid<>(Person.class);
        grid.getColumns().forEach(column -> column.setHidable(true).setSortable(true));
        grid.setItems(
                new Person("Darth", "Vader"),
                new Person("Luke", "Skywalkaer"),
                new Person("Java", "De-Hut")
        );

        // grid context menu setup
        Random random = new Random();
        GridContextMenu<Person> contextMenu = new GridContextMenu<>(grid);
        // handle header right-click
        contextMenu.addGridHeaderContextMenuListener(event -> {
            contextMenu.removeItems();
            contextMenu.addItem("Hide", VaadinIcons.EYE_SLASH, selectedMenuItem -> {
                event.getColumn().setHidden(true);
            });
            contextMenu.addItem("Sort", VaadinIcons.LIST_OL, selectedMenuItem -> {
                grid.sort(event.getColumn().getId(), SortDirection.values()[random.nextInt(2)]);
            });
        });
        // handle item right-click
        contextMenu.addGridBodyContextMenuListener(event -> {
            contextMenu.removeItems();
            if (event.getItem() != null) {
                grid.select((Person) event.getItem());
                contextMenu.addItem("Info", VaadinIcons.INFO, selectedMenuItem -> {
                    Notification.show("Right-clicked item " + event.getItem());
                });
            }
        });

        // set UI content
        VerticalLayout content = new VerticalLayout();
        content.setSizeFull();
        content.addComponents(grid);
        setContent(content);
    }

    // basic bean
    public static class Person {
        private String firstName;
        private String lastName;

        public Person(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "firstName='" + firstName + '\'' +
                    ", lastName='" + lastName + '\'' +
                    '}';
        }
    }
}

结果:

网格上下文菜单

于 2017-06-01T00:17:59.367 回答