簡體   English   中英

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

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

使用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);
   }
});     

Vaadin 8中存在Action.Handler,但是無法在Vaadin 8中將Action.Handler添加到網格中,我也找不到其他方法來創建上下文菜單。

在網格中使用動作框架的方式是什么? Grid還有其他創建上下文菜單的方法嗎? 換句話說,上面的示例將如何編寫。

現有的文章和答案(例如Vaadin Grid vs Table )沒有涵蓋上述主題,並且在Vaadin docs( https://vaadin.com/docs/-/part/framework/components/components-components-grid.html )。

您可以使用Vaadin 7.6在線演示github源碼以來引入的vaadin-context-menu插件 從理論上講,它可以支持任何組件,但是對於網格,我們將使用專用的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 + '\'' +
                    '}';
        }
    }
}

結果:

網格上下文菜單

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM