My problem is that the following code returns null but if I look when debuging source then submittedValue has the correct date.
Java:
public void changeOneMenuP(AjaxBehaviorEvent event) {
String id = (String) event.getComponent().getAttributes().get("value");
if(id != null) {
listEntity = escDao.findByxxxx(id, true);
}
}
XHTML:
<h:selectOneMenu id="idSelect" immediate="true" style="width:120px" value="#EntityBB.idUni}" label="#{bundleComunes.unidad}">
<f:selectItem itemLabel="#{bundleComunes.seleccionar}..." itemValue="" />
<f:selectItems value="#{configuracionBB.listEntity}" var="lUni" itemValue="#lUni.id}" itemLabel="#{lUni.desc}" />
<p:ajax event="change" update="sisArm" listener="#{entityBB.changeOneMenuP}" />
</h:selectOneMenu>
Any Idea????
better don't mix primefaces p:ajax with Pure JSF
try using f:ajax
<f:ajax event="change" render="sisArm" listener="#{entityBB.changeOneMenuP}" />
Also , you better access your id like this
public void changeOneMenuP(AjaxBehaviorEvent event) {
listEntity = escDao.findByxxxx(getidUni(), true);//or just idUni
}
Related
I'm trying to use ajax to update a component in my jsf page when selecting a radio button.
The managed bean is the following:
#SessionScoped
#ManagedBean
public class TestMB {
ArrayList<String> domande;
String[] risposte = new String[3];
public TestMB() {
domande = new ArrayList<String>();
domande.add("Domanda1");
domande.add("Domanda2");
domande.add("Domanda3");
}
public ArrayList<String> getDomande() {
return domande;
}
public void setDomande(ArrayList<String> domande) {
this.domande = domande;
}
public String[] getRisposte() {
return risposte;
}
public void setRisposte(String[] risposte) {
this.risposte = risposte;
}
public void asserta() {
System.out.println(risposte[0]);
}
}
The JSF page is this one:
<h:form id="label">
<p:panelGrid columns="1" layout="grid" styleClass="showcase-text-align-center">
<p:repeat var="domanda" value="#{testMB.domande}" varStatus="status">
<h1>#{domanda}</h1>
<p:selectOneRadio value="#{testMB.risposte[status.index]}">
<f:selectItem itemLabel="1" itemValue="1"></f:selectItem>
<f:selectItem itemLabel="2" itemValue="2"></f:selectItem>
<f:selectItem itemLabel="3" itemValue="3"></f:selectItem>
</p:selectOneRadio>
</p:repeat>
</p:panelGrid>
<p:outputLabel value="#{testMB.risposte[0]}"></p:outputLabel>
<p:commandButton update="label" value="Si" />
</h:form>>
I would like to update the form when I change option in the tag.
If I press the commandButton everything is ok but how to implement the same behavior in the radio button selection?
Thanks in advance!
You have the <p:ajax> component. Try something like this:
<p:selectOneRadio value="#{testMB.risposte[status.index]}">
<f:selectItem itemLabel="1" itemValue="1"></f:selectItem>
<f:selectItem itemLabel="2" itemValue="2"></f:selectItem>
<f:selectItem itemLabel="3" itemValue="3"></f:selectItem>
<p:ajax update="label" />
</p:selectOneRadio>
I have this code :
<p:inputText id="abc" value="#{myBean.name}" >
<f:validator validatorId="mandatoryInput" />
<p:ajax event="change" update="..." listener="#{myBean.myFunction()}" />
</oct:inputTextUpper>
When the Validator throw a new ValidatorException, is skipped. Is it possible to launch myFunction() even if validation is failed ?
Thanks :)
That's the default JSF behavior. As a work around you could work with f:event:
<p:inputText id="abc" value="#{myBean.name}">
<f:validator validatorId="mandatoryInput" />
<p:ajax event="change" update="..." listener="#{myBean.myFunction}" />
<f:event type="preRenderComponent" listener="#{myBean.callMyFunction}" />
</p:inputText>
In MyBean add the following method:
public void callMyFunction(ComponentSystemEvent e) {
UIInput input = (UIInput) e.getComponent();
if (!input.isValid()) {
myFunction();
}
}
I have an issue with missing ajax fired events. I have tried as much as I can to resolve this with searches, reading and experimentation.
Can someone assist in providing guidance?
Here is the form part:
<h:form>
...
<h:outputLabel>Chart Category:</h:outputLabel>
<h:selectOneMenu id="chartcategory" value="#{chartBean.category}" >
<f:ajax event="change" listener="#{chartBean.categoryChangedAJAX}"/>
<f:selectItems value="#{chartBean.categories}"/>
</h:selectOneMenu>
<h:outputLabel>Chart Type:</h:outputLabel>
<h:selectOneMenu value="#{chartBean.type}">
<f:selectItems id="charttypes" value="#{chartBean.types}"/>
</h:selectOneMenu>
...
</h:form>
Here is the code:
public void categoryChangedAJAX(final AjaxBehaviorEvent ajaxBehaviorEvent) {
System.out.println(" -------------------------------------- categoryChanged entered");
setTypes(chartCategoryAndTypes.getChartTypesForCategory( getCategory() ));
System.out.println("for Category: "+ getCategory() );
Arrays.stream(getTypes()).forEach(System.out::println);
setColumnVisibilities();
}
I have a datatable and two checkboxes. The content of the datatable is rendered considering which of the checkboxes are selected. I have the following functional code which renders the datable content when pressing the submit button:
<h:form>
<h:selectManyCheckbox value="#{myBean.selections}">
<f:selectItem itemValue="expired" itemLabel="Expired" />
<f:selectItem itemValue="active" itemLabel="Active" />
</h:selectManyCheckbox>
<h:commandButton id="submit" value="Select"
action="#{myBean.getList}" />
</h:form>
<h:form>
<h:dataTable ... />
</h:form>
However, I want to replace the submit button with an ajax call using the f:ajax tag. I tried something like:
<h:selectManyCheckbox value="#{myBean.selections}">
<f:selectItem itemValue="expired" itemLabel="Expired" />
<f:selectItem itemValue="active" itemLabel="Active" />
<f:ajax event="click" render="#form" listener="#{myBean.getList}" />
</h:selectManyCheckbox>
<h:form>
<h:dataTable .... />
</h:form>
But the content is not rendered. What am I doing wrong?
Bean:
public String[] selections = { "expired", "active" };
public String[] getSelections() {
return selections;
}
public void setSelections(String[] selections) {
this.selections = selections;
}
public void getList() {
for (String option : selections) {
// add expired
if (option.equals("expired"))
showExpired();
// add active
else if (option.equals("active"))
showActive();
}
//return Arrays.toString(selections);
}
I need to pass a parameter to the server in my ajax request. Please see the code below.
Scope: View Scope
Without f:param
<p:column width="40">
<h:inputText id="originalCostInputTxt" value="#{articlePromo.costoBruto}"
<f:ajax event="change"
execute="#this"
listener="#{promotionDetailManagedBean.onCostoBrutoChange}">
</f:ajax>
</h:inputText>
</p:column>
Managed Bean
public final void onCostoBrutoChange(final AjaxBehaviorEvent event) {
createCostoBrutoOptions(promoArticlesList);
}
In this case, the method onCostoBrutoChange() does gets invoked. But, it does not get invoked when I include f:param. Please see the code below.
With f:param
<p:column width="40">
<h:inputText id="originalCostInputTxt" value="#{articlePromo.costoBruto}"
<f:ajax event="change"
execute="#this"
listener="#{promotionDetailManagedBean.onCostoBrutoChange}">
<f:param value="#{articlePromo.promocionArticuloId}" name="myId"/>
</f:ajax>
</h:inputText>
</p:column>
Managed Bean
public final void onCostoBrutoChange(final AjaxBehaviorEvent event) {
createCostoBrutoOptions(promoArticlesList);
String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("myId");
}
Not able to identify whats incorrect in this code. Please guide.
Thanks,
Shikha
The <f:param> works in links and buttons only, not in inputs.
If your environment supports EL 2.2, just pass it as method argument instead:
<h:inputText ...>
<f:ajax listener="#{bean.listener(item.id)}" />
</h:inputText>
public void listener(Long id) {
// ...
}
You can also just pass the whole item:
<h:inputText ...>
<f:ajax listener="#{bean.listener(item)}" />
</h:inputText>
public void listener(Item item) {
// ...
}
If your environment doesn't or can't support EL 2.2, then evaluate EL programmatically instead.
public void listener() {
FacesContext context = FacesContext.getCurrentInstance();
Long id = context.getApplication().evaluateExpressionGet(context, "#{item.id}", Long.class);
// ...
}