简体   繁体   English

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

[英]How to use Vaadin Action Framework in Grid of Vaadin 8

Using the Table class of Vaadin one could add an action handler to a table. 使用Vaadin的Table类可以将动作处理程序添加到表中。 For example in previous Vaadin versions the following 2 options could be shown to the screen when a user right-click inside the table area: 例如,在以前的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 exist in Vaadin 8 however it is not possible to add an Action.Handler to a Grid in Vaadin 8, nor have I found any other way to create a context menu. Vaadin 8中存在Action.Handler,但是无法在Vaadin 8中将Action.Handler添加到网格中,我也找不到其他方法来创建上下文菜单。

What would be the way to use the Action Framework in a Grid? 在网格中使用动作框架的方式是什么? Does Grid have any other method for creating a context menu? Grid还有其他创建上下文菜单的方法吗? In other words, how would the example above be written. 换句话说,上面的示例将如何编写。

Existing articles and answers (eg Vaadin Grid vs Table ) do not cover the topic above, and it is not documented in Vaadin docs ( https://vaadin.com/docs/-/part/framework/components/components-grid.html ). 现有的文章和答案(例如Vaadin Grid vs Table )没有涵盖上述主题,并且在Vaadin docs( https://vaadin.com/docs/-/part/framework/components/components-components-grid.html )。

You can use the vaadin-context-menu add-on introduced since Vaadin 7.6 ( online demo and github source ). 您可以使用Vaadin 7.6在线演示github源码以来引入的vaadin-context-menu插件 Theoretically it can support any component, but for the grid we'll use the dedicated GridContextMenu (remember to recompile your widgetset). 从理论上讲,它可以支持任何组件,但是对于网格,我们将使用专用的GridContextMenu (记住要重新编译窗口小部件集)。

Dependency: 依赖关系:

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

Implementation: 实现方式:

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 + '\'' +
                    '}';
        }
    }
}

Result: 结果:

网格上下文菜单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM