Hello!
I am working on a Jmix 2.4.4 project.
Currently, I am dynamically creating menu items (ListMenu.MenuItem
) with a click handler (withClickHandler(...)
) that handles navigation inside the same browser tab.
Problem:
I would like the menu items to behave like standard HTML <a>
links instead of programmatic navigation, so that when a user clicks a menu item, it opens the view in a new browser tab (target="_blank"
).
In other words, I want to replace the ClickHandler
with an Anchor
component that navigates via href
.
How can I correctly implement this behavior with AppListMenu.BadgeMenuItem
?
Is it possible to attach an Anchor
component directly to a BadgeMenuItem
?
If not, what is the best approach — extending BadgeMenuItem
, using a custom Component
, or another solution?
private ListMenu.MenuItem badgeMenu(Navigation navigation, Class<? extends View<?>> view, RouteParameters route) {
return new AppListMenu.BadgeMenuItem(navigation.getCrcId())
.withBadge(navigationService.getBadge(navigation))
.withTitle(translateService.translate(
navigation,
HasName.HasShortName.SHORT_NAME,
locale.getLanguage(),
navigation.getShortName()
))
.withClassNames(List.of("sub-menu"))
.withClickHandler(item -> {
listMenu.getElement().executeJs("""
const activeMenus = document.getElementsByClassName("active");
for (let i = 0; i < activeMenus.length; i++) {
activeMenus[i].classList.remove("active");
}
document.getElementById($0).classList.add("active");
""", navigation.getCrcId());
viewNavigators.view(origin, view)
.withRouteParameters(route)
.withQueryParameters(QueryParameters.of("navId", navigation.getCrcId()))
.withBackwardNavigation(true)
.navigate();
});
}