Hide submenu if it has not children - jsf

If I have a panelMenu component and some of its submenu components doesn't show children due to all its children have the rendered attribute set to false. What can I do to hide the submenu option?
For example SubMenu_1_2 and SubMenu_1_3 should be hidden:
<p:panelMenu>
<p:submenu label="SubMenu_1_1">
<p:menuitem action="#{backingBean.view_1_1_1}" value="View_1_1_1" rendered="true" />
<p:menuitem action="#{backingBean.view_1_1_1}" value="View_1_1_2" rendered="true" />
</p:submenu>
<p:submenu label="SubMenu_1_2">
<p:menuitem action="#{backingBean.view_1_2_1}" value="View_1_2_1" rendered="false" />
</p:submenu>
<p:submenu label="SubMenu_1_3">
<p:menuitem action="#{backingBean.view_1_3_1}" value="View_1_3_1" rendered="false" />
<p:menuitem action="#{backingBean.view_1_3_2}" value="View_1_3_2" rendered="false" />
<p:menuitem action="#{backingBean.view_1_3_3}" value="View_1_3_3" rendered="false" />
</p:submenu>
</p:panelMenu>
It doesn't matter if I need to use jQuery

Step 1. Give id attribute to your panelMenu.
Step 2 Now find the component from the tree and set the rendered attribute false if children is present.
<p:panelMenu id="panelMenu">
for(UIComponent component: FacesContext.getCurrentInstance().getViewRoot().getChildren()) {
if(component.getId().contains("panelMenu")){
for(UIComponent panelMenuChild:component.getChildren()){//this is the child of panelMenu ie Submenu
if(panelMenuChild.getChildren().size()>0){
((SubMenu)panelMenuChild).setRendered(false);
}
}
}
}
RequestContext.getCurrentInstance().update("panelMenu");
Step 3. then update the panelMenu;

Related

PrimeFaces menubar image

I have a problem consisting of not being able to put an icon to the left of the menubar and the menu items to the right, can anyone help me?
<h:body>
<h:form>
<p:menubar>
<f:facet name="options" >
<h:link outcome="index.xhmtl">
<p:graphicImage styleClass="a" name="img/logoGovernoTO.png"/>
</h:link>
</f:facet>
<p:menuitem value="NOTÍCIA" url="#"/>
<p:menuitem value="FÓRUM" url="#"/>
<p:menuitem value="EXPLORE" url="#"/>
<p:menuitem value="SOBRE" url="#"/>
<p:menuitem value="CONTATO" url="#"/>
</p:menubar>
</h:form>
</h:body>
I would have to stay like this
Have you tried with:
<p:graphicImage styleClass="a" name="img/logoGovernoTO.png" style="float: left" />
<p:menuitem value="NOTÍCIA" url="#" style="float: right" />

MenuBar open a dialog?

I'm trying open a dialog using Primefaces 3.5. I create a MenuBar and an view.xhtml with <p:dialog> .I want to open this view.xhtml as dialog modal.
I am using: JSF2 and Primefaces 3.5
I'm trying this
<h:form>
<p:menubar>
<!-- cadastros -->
<p:submenu label="Cadastro">
<p:submenu label="Participantes">
<p:menuitem value="Aluno" />
<p:menuitem value="Professor"/>
</p:submenu>
<p:separator/>
<p:menuitem value="Turma" onclick="#{menuMB.openDialog('/turma/view.xhtml')}"></p:menuitem>
</p:submenu>
<!-- termina cadastros -->
<!-- relatorios -->
<p:submenu label="Relatorios">
</p:submenu>
</p:menubar>
</h:form>
view.xhtml
<p:dialog header="Turmas" appendToBody="false" modal="true" widgetVar="turmaView">
<h:form>
<h:outputLabel value="I am a modal" />
</h:form>
</p:dialog>
managed bean
#ManagedBean
public class MenuMB {
public void openDialog(String view){
RequestContext.getCurrentInstance().execute("view.show()");
}
}
Any idea ?
Solved
view.xhtml
<p:dialog header="Turmas" widgetVar="turmaView" appendToBody="true" modal="true" resizable="false" draggable="false">
<h:form>
<h:outputLabel value="I am a modal" />
</h:form>
</p:dialog>
menubar
<h:form>
<p:menubar>
<p:submenu label="Cadastro">
<p:submenu label="Participante">
<p:menuitem value="Aluno"></p:menuitem>
<p:menuitem value="Professor"></p:menuitem>
</p:submenu>
<p:separator/>
<p:menuitem value="Turma" onclick="PF('turmaView').show()"/>
</p:submenu>
</p:menubar>
</h:form>
<ui:include src="/turma/view.xhtml"/>
now works.
It's not possible to execute server side methods wrapping them in onclick listeners or similars like that. You're confused about client side javascript functions and server side listeners. According to PF docs:
MenuItem has two use cases, directly navigating to a url using GET or doing a POST to execute an action.
So there's no way to use it for client-side specific purpose, as it's going to perform one request. What you could do is to show the dialog after it happens:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head />
<h:body>
<h:form>
<p:menubar>
<p:menuitem value="Turma" oncomplete="dlg.show();" />
</p:menubar>
<p:dialog header="Modal Dialog" widgetVar="dlg" modal="true"
height="100">
<h:outputText value="This is a Modal Dialog." />
</p:dialog>
</h:form>
</h:body>
</html>
This properly displays the dialog when the p:menuItem is clicked. An ajax request with no listener is performed and when it's completed, the javascript code to show it runs.

primefaces single menuItem in panelmenu

I'm trying to implement a menu with JSF 2.2 and Primefaces 5
I added PanelMenu of primefaces. Let's say I have a code snippet like this:
<p:panelMenu>
<p:menuitem value="Home" url="homee.xhtml" icon="ui-icon-home"/>
<p:submenu label="User Operations">
<p:menuitem value="ETC" actionListener="#{menuView.save}" icon="ui-icon-folder-open" />
<p:menuitem value="New ETC" actionListener="#{menuView.update}" icon="ui-icon-plusthick" />
</p:submenu>
</p:panelMenu>
The problem is, when I do this, that single menuItem which doesn't have a surrounding subMenu doesn't show up in the menu.
How can I overcome this?
Thanks.
you need to put the single menuitem inside a menu tag and outside the panelMenu :
<p:menu>
<p:menuitem value="Home" url="homee.xhtml" icon="ui-icon-home"/>
</p:menu>
<p:panelMenu>
<p:submenu label="User Operations">
<p:menuitem value="ETC" actionListener="#{menuView.save}" icon="ui-icon-folder-open" />
<p:menuitem value="New ETC" actionListener="#{menuView.update}" icon="ui-icon-plusthick" />
</p:submenu>
</p:panelMenu>

How to retain/maintain collapse and expand states of <p:panelMenu> even after a page refresh?

Suppose I have the following simple <p:panelMenu>.
<p:panelMenu style="width:200px">
<p:submenu label="Contents">
<p:menuitem value="MenuItem1" url="SomeURL" icon="ui-icon-disk" />
<p:menuitem value="MenuItem2" url="SomeURL" icon="ui-icon-disk" />
<p:menuitem value="MenuItem3" url="SomeURL" icon="ui-icon-disk" />
</p:submenu>
<p:separator />
<p:submenu label="Users">
<p:menuitem value="MenuItem1" url="SomeURL" icon="ui-icon-disk" />
<p:menuitem value="MenuItem2" url="SomeURL" icon="ui-icon-disk" />
<p:menuitem value="MenuItem3" url="SomeURL" icon="ui-icon-disk" />
</p:submenu>
</p:panelMenu>
Once a sub menu is expanded should remain expanded until it is explicitly collapsed even after a page refresh.
Similarly, sometime later, if the sub menu is collapsed should remain collapsed until it is explicitly expanded even after the page reloaded/refreshed. Is this possible?
By the way, the following script expands a <p:panelMenu> by default (all <p:submenu>).
$(document).ready(function(){
$(".ui-panelmenu-content").css("display","block");
$(".ui-panelmenu-header").addClass("ui-state-active");
$(".ui-icon-triangle-1-e").removeClass("ui-icon-triangle-1-e").addClass("ui-icon-triangle-1-s");
});
And the following script expands the first sub menu of <p:panelMenu>, if it is collapsed.
var isMenuExpanded = jQuery('div.ui-panelmenu-content.ui-widget-content.ui-helper-hidden').is(':visible');
if(!isMenuExpanded)
{
jQuery('.ui-panelmenu-panel a').first().click();
}
Set every value item with ids
and also with class to the click response
<p:panelMenu id="menu" style="width:200px">
<p:submenu id="submenu" label="Contents" styleClass="submenu">
<p:menuitem id="menu1" value="MenuItem1" url="SomeURL" icon="ui-icon-disk" />
<p:menuitem id="menu2" value="MenuItem2" url="SomeURL" icon="ui-icon-disk" />
<p:menuitem id="menu3" value="MenuItem3" url="SomeURL" icon="ui-icon-disk" />
</p:submenu>
</p:panelMenu>
set localstorage as global variable (index)
$(document).ready(function(){
localStorage.param=Number(0);
});
variables session with localstorage javascript
var param= Number(localStorage.param);
every page refresh, get the localstorage variable session to change the menu state
$(document).ready(function(){
var e1 = document.getElementById("menu_submenu");
if(Boolean(param)==true){
e1.style.display="block";
e1.parentNode.getElementsByTagName("h3")[0].className="ui-widget ui-panelmenu-header ui-state-default ui-state-active ui-corner-top";
e1.parentNode.getElementsByTagName("h3")[0].getElementsByTagName("span")[0].className="ui-icon ui-icon-triangle-1-s";
}
else{
e1.style.display="none";
e1.parentNode.getElementsByTagName("h3")[0].className="ui-widget ui-panelmenu-header ui-state-default ui-corner-all";
e1.parentNode.getElementsByTagName("h3")[0].getElementsByTagName("span")[0].className="ui-icon ui-icon-triangle-1-e";
}
the click event to set the localstorage variable session
$(document).on("click", ".submenu", function(e) {
var target = e.target || e.srcElement;
while (target && !target.id) {
target = target.parentNode;
}
if (!(target.id == 'menu1')){
var n= Number(param)==Number(0)?Number(1):Number(0);
param=Number(n);
localStorage.param=Number(n);
}
});
<p:panelMenu stateful="false"></p:panelMenu>
stateful attruibute default is true. If you want to control collapsed/expanded states by hand, you should set it false, then you can use expanded attribute on submenu elements.
Ways to store the current state of your panels (which are open and which are closed) are already covered pretty nicely with this earlier Stack Overflow question.

Displaying menu with panelgrid rendered

My Below code display menu based on if not database is empty, my problem is, it displays the second submenu but does not display the first one. i have got same validation on both
<h:panelGrid rendered="#{not empty dataBase}" width="100%">
<h:form>
<p:menubar style="height: 25px;background-color: #9999ff" >
<h:panelGrid rendered="#{not empty rights}" >
<p:submenu label="Master" >
<p:menuitem value="Client" url="test.xhtml" />
<p:menuitem value="TaxMaster" url="test.xhtml" />
<p:menuitem value="Quotation" url="test.xhtml" />
<p:separator />
<p:menuitem value="Area" url="test.xhtml" />
</p:submenu>
</h:panelGrid>
<p:submenu label="Back Office" icon="ui-icon-pencil">
<p:submenu label="Book Issue" icon="ui-icon-contact">
<p:menuitem value="Add" url="BookIssue_Add.xhtml" />
<p:menuitem value="Edit" url="BookIssue_Edit.xhtml" />
<p:menuitem value="View" url="test.xhtml" />
</p:submenu>
</p:submenu>
</p:menubar>
</h:form>
</h:panelGrid>
<p:submenu/> must be child of <p:menubar/> otherwise it wont be rendered

Resources