0

I am trying to modify a facet in my .xhtml file programatically. I am using Primefaces 6.1.

I have the following code in .xhtml

<p:menubar model="#{menu.model}" id="myMenu">
    <f:facet name="options">

    </f:facet>
</p:menubar>

In my bean I have the following code to update "options" facet.

@PostConstruct
public void initMenu(){     
// getting data is skipped
    HtmlPanelGrid mainPanel = new HtmlPanelGrid();
    HtmlOutputLabel htmlOutputLabel = new HtmlOutputLabel();
    htmlOutputLabel.setValue("Search Template");
    mainPanel.getFacets().put("options",htmlOutputLabel);    
}

mainPanel.getFacets() returns no facets hence can't add the label.

How can I programmatically access and modify the facet? I am stumped.

svager
  • 769
  • 5
  • 13
  • 32
  • Why should a newly (in a wrong way btw) created **panelgrid** return the facets of a non-related menubar??? – Kukeltje Nov 18 '17 at 08:07

1 Answers1

0

In order to getFacets() from menubar you need to get reference on instance of existing menubar.

You can achieve this by binding p:menubar to server side component instance.

  1. In you managed bean add

private org.primefaces.component.menubar.Menubar menubar;

and add getter and setter.

  1. Add binding attribute to p:menubar like this

    <p:menubar model="#{menu.model}" id="myMenu" binding="#{menu.menubar}">
        <f:facet name="options">
    
        </f:facet>
    </p:menubar>
    
  2. Inside @PostConstruct method add

    @PostConstruct
    public void initMenu() {
        // getting data is skipped
        //initMenuModel();
    
        menubar=(Menubar)FacesContext.getCurrentInstance().getApplication().createComponent(Menubar.COMPONENT_TYPE);
        HtmlOutputText htmlOutputLabel = new HtmlOutputText();
        htmlOutputLabel.setValue("Search Template");
        Map<String, UIComponent> facets = menubar.getFacets();
        facets.put("options", htmlOutputLabel);
    }
    

Also check recommendations and best practices regarding JSF 'binding' attribute.

Dusan Kovacevic
  • 1,377
  • 1
  • 13
  • 19
  • Thanks for trying to answer. Sure thus works with the declaratively added menumodel? And it is not good practice to create components via 'new'. – Kukeltje Nov 18 '17 at 09:22
  • I've tested this solution with a case that user posted (no declarative data model). I agree its not best practice but I think there is no harm in this case. – Dusan Kovacevic Nov 18 '17 at 09:40
  • Sorry, I meant the reference to the menuModel was declared via an attribute, not a declarative menu structure. OP has that in his question. – Kukeltje Nov 18 '17 at 09:53
  • @Kukeltje, ok. Now I understand what you meant. Anyways, I've tested solution and it works as expected. – Dusan Kovacevic Nov 18 '17 at 18:03
  • Still, doing 'new MenuBar()' is not good practice... You should use the `FacesContext.getCurrentInstance().getApplication()` and create the component this way – Kukeltje Nov 18 '17 at 18:12
  • @Kukeltje, thank you for pointing that. I've updated my answer and also added additional resource for learning. – Dusan Kovacevic Nov 18 '17 at 19:29