简体   繁体   English

如何使用 PrimeFaces 将 pe:badge 添加到 bean 中动态创建的菜单

[英]How to add pe:badge to dynamically created menu in bean using PrimeFaces

I'm rewriting static menu to dynamic, since our customer wants to dynamically change the menu on the fly.我正在将 static 菜单重写为动态,因为我们的客户希望动态更改菜单。 Before that I had standard <p:menu> <p:menuItem> </p:menu> structure in my xhtml.在此之前,我的 xhtml 中有标准的 <p:menu> <p:menuItem> </p:menu> 结构。

But now I have changed it to: <p:menu model="#{pageTemplateView.menuModel}"/>但现在我已将其更改为: <p:menu model="#{pageTemplateView.menuModel}"/>

And I'm creating model in my backing bean like this:我在我的支持 bean 中创建 model,如下所示:

DefaultMenuItem menuItem = new DefaultMenuItem();
menuItem.setIcon(item.getIcon());
menuItem.setTarget(item.getLink());
menuItem.setValue(item.getName());

But the problem is I don't know how to add <pe:badge> component inside menu item from bean.但问题是我不知道如何在 bean 的菜单项中添加<pe:badge>组件。

Before that I included badges to menus the following way:在此之前,我通过以下方式将徽章添加到菜单中:

<p:menuitem id="tasks_icon_menuitem_id" icon="fa fa-tasks" url="#">
  <pe:badge content="#{badgeCountBean.badgeCount()}"/>
</p:menuitem> 

So how do I add <pe:badge> to dynamically created menu in bean?那么如何将<pe:badge>添加到 bean 中动态创建的菜单中呢?

I'm using PrimeFaces 8我正在使用 PrimeFaces 8

After few hours of testing and plying around I found the issue that prevents me from adding badge to default menu item.经过几个小时的测试和四处游荡,我发现了阻止我将徽章添加到默认菜单项的问题。 The issue is when you try to add child to DefaultManuItem like this:问题是当您尝试像这样将子项添加到DefaultManuItem时:

Badge badge = new Badge();
DefaultMenuItem defaultMenuItem = new DefaultMenuItem();
defaultMenuItem.getChildren().add(badge);

You get unsupported operation exception since DefaultMenuItem:: getChildren() is implemented like this:由于 DefaultMenuItem:: getChildren() 是这样实现的,因此您会得到不受支持的操作异常:

public List<UIComponent> getChildren() {
    return Collections.emptyList();
}

But I have found an ugly workaround for this issue.但我发现了一个丑陋的解决这个问题的方法。 So I might share it and maybe well get to better solution eventuality.所以我可能会分享它,并且可能会得到更好的解决方案。 In my xhtml I added empty menu and gave it fixed ID like this:在我的 xhtml 中,我添加了空菜单并为其提供了固定 ID,如下所示:

<!-- MENU PLACEHOLDER -->
<p:menu id="dynamic_menu_placeholder"/>

<!-- need to add dummy badge to xhtml otherwise it wont be displayed -->
<pe:badge/>

And then in my bean I got the component by ID and added UIMenuItem elements to it like this:然后在我的 bean 中,我通过 ID 获取组件并向其中添加UIMenuItem元素,如下所示:

// get component by ID
UIComponent component = FacesContext.getCurrentInstance().getViewRoot().findComponent(":form:dynamic_menu_placeholder");
Menu menu = (Menu) component;
menu.getChildren().clear();

for (MenuItem item : menuItemList) {

    // creating menu item
    UIMenuItem menuItem = new UIMenuItem();
    menuItem.setUrl(item.getLink());
    menuItem.setValue(item.getValue());
    menuItem.setId(... generated some id);
    
    // creating badge
    Badge badge = new Badge();
    badge.setContent(...getBadgeCount()..);
    
    // adding badge to menu item
    menuItem.getChildren().add(badge);
}

// addin menu item to menu
menu.getChildren().add(menuItem);

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

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