如何在Vaadin 8的网格中使用Vaadin Action Framework

Spy*_*s K 1 java datagrid right-click vaadin8

使用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);
   }
});     
Run Code Online (Sandbox Code Playgroud)

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

在网格中使用Action Framework的方法是什么?Grid是否有任何其他方法来创建上下文菜单?换句话说,如何写上面的例子.

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

Mor*_*fic 7

您可以使用自Vaadin 7.6(在线演示github源)以来引入的vaadin-context-menu附加组件.从理论上讲它可以支持任何组件,但对于网格,我们将使用专用(记得重新编译您的widgetset).GridContextMenu

相关性:

<dependency>
   <groupId>com.vaadin</groupId>
   <artifactId>vaadin-context-menu</artifactId>
   <version>2.0.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

执行:

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 + '\'' +
                    '}';
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

网格上下文菜单