XPages disable eventHandler - xpages

I have a span tag with an onCLick event handler. I would like to disable the event handler based on the value of a scope variable. The scope variable is being set when I click a checkbox.
How can I do this?
Thanks for your input!
Dan

In client side part of the event use this code.
return !#{sessionScope.disableEvent};
That should render as
return !false; // continues to SSJS
or
return !true; // no SSJS

Event handlers have a rendered-attribute that you can calculate.
If the span in question is in the area that's being refreshed, you can use this attribute to control if the event handler is active or not.
E.g.
<xp:eventHandler event="onclick" rendered="#{viewScope.someVariable == 'someValue'}" ... />

Related

How to call a JavaScript function form a p:poll iteration

I have PrimeFaces poll interval to update table in one minute it is disabling a radio button.
<p:poll interval="60" update="sendServer" />
sendServer is the id of the table, but for the radio button selection I have a script to call while reloading the page.
function loadPageScripts() {
selectFirstRow(window.document.forms["send-blacklist-form"]);
}
As with all PrimeFaces components triggering Ajax calls, you have the onstart, oncomplete, onsuccess and onerror attributes you can use for that.
See:
https://primefaces.github.io/primefaces/10_0_0/#/components/poll

Disable dynamically created button from managed bean

I have a problem with dynamicaly created CommandButton in Primefaces.
I want to disable button after user click (submit). I've tried in two ways:
By adding widgetWar (commandButton.setWidgetVar("pony");) property to my commandButton and setting onclick behaviour (commandButton.setOnclick("PF('pony').disable();");). WidgetVar property is unique among all elements on page. This approach doesn't work - my button after click turns into disable and then immediately goes back to enable.
From managedBean by calling setDisabled method on button UiComponent ((CommandButton) component).setDisabled(true);. This also doesn't work.
Is there any other way to disable CommandButton from Java code or am I missing something?
I'm generating my page dynamically so I cannot use disabled property in xhtml.
I also set my commandButton update property to update parent p:outputPanel.
Thanks in advance.
my button after click turns into disable and then immediately goes back to enable.
That will happen if the ajax update covers the button itself. Just exclude the button from it and specify only the parts which really need to be updated.
you can a declare field in your bean
boolean disable = false;
after click on button in your listener:
disable = true;
in your button use this code:
<p:commandButton id="x" disabled="#{bean.disable}" update="#form:x" />

How to Run SSJS in Application Layout Item Buttons / Nodes?

I've just started using the xe:applicationLayout for an application. I have added a "Basic Node" to the "Place Bar". I want to run some server side JavaScript code onClick of the node/button. I have tried computing the onClick property of the Basic Node, but this doesn't do anything and it seems like the onClick only runs client side JavaScript.
Is there a property or node that can run SSJS? Thanks for any help.
Set the submittedValue property of the node. ( the procedure is the same for all places in the application layout; here is an example snippet for the banner.ApplicationLinks )
<xe:this.bannerApplicationLinks>
<xe:basicContainerNode styleClass="firstApplication" label="${langString_CRM['CREATE']}">
<xe:this.children>
<xe:basicLeafNode submitValue="company_new"
Then add an event handler to your page and run your SSJS depending on the submitted value
<xp:eventHandler event="onItemClick" submit="true"
refreshMode="partial" disableValidators="true" refreshId="cois_application_layout"
execMode="partial">
<xp:this.action><![CDATA[#{javascript:
var submittedValue=context.getSubmittedValue();
// evaluate the submitted value here ...
Not the nicest version but it works.
Add buttons to the XPage and hide then using css.
In your application layout add a client side script that gets a handle to the buttons using
dojo.byId and do a click() on them. Then it's easy to have different panels update when you click on different buttons.

Primefaces onclick and onsuccess differences

I have the following situation:
after clicking a button, some business logic is done and after it is done, a new tab with a report should be visible.
<p:commandButton value="this button" update="growlMain"
actionListener="#{myBean.businesslogic}"
onstart="ajaxDialog.show();"
oncomplete="ajaxDialog.hide();"
onsuccess="window.open('./report.jsp', '_newtab');" />
This does not work :(
If the business logic only lasts some milliseconds, the following works:
<p:commandButton value="this button" update="growlMain"
actionListener="#{myBean.fastbusinesslogic}"
onclick="window.open('./report.jsp', '_newtab');" />
the onclick opens a new tab, also things like onstart but it doesn't work with onsuccess or oncomplete. Why? And is there a solution for business logic that lasts some seconds?
onclick is called before the ajax request is even created (pure client side) while oncomplete and onsuccess are executed after the server responded to the ajax request. So, if you need to execute some business logic before showing a dialog, for example, you want to go with oncomplete. That's what I always use.
You can also condition your javascript inside oncomplete to perform only if there's no validation errors. Intuitively I think onsuccess would behave like that and only execute when there's no validation errors, but that's not how it goes. I don't really know the difference between them. I assume there's a way to flag success=false in the backing beans, but I couldn't really find it in the documentation.
If you want to check for validation in your oncomplete attribute, this is how you'd do:
oncomplete="if (!args.validationFailed){someDialog.hide()}"
In this case, you would only close the dialog if the fields are properly validated. You can actually inject parameters from the backing bean and use them in the javascript after the request has been served. In your backing bean you can do something like this:
RequestContext.getCurrentInstance().addCallbackParam("showDialog", false);
And you can access the parameter like this in your incomplete attribute:
oncomplete="if (args && args.showDialog){someDialog.show()}else{ alert('the bean didnt let me open the dialog')}"
Anyway, I hope that helps.
I have noticed that onsuccess for PrimeFaces command button does not work. The oncomplete however works and does the needful even if there is an error , such as in my case shows a success dialog even if there is an error in my business logic. Tried to use onsuccess but doesn't work.
You could try oncomplete as below:
<p:commandButton value="this button" update="growlMain"
actionListener="#{myBean.businesslogic}"
onstart="ajaxDialog.show();"
oncomplete="ajaxDialog.hide(); window.open('./report.jsp', '_newtab');"/>
You can see the difference here:
Primefaces and ajax onsuccess event
or with onsuccess you can do something before full loading DOM

ValueChangeListener method not called on my h:selectOneRadio

I am little confused about my radio button list in JSF and how it reacts to stuff and I didn't find much help online. Below is the declaration of my radio button list and the method which should be called in case the value of the radio changes:
<h:selectOneRadio value="#{AddExpense.selectedTypeExp}" layout="pageDirection"
valueChangeListener="#{AddExpense.changed}">
<f:selectItems value="#{AddExpense.typeExpList}"/>
<a:support event="onclick" action="#{AddExpense.typeExpChanged}" immediate="true"/>
</h:selectOneRadio>
When I choose a different value, only the typeExpChanged is called, but the AddExpense.changed method is not called. I think I'm confusing something here, not sure how the changeListener should react... Below is my very simple test method which should be called:
public void changed(ValueChangeEvent event){
System.out.println("In changed event method: "+event.getNewValue());
}
Should I change something in the <a:support> ?
The reason I have both event and valueChangeListener is because I wanted to test what reacts to my changing the selection. I just need a method to be called with a parameter which tells me the selected value, so I can load something else.
Thanks in advance!
The valueChangeListener is not a client side event listener. It's a server side event listener. It does not generate any line of HTML/JS/Ajax code and it is triggered by JSF itself when you submit the form to the server and the submitted value is different from the initial model value.
Just keep using Ajax4jsf <a4j:support>, it's perfectly fine for your particular functional requirement.

Resources