Vaadin: 防止点击MenuBarRootItem时关闭MenuBar。

huangapple go评论92阅读模式
英文:

Vaadin : preventing MenuBar from closing when MenuBarRootItem is clicked

问题

我使用Vaadin 24。我有一个MenuBar,并且我想修改MenuBar的默认行为,使其在单击项目时不会关闭(以便能够多次单击它们)。有人知道如何实现这一点吗?

这是我的代码:

  1. MenuBar menu = new MenuBar();
  2. menu.addItem(VaadinIcon.PLUS.create(), event -> increase());
  3. menu.addItem(VaadinIcon.MINUS.create(), event -> decrease());
英文:

I am using vaadin 24. I have a MenuBar and I would like to modify the default behavior of MenuBar so that it doesn't close when I click on the items (to be able to click them several times). Do someone know how to achieve this please ?

Here's my code :

  1. MenuBar menu = new MenuBar();
  2. menu.addItem(VaadinIcon.PLUS.create(), event -> increase());
  3. menu.addItem(VaadinIcon.MINUS.create(), event -> decrease());

答案1

得分: 1

这是一个已知的遗漏1。以下是来自问题单的解决方法:

  1. /*
  2. * 当单击菜单项时,不要关闭包含该菜单项的覆盖层({@link SubMenu})。
  3. */
  4. public class RetainOpenedMenuItemDecorator {
  5. public static final String CHECKED_ATTR = "menu-item-checked";
  6. private final MenuItem menuItem;
  7. public RetainOpenedMenuItemDecorator(MenuItem menuItem) {
  8. this.menuItem = menuItem;
  9. menuItem.getElement().addEventListener("click", e -> {
  10. if (!menuItem.isCheckable()) {
  11. return;
  12. }
  13. // 需要这个操作,因为也阻止了 UI 更改
  14. if (menuItem.isChecked()) {
  15. menuItem.getElement().executeJs("this.setAttribute('" + CHECKED_ATTR + "', '')");
  16. } else {
  17. menuItem.getElement().executeJs("this.removeAttribute('" + CHECKED_ATTR + "')");
  18. }
  19. }).addEventData("event.preventDefault()");
  20. }
  21. public MenuItem getMenuItem() {
  22. return menuItem;
  23. }
  24. public static void keepOpenOnClick(MenuItem menuItem) {
  25. new RetainOpenedMenuItemDecorator(menuItem);
  26. }
  27. public static void keepOpenOnClick(SubMenu subMenu) {
  28. subMenu.getItems().forEach(RetainOpenedMenuItemDecorator::keepOpenOnClick);
  29. }
  30. }
英文:

It's a known omission. Here's a workaround from the ticket:

  1. /*
  2. * Do not close the overlay ({@link SubMenu} containing this menu item when clicked.
  3. */
  4. public class RetainOpenedMenuItemDecorator {
  5. public static final String CHECKED_ATTR = "menu-item-checked";
  6. private final MenuItem menuItem;
  7. public RetainOpenedMenuItemDecorator(MenuItem menuItem) {
  8. this.menuItem = menuItem;
  9. menuItem.getElement().addEventListener("click", e -> {
  10. if (!menuItem.isCheckable()) {
  11. return;
  12. }
  13. // needed because UI change is also prevented
  14. if (menuItem.isChecked()) {
  15. menuItem.getElement().executeJs("this.setAttribute('"+ CHECKED_ATTR + "', '')");
  16. } else {
  17. menuItem.getElement().executeJs("this.removeAttribute('"+ CHECKED_ATTR + "')");
  18. }
  19. }).addEventData("event.preventDefault()");
  20. }
  21. public MenuItem getMenuItem() {
  22. return menuItem;
  23. }
  24. public static void keepOpenOnClick(MenuItem menuItem) {
  25. new RetainOpenedMenuItemDecorator(menuItem);
  26. }
  27. public static void keepOpenOnClick(SubMenu subMenu) {
  28. subMenu.getItems().forEach(RetainOpenedMenuItemDecorator::keepOpenOnClick);
  29. }
  30. }

huangapple
  • 本文由 发表于 2023年4月11日 14:30:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定