I am working with JSF 2.0 and primefaces 3.3. I want to hide/show a toolbar onclick of a menuitem.
Here is the more info.
<p:menubar autoSubmenuDisplay="true" style="width:99%">
<p:submenu label="Projects">
<p:menuitem id="m11" value="Select Product" actionListener="#{menuBean.renderTool}" update="t1" />
<p:menuitem id="m12" value="Select Project" />
<p:menuitem id="m13" value="Select Contract" />
</p:submenu>
<p:menuitem id="m2" value="Global" />
<p:menuitem id="m7" value="Exit" />
</p:menubar>
<p:toolbar id="t1" rendered="#{menuBean.renderToolbar}">
<p:toolbarGroup align="left" style="height:20px;">
<h:outputText value="Projects " />
<h:outputText value=" - select Product" />
</p:toolbarGroup>
</p:toolbar>
ManagedBean
private boolean renderToolbar = false;
//getters and setters
public void renderTool(ActionEvent actionEvent){
System.out.println("inside renderTool method...");
renderToolbar = true;
}
The actionListener method is executing But, it's not updating or rendering the toolbar.
add some boolean variable to your bean
boolean someBoolean; //+ getter/setter
and inside your renderToolbar method add
someBoolean = !someBoolean; // toggle the disaplay on and off
in xhtml change
<p:toolbar id="t1" rendered="#{menuBean.renderToolbar}">
into
<h:panelGroup id="t1">
<p:toolbar rendered="#{menuBean.someBoolean}">
.
.
.
</h:panelGroup>
Not much info that you provided.
However, one way to do it is using Javascript with the onclick event handler.
Like this (untested code):
<p:toolbar id="toolbarID" />
<p:menu>
<p:menuitem onclick="$('#toolbarID').toggle();" />
</p:menu>
I think primefaces already includes jquery so you should be able to use jquery selectors out of the box.
The solution Daniel provided works using a backing bean. However if showing/displaying some element isn't dependent on data but is more a client-side thing or a simple user controlled element I would advice against using a backing bean. Using a backing bean for client-side stuff causes delays or as a regular user would put it: "It's slow".
In stead use client side things like JavaScript as Jens suggested. Since you're using PrimeFaces you can make use of jQuery. A simple example to demonstrate jQuery's toggle(), show() and hide() functions:
<h:form>
<p:menubar>
<p:menuitem value="Toggle" onclick="$("[id='t1']").toggle()" />
<p:menuitem value="Show" onclick="$("[id='t1']").show()" />
<p:menuitem value="Hide" onclick="$("[id='t1']").hide()" />
</p:menubar>
</h:form>
<p:toolbar id="t1" />
Note that if your p:toolbar lives in a container like a form or such the client-side ID is prefixed with the form's ID.
Related
I'm trying use the STEPS component - Primefaces. But in the documentation the tutorial is very poor.
Can someone write an example using steps with property rendered or something like that, how Can I show and hide a panel using STEPS component.
I tried like this but does not work
My xhtml
<p:steps id="testSteps">
<p:menuitem value="Personal" update="testSteps" actionListener="#{BeanTest.shown2()}"/>
<p:menuitem value="Seat Selection" update="testSteps"/>
</p:steps>
<form id="formShowField1" >
<p:panel rendered="#{BeanTest.showfield1}">
<p:outputLabel value="FORM 1"/>
</p:panel>
</form>
<form id="formShowField2">
<p:panel rendered="#{BeanTest.showfield2}">
<p:outputLabel value="FORM 2" />
</p:panel>
</form>
My bean
public void shown1(){
showfield1 = true;
updateEntirePage("formShowField1");
}
public void shown2(){
showfield1 = false;
updateEntirePage("formShowField1");
showfield2 = true;
updateEntirePage("formShowField2");
}
As stated by comments, there are multiple issues with your XHTML code.
1) Use <h:form> instead of <form>
2) The p:steps component is readonly by default. Set readonly="false" in order to have interactive menu items. In this mode, it needs to be placed somwhere inside a h:form to - I get a javax.faces.FacesException: MenuItem must be inside a form element else.
3) Your menuItems update the p:steps component only. Your other panels won't ever show up this way as they are not updated. You should update an element containing them too. Don't know what updateEntirePage is though, especially when invoked twice.
4) Bean names like Java variables typically start with lower case character.
Try it like this:
<h:form>
<p:steps id="testSteps" readonly="false">
<p:menuitem value="Personal" update="#form" actionListener="#{beanTest.shown2()}"/>
<p:menuitem value="Seat Selection" update="#form"/>
</p:steps>
<p:panel rendered="#{neanTest.showfield1}">
<p:outputLabel value="FORM 1"/>
</p:panel>
<p:panel rendered="#{beanTest.showfield2}">
<p:outputLabel value="FORM 2" />
</p:panel>
</h:form>
And in your bean:
public void shown2(){
showfield1 = false;
showfield2 = true;
}
I have the following code in an XHTML page :
<h:form id="myform">
<p:breadCrumb>
<p:menuitem value="Home" ajax="false" actionListener="#{menu.actionButton}"/>
<p:menuitem value="Page 1" actionListener="#{menu.actionButton()}" ajax="false"/>
<p:menuitem value="Page 2" url="#" />
</p:breadCrumb>
</h:form>
In my bean menu the method :
public void actionButton() {
System.out.println("OKKKKKKKKKKKKK");
}
If I call the method with a command button, it works fine but in menuitem the method is not call.
Can someone please help ?
I am currently using PrimeFaces 5.
I have this method in a bean called home:
public String panelSearch() {
pnlsearch = true;
return "home";
}
pnlsearch is a property that makes a panel visible
home.xhtml contains:
<p:menubar>
<p:menuitem id="mnusearch"
value="Search"
ajax="true"
action="#{home.panelSearch}"
icon="ui-icon-search" />
<p:menuitem value="Search" >
<p:commandLink ajax="false"
action="#{home.panelSearch}"
value="Search" />
</p:menuitem>
</p:menubar>
<h:panelGrid rendered="#{home.pnlsearch}">...</h:panelGrid>
panelSearch is called with action in both p:menuitem and in p:commandLink
but panelSearch is visible only with p:commandLink
Why panelSearch is not made visible with a click on p:menuitem alone as well?
note: p:commandLink is called no matter the navigation-rule in faces-config.xml
I want to change my ActionListener of commandButton depending on the icon selected in advance:
<p:dialog id="dialog" header="Add Memo" widgetVar="dialogMemo" resizable="false" >
<h:form>
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="commentInput" value="Comment:" />
<p:inputTextarea id="commentInput" value="#{dashboardBean.getCurrentComment()}" rows="6" cols="25" label="commentInput"/>
<p:watermark for="commentInput" value="Enter your memo..."/>
<h:outputLabel for="selectShare" value="Share Memo: " />
<p:selectBooleanCheckbox id="selectShare" />
<h:outputLabel for="choosePriority" value="Priority:" />
<p:selectOneMenu id="choosePriority" value="#{dashboardBean.currentPriority}" label="choosePriority">
<f:selectItem itemLabel="Low Priority" itemValue="1" />
<f:selectItem itemLabel="Medium Priority" itemValue="2" />
<f:selectItem itemLabel="High Priority" itemValue="3" />
</p:selectOneMenu>
<p:commandButton id="submitDialog" icon="ui-icon-check" value="Confirm" ajax='false' type="submit" action="#{dashboardBean.getLastMemo()}"/>
<p:commandButton icon="ui-icon-close" onclick="dialogMemo.hide();" value="Cancel"/>
</h:panelGrid>
</h:form>
</p:dialog>
<p:layout fullPage="true">
<p:layoutUnit id="leftPanel" position="west" size="250" header="My Memos" resizable="false" closable="false" collapsible="false">
<h:form id="form">
<p:commandButton id="addMemo" icon="ui-icon-plus" onclick="dialogMemo.show();" type="submit" action="#{dashboardBean.getEditControl}"/>
<p:dashboard id="dashboardId" model="#{dashboardBean.model}" binding="#{dashboardBean.dashboard}">
</p:dashboard>
</h:form>
</p:layoutUnit>
</h:body>
When i click to command button (id="addMemo"), i want to change actionListener to commandButton(id="submitDialog").
I try that with:
public void getEditControl()
{
UIViewRoot view = _context.getViewRoot();
CommandButton button = (CommandButton) view.findComponent("submitDialog");
System.out.println("I am ID ==== [ " + button.getId() +" ]");
}
But 'button.getId()' does't work.
But 'button.getId()' does't work.
I gather that this is because the button is actually null and therefore the attempt to invoke the getId() method threw a NullPointerException? It's surprising that you stated the problem like that, "button.getId() doesn't work" instead of like "view.findComponent() returned null" (which has in turn the pretty obvious consequence that any attempt to access it would throw NullPointerException). If you had indeed no clue why it threw NullPointerException, then I strongly recommend to take a JSF pause and learn basic Java first.
Coming back to the concrete problem, the findComponent will return null when the client ID submitDialog doesn't exist in the component tree. Indeed, you have it inside a <h:form> which is a NamingContainer component. The button's client ID is more likely formId:submitDialog where formId is the ID of the button's parent form which you have to assign yet. An easy way to find out it is checking the ID of the generated HTML representation of the button.
See also this related question: How to find out client ID of component for ajax update/render? Cannot find component with expression "foo" referenced from "bar" Just substitute "ajax" in the answer with findComponent.
I am not sure that the way you do this is the right way.
If you want to get component instance better way to do this will be not get it by id but add
<p:commandButton id="submitDialog" icon="ui-icon-check"
value="Confirm" ajax='false' type="submit"
action="#{dashboardBean.getLastMemo()}" binding="#{dashboardBean.component}" />
and that way the property in the bean will always have the component instance that you need.
Note that the scope of bean where you put component binding should be view to avoid side affects.
There is some way to pass action in <rich:modalPanel>. I want to write simple popup with "Yes" and "No" button, and I want to reuse this popup in different pages, that's why I need that different action was invoked when "Yes" button pressed. There is a way to pass some value in <rich:modalPanel> with <a4j:actionparam>:
<a4j:commandButton value="See details…" id="det"
reRender="popup_dataIdField, …">
<rich:componentControl for="popup"
operation="show" event="onclick" />
<a4j:actionparam name="message" assignTo="#{popupBean.message}"
value="#{myBean.message}" />
</a4j:commandButton>
But is some way to pass action ?
As you're using Facelets then look at Rick Hightower's article and the section "Passing Actions". It's the same basic method to what you've used, but also passing in the method on the bean.
Eg:
<ui:include src="./WEB-INF/popup/popup.xhtml">
<ui:param name="yesAction" value="save"/>
<ui:param name="cancelAction" value="cancel"/>
<ui:param name="backingBean" value="#{thisPageBean}"/>
</ui:include>
And then in the popup:
<a4j:commandButton id="yesButton" value="#{msgs.yesButton}"
action="#{backingBean[yesAction]}"/>
Note that you use the #.
I find the answer by myself :). Maybe it will be helpful for somebody.
I've done this using Facelets. When I'm include popup to my page I pass parametr to it:
<ui:include src="./WEB-INF/popup/popup.xhtml">
<ui:param name="yesAction" value="${thisPageBean}" />
</ui:include>
Where thisPageBean - is bean from what popup invoked.
Then in my popup I wrote:
<a4j:commandButton id="yesButton" value="#{msgs.yesButton}"
action="#{yesAction.someAtion}"
</a4j:commandButton>
And with this I invoke thisPageBean.someAtion. All magic is ${thisPageBean}, it's is necessary to us $ but no #.
Yes I dont see any problems with doing this. You can use my code if you want:
<rich:modalPanel id="popup" width="261" height="386" autosized="true"left="180" top="200" keepVisualState="true">
<h:panelGrid id="panelGrid">
<h:outputText value="#{PopupBean.output}" id="popupMessage"/>
<a4j:commandLink action="#">
<h:outputText value="Close" />
<rich:componentControl for="popup" operation="hide" event="onclick"/>
</a4j:commandLink>
</h:panelGrid>
</rich:modalPanel>
<h:panelGrid columns="2">
<a4j:commandLink action="#" reRender="panelGrid">
<h:outputText value="Yes" />
<rich:componentControl for="popup" operation="show" event="onclick"/>
<a4j:actionparam name="message" assignTo="#{PopupBean.output}" value="#{TestBean.input1}"/>
</a4j:commandLink>
<a4j:commandLink action="#" reRender="panelGrid">
<h:outputText value="No" />
<rich:componentControl for="popup" operation="show" event="onclick"/>
<a4j:actionparam name="message2" assignTo="#{PopupBean.output}" value="#{TestBean.input2}"/>
</a4j:commandLink>
</h:panelGrid>
Basicly the output in the modal panel will contain the values in the TestBean
Edit (the misunderstanding):
I believe you will have to define your modal panel like this:
<rich:modalPanel id="popup" width="261" height="386" autosized="true"left="180" top="200" keepVisualState="true"
binding="#{PopupBean.popupPanel}">
</rich:modalPanel>
And in your managed bean you will have to addChildren to your modal panel dynamically with java like this:
public String action_PoppingThePanel() {
HtmlCommandButton button = new HtmlCommandButton();
button.setValue("Yes");
String action = "#{TestBean.action_yes}";
MethodExpression methodExpression =
FacesContext.getCurrentInstance().getApplication().getExpressionFactory().
createMethodExpression(FacesContext.getCurrentInstance().getELContext(), action, null,
new Class<?>[0]);
button.setActionExpression(methodExpression);
getPopupPanel().getChildren().add(button);
button = new HtmlCommandButton();
button.setValue("No");
String action = "#{TestBean.action_no}";
methodExpression =
FacesContext.getCurrentInstance().getApplication().getExpressionFactory().
createMethodExpression(FacesContext.getCurrentInstance().getELContext(), action, null,
new Class<?>[0]);
button.setActionExpression(methodExpression);
getPopupPanel().getChildren().add(button);
getPopupPanel().setRendered(true);
getPopupPanel().setShowWhenRendered(true);
return null;
}