Ajax call in p:selectManyCheckbox - java

Hi guys is there a way to fire an ajax call in a p:selectManyCheckbox of primefaces once I check one of them? something like this:
<p:selectManyCheckbox label="Elements" value="#{crearVacacionMB.elementSelected}" layout="grid" columns="1">
<f:selectItems value="#{MB.elements}" />
<p:ajax event="check" listener="#{MB.function}"/>
</p:selectManyCheckbox>
And my MB:
public void function(AjaxBehaviorEvent event){
System.out.println("I was fired");
}

The code you posted produces a JSF error, at least with Mojarra 2.2, and I am surprised you haven't mentioned it:
<p:ajax> Event:check is not supported.
Remove the event attribute from <p:ajax>: the ajax behaviour will default to the default event valueChange:
<p:ajax listener="#{MB.function}"/>
Links:
When to use valueChangeListener or f:ajax listener?

Related

Primefaces, periodical update when idle

I want to auto update datatable (on client) when user is idle. I'm using Primefaces 3.5 with glassfish and my idea was following:
I've boolean autoUpdate variable in bean. And user can turn on/off the auto update.
I created two components: p:poll and p:idleMonitor.
The idleMonitor has two events, which trigger the update
<p:ajax event="idle" oncomplete="myPoll.start();" />
<p:ajax event="active" oncomplete="myPoll.stop();" />
This part is ok. The problem is, how to disable idleMonitor when autoUpdate is set to False? When I do
<h:panelGroup id="updatePanel" rendered="#{cc.attrs.bean.autoUpdate}">
....
</h:panelGroup>
it works only if I start with autoUpdate = false and I can switch the mode only once. Once it's rendered it can't be changed. I've also tried
<p:ajax event="idle" oncomplete="#{cc.attrs.bean.autoUpdate ? 'myPoll.start();' : ''}" />
but it didn't work neither. The #{cc.attrs.bean.autoUpdate} is never changed. Even I'm calling upadte on it and I can see the variable changed on another place.
So, my question is: Is there any way how to disable idleMonitor, after it was rendered? Or what is the better solution for optional periodical update for idle's user?
Your autoUpdate variable is evaluated only at render time. (at page load)
#{cc.attrs.bean.autoUpdate}
That's why js doesn't know about its modifications.
Solution 1, rerender a js code:
<p:ajax event="idle" update="afterIdle" />
<h:panelGroup id="afterIdle">
<h:outputScript>
if ('#{cc.attrs.bean.autoUpdate}' == 'true')
myPoll.start();
</h:outputScript>
</h:panelGroup>
Solution 2: keep a js variable updated on autoUpdate (somehow, for example with remoteCommand):
<p:ajax event="idle" oncomplete="myFunction()" />
<script type="text/javascript">
function myFunction(){
if (jsAutoUpdate) myPoll.start();
}
</script>
Solution 3: primefaces idleMonitor widget has a stop() function, which stops idle monitoring.

JSF f:ajax and IE 8

I encountered with problem: in IE8 don't work event in f:ajax and don't update other components after change value.
<h:selectBooleanCheckbox id="someId"
value="#{someBean.showEmpty}"
title="#{i18n['button.showEmpty']}">
<f:ajax event="change"
listener="#{someBean.changeShowEmpty}"
execute=":someForm #form" render=":messages :someForm #form" />
</h:selectBooleanCheckbox>
In Chrome, Opera, Firefox - it works.
Thanks for the help.
That's indeed "expected" behaviour for MSIE. It will only work on 2nd change and forth, because MSIE thinks that the 1st click is in essence not a change. You should be listening on the click event instead. That's also exactly what the <f:ajax> already by default does for a <h:selectBooleanCheckbox>. Just remove the event attribute altogether.
<f:ajax listener="#{someBean.changeShowEmpty}"
execute=":someForm #form" render=":messages :someForm #form" />
The <f:ajax event> defaults to "valueChange" in UIInput components and defaults to "action" in UICommand components. In UIInput components which generate radio button or checkbox, it will then generate onclick. In other UIInput components (text fields, textarea, dropdowns, etc) it will generate onchange.
Unrelated to the concrete problem, an other <h:form> can not be processed in contrary to what you seem to think in execute attribute, simply because its values are not submitted along with the submit of the current form. But that's another story.

How to use both Navigation Rule and f:ajax

Here's my scenario: I'd like to update a page via Ajax in some cases, in other cases, execute a navigation rule. My use case is a login form. I'd like them to receive an error message via ajax if their uname/password fails, but navigate to a new page if it succeeds.
Has anyone done this using JSF2.0 f:ajax apis? I'm not really interested in solutions that go outside standard facelets, jsf2.0, etc.
It's not different from when doing it without ajax. Just return the next view ID as String the usual way via <h:commandXxx action> (and thus not <f:ajax listener>).
So, just
<h:commandButton value="Login" action="#{bean.login}">
<f:ajax execute="#form" render="#form" />
</h:commandButton>
with
public String login() {
// ...
return "nextpage";
}
will work as good as without <f:ajax>. It'll just go to nextpage.xhtml.
See also:
Differences between action and actionListener
JSF f:ajax listener vs commandButton action

JSF GAE: Value Update Problem in managed bean method

I have the following piece of code with a simple h:outputText pointing to a int and a p:commandLink to set a value:
<h:form id="f1">
<h:outputText id="text" value="#{testBean.index}"/>
<p:commandLink actionListener="#{testBean.test}" update="text">
<f:setPropertyActionListener target="#{testBean.index}" value="5" />
<h:graphicImage url="/images.png"/>
</p:commandLink>
</h:form>
The managed bean looks like this:
#javax.faces.bean.ManagedBean #ViewScoped
public class TestBean implements Serializable{
private int index; // getter/setter
#PostConstruct public void init() {
index = 0;log.log(Level.WARNING, "#PostConstruct");}
public void test(ActionEvent ae){
log.log(Level.WARNING, "Index: "+index);}
}
The bean is constructed correctly, and after the first click on the image the h:ouputText is updated to 5. But in my log message I only see Index: 0 during the first click on the image.
It's something similar like Jsf updates model value with old value, but I have the JSF #ManagedBean annotation.
Action listeners are invoked in the order they're definied in the view. You want to use action instead of actionListener. Even more, the action should in first place have been used to invoke a business action.
<p:commandLink action="#{testBean.test}" update="text">
<f:setPropertyActionListener target="#{testBean.index}" value="5" />
<h:graphicImage url="/images.png"/>
</p:commandLink>
See also:
Differences between action and actionListener
What is happening is that the test ActionEvent is getting fired before the request values have been applied.
To get a better understanding of the JSF phase lifecycle and when lifecycle events and ActionEvents fire, implement the Debug PhaseListener as specified in the following blog article.
http://balusc.blogspot.com/2006/09/debug-jsf-lifecycle.html
This should help you understand when request values are being applied, and when events are being fired.

Primefaces dataTable with embedded commandLink

I'm trying to embed a Primefaces commandLink and call an action listener from a link inside a column of a Primefaces dataTable. Is this not possible? The "Test" onclick alert gets fired but it never makes it to my bean's method.
<p:dataTable var="location" value="#{adminBean.locations}">
<p:column headerText="Options">
<p:commandLink value="delete" actionListener="#{admin.deleteLocation}" onclick="alert('test')"/>
</p:column>
</p:dataTable>
bean code:
public void deleteLocation(ActionEvent e){
//delete logic here...
}
This is possible. Your actionListener should be called. Keep in mind that the p:commandButton uses ajax by default. So you should use the update attribute in order to define the components to be updated.
However, I don't know if this affects the actionListener. Did you try it with action instead of actionListener?
Here is an example how I got it working:
<p:commandLink action="#{spc.selectPatient(item)}"
ajax="false"
value="Open"/>
The bean method looks as follows:
public String selectPatient(Patient p) {
// do something
// return some outcome
}

Categories

Resources