How to trigger primefaces <p:blockUI> using boolean in backing bean? In this showcase, it requires a commandButton to trigger the the blockUI. What I want is block some part of the page base on a boolean in the backing bean. Is this possible? I tried setting the rendered attribute in the <p:blockUI> tag but it still won't work.
Here is my code:
<p:blockUI block="grid" rendered="#{bean.trueValue}"></p:blockUI>
<p:panelGrid id="grid">
-- content
</p:panelGrid>
One way is to call hide() and show() method of BlockUI from Managed Bean.
You can do that by using RequestContext:
RequestContext.getCurrentInstance().execute("widgetVar.show()");
Another is you can pass the variable to JavaScript function and then let the Javascript function take care of that for you.
onClick="func(#{elvariable})"
<script type="text/javascript">
function func(value)
{
if(value==something){
widgetVar.show();
}else{
widgetVar.hide();
}
}
Related
This is code:
<p:ajax event="eventResized" process="#this calendar" listener="#{bean.eventResized}" oncomplete="resizeComplete()"/>
eventReized invoked by EventResizeBehavior which extended from AjaxBehaviorEvent and it contains some property. Can I check inside <p:ajax....> call its value and pass result to oncomplete="resizeComplete(result)"
Something like that
<p:ajax event="eventResized" process="#this calendar" listener="#{bean.eventResized}" oncomplete="resizeComplete(#{eventResized.id == 0})"/>
PrimeFaces doesn't support it. Any EL expressions in oncomplete attribute are immediately evaluated during render response of that HTML document, not during oncomplete of the associated ajax call. Basically, the JavaScript code generated by oncomplete attribute contains the old value as it was during the page load.
Your best bet is using RequestContext#addCallbackParam() to add a property to the PrimeFaces-specific args object which is available in oncomplete scope.
RequestContext.getCurrentInstance().addCallbackParam("result", eventResized.getId() == 0);
<p:ajax ... oncomplete="resizeComplete(args.result)" />
An alternative is to use RequestContext#execute() instead of oncomplete to programmatically instruct PrimeFaces to execute a piece of JavaScript on complete of the ajax request.
RequestContext.getCurrentInstance().execute("resizeComplete(" + (eventResized.getId() == 0) + ")");
I'm using p:button with onclick action. (I can't move to p:commandButton, because of legacy outcome navigation in faces-config.xml and custom navigation history in db):
<p:remoteCommand name="unlock_tt" actionListener="#{ttEntityMBean.unlock()}"/>
<p:button value="#{msgs['button.ok']}" outcome="#{ttEntityMBean.navigationMenuItemToRedirect.navigationRule}" onclick="unlock_tt()"/>
Generated by primefaces javascript looks like
onclick="unlock_tt(); window.open(....)"
And after clicking button, unlock_tt() in browser initiated, but immediatly broken by page redirecting, so backed java method didn't execute.
Should I make unlock_tt() or java call async to be sure it will be executed before browser leaves page?
Upd: I'm thinking to use p:commandButton, if it is possible to get to-view-id programically, like in this question:
Programmatically get navigation case <to-view-id> from faces-config.xml by outcome
<p:commandButton action="#{ttEntityBean.unlock()}"/>
public String unlock() {
//some business logic
return OutcomeResolverHelper.getRuleFor(navigationMenuItemToRedirect.navigationRule)
}
This should reduce number of requests
I'm not sure this will work but can you try this :
Instead of :
<p:remoteCommand name="unlock_tt" actionListener="#{ttEntityMBean.unlock()}"/>
<p:button value="#{msgs['button.ok']}" outcome="#{ttEntityMBean.navigationMenuItemToRedirect.navigationRule}" onclick="unlock_tt()"/>
do:
<h:panelGroup>
<f:ajax event="click" listener="#{ttEntityMBean.unlock}"/>
<p:button value="#{msgs['button.ok']}" outcome="#{ttEntityMBean.navigationMenuItemToRedirect.navigationRule}" />
<h:panelGroup>
Don't forget to remove onclick and your remotecommand.
This should wrap a panelGroup around your button. PanelGroup implements ClientBehaviorHolder so it can get f:ajax tag in its children. Thus when you click on it it should trigger the listener.
The easiest way was migration from p:button to p:commandButton, which allows to execute some java-code in action method before redirect. The core is programmical translation of navigation rule, defined in faces-config.xml to base url, usable in explisit jsf-2.0 style:
//receive base url from <to-view-id> tag
public static String resolveOutcomeRule(String ruleName) {
FacesContext context = FacesContext.getCurrentInstance();
ConfigurableNavigationHandler configNavHandler = (ConfigurableNavigationHandler)context.getApplication().getNavigationHandler();
NavigationCase navCase = configNavHandler.getNavigationCase(context, null, ruleName);
return navCase.getToViewId(context);
}
//decorate it with faces-redirect=true and our custom navigation
public static String resolveOutcome(NavigationMenuItem item) {
return resolveOutcomeRule(item.getNavigationRule()) + "?faces-redirect=true&menu=" + item.getId();
}
//controller-method
public String cancelEditTT() {
super.unlock();
return FacesUtil.resolveOutcome(getNavigationMenuItemToRedirect());
}
//new jsf button:
<p:commandButton value="#{msgs['button.ok']}" action="#{ttEntityMBean.cancelEditTT}"/>
This solution provided 1-step redirect instead of 2-step old one.
Let's say I have a simple method that, like this:
public String test()
{
return "hello";
}
Now let's say I have the following PrimeFace component:
<p:fieldset styleClass="containers" rendered="#{controller.test()}">
<h2>Testing</h2>
<p:outputLabel for="test" value="Test" />
<p:inputText id="test" />
</p:fieldset>
The method above returns "hello". I would like to dynamically show and hide that fieldSet by comparing the returned value of that method to a field of one of my beans. For instance, on the rendered parameter, I would like to do something like: controller.test() != "some variable" which would return true or false. Am I allow to do that? If not, what is the way of doing it?
Basically the goal is to dynamically show and hide some container by comparing the returned value of a method with a bean property.
Look Like you misunderstood rendered
The rendered Attribute
A component tag uses a Boolean EL expression, along with the rendered
attribute, to determine whether or not the component will be rendered.
If you will check above definition you will know what exactly is the use of this attribute.
More you can see below
The rendered attribute which uses Boolean EL expression indicates
whether a component is currently visible or not. The property is
useful when you want to hide components for specific users or based on
condition. The default value of rendered attribute is true.
<h:outputText value=”mapping” rendered=”Boolean EL expression” />
For example, the commandLink component in the following section of a page is not rendered if the cart contains no items:
<h:commandLink id="check"
...
rendered="#{cart.numberOfItems > 0}">
<h:outputText
value="#{bundle.CartCheck}"/>
</h:commandLink>
With your concrete problem you can do like this
Make a String variable call value
Now create get/set methods for above variable
Now in your test method you can add
public void test(){
value="hello";
}
Bur remember you have call test() method of page load
Now in your Xhtml or Jsf or Jsp page
rendered="#{controller.value != 'hello'}"
Or better way create a Boolean variable and do all the magic of hide and show the component
How can I pass current selected row to JavaScript function in <p:ajax oncomplete>?
<p:dataTable value="#{bolt.sites}" var="bolt" selection="#{bolt.selectedSite}" ...>
<p:ajax event="rowSelect" oncomplete="alert(#{bolt.selectedSite.name});" />
I have tried all: #{bolt.selectedSite.name}, #{bolt.name}.
EL expressions in oncomplete attribute are not evaluated after invoking the action. They are already evaluated while rendering the JavaScript code containing that ajax calling logic.
Your best bet is adding a listener method which adds the name as a callback parameter via RequestContext#addCallbackParam() which will then be available as a property of the implicit args object inside oncomplete context.
<p:ajax ... listener="#{bean.select}" oncomplete="alert(args.name)" />
public void select() {
String name = selectedSite.getName();
RequestContext.getCurrentInstance().addCallbackParam("name", name);
}
Unrelated to the concrete problem, you have a syntax error in your initial JavaScript attempt. The property name "name" suggests that it's a string. In JavaScript, all string values should be quoted. So your fictive solution would be oncomplete="alert('#{bolt.selectedSite.name}');"
In order to reducing the state I am trying to reduce the no of commandButtons on my webpages.(I had large no of commandButtons shown under a long list of 20 items (6*20 = 120 commandButtons)). Thus I am trying to figure out a way, through javascript by which I can pass parameters & call the action methods in the ManagedBean controller classes. Is there any way to call action methods from javascript & pass them parameters ?
Richfaces 3.2.0.GA and XHTML as mark up
You can use javascript to call an a4j:js method, which inturn calls action method from managed bean.
The param which you need to pass can be set to a hidden variable which when set will set the value to the java variable in your bean.
<script>
function onButtonClick(){
$("#yourValue").val("value");
actionListenerMethod();
}
</script>
<a4j:jsFunction name="actionListenerMethod"
actionListener="#{yourManagedBean.actionMethod}"
oncomplete="scriptOnComplete();">
</a4j:jsFunction>
<h:inputHidden id="yourValue"
value="#{yourManagedBean.yourValue}" />
Managed bean:
public void actionMethod(ActionEvent event){
if(yourValue == "something"){
/*your action goes here*/
}
}
Otherwise you can by-pass this hidden variable by use of action param
<script>
function onButtonClick(){
actionListenerMethod("value");
}
</script>
<a4j:jsFunction name="actionListenerMethod"
actionListener="#{yourManagedBean.actionMethod}"
oncomplete="scriptOnComplete();">
<a4j:actionparam name="param1"
assignTo="#{yourManagedBean.yourValue}" />
</a4j:jsFunction>
In the later case the action param might get set only after the manged bean gets completed, in this case you can use Action atribute to call your action method instead of an action listener.
This will help you to set the param and then call the action method.
<a4j:jsFunction name="actionListenerMethod"
action="#{yourManagedBean.actionMethod}"
oncomplete="scriptOnComplete();">
<a4j:actionparam name="param1"
assignTo="#{yourManagedBean.yourValue}" />
</a4j:jsFunction>