h:selectOneMenu change event with p:ajax - java

I am trying to get the value from on change in h:selectOneMenu using p:ajax.
But I am getting null value always, dont know whats wrong below is the code.
<h:form>
<h:selectOneMenu id="selectMenu" value="#{userHomeController.gymsSelectType}">
<f:selectItem itemLabel="Close to me" itemValue="closest" />
<f:selectItem itemLabel="Visited by me" itemValue="visited" />
<p:ajax process="selectMenu" listener="#{userHomeController.selectMenuListener}" event="change" update=":home-form:panel" />
</h:selectOneMenu>
</h:form>
and the bean class is
public void selectMenuListener() {
System.out.println("-------- >> " + gymsSelectType); // here null coming
if (gymsSelectType.equals("a")) {
//
} else {
//
}
}
this is a viewscoped class.
and below setter for the variable gymsSelectType is also prints null
public void setGymsSelectType(String gymsSelectType) {
System.out.println("------------ >> "+gymsSelectType);
this.gymsSelectType = gymsSelectType;
}

Have your tries this
<p:ajax process="#this" listener="#{userHomeController.selectMenuListener}" event="change" update=":home-form:panel" />

Try this
<p:ajax partialSubmit="true" listener="#{userHomeController.selectMenuListener}" event="change" update=":home-form:panel" />

Related

<p: ​selectOneMenu /> <f: selectItems> do not appear

I'm developing a java application using jsf and put the <p: ​​selectOneMenu /> component primefaces in my project,But the <p: ​​selectOneMenu /> <f: selectItems> component items do not appear and <selectOneMenu /> has disappeared
UPDATE
<h:panelGroup layout="block" styleClass="tamanho">
<p>Tamanho:</p> <p:selectOneMenu id="tamanhos" value="#{tamanho.tamanho}" styleClass="tamanho" effect="fold" editable="true">
<f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
<f:selectItems value="#{tamanho.tamanhos_roupas}" />
</p:selectOneMenu>
#ManagedBean(name="tamanho")
#RequestScoped
public class Tamanho {
private String tamanho;
private List<SelectItem> tamanhos_roupas; // +getter (no setter necessary)
#PostConstruct
public void init() {
tamanhos_roupas = new ArrayList<SelectItem>();
tamanhos_roupas.add(new SelectItem("PP", "PP"));
tamanhos_roupas.add(new SelectItem("P", "P"));
tamanhos_roupas.add(new SelectItem("M", "M"));
tamanhos_roupas.add(new SelectItem("G", "G"));
tamanhos_roupas.add(new SelectItem("GG", "GG"));
}
public String getTamanho() {
return tamanho;
}
public void setTamanho(String tamanho) {
this.tamanho = tamanho;
}
public List<SelectItem> getTamanhos_roupas() {
return tamanhos_roupas;
}
}
Look how the copier of the primefaces was, disappeared itens the <p:selectOneMenu/>
page
I'm assuming "#{tamanho.tamanhos_roupas}" is a list that has values in it already, so what you need to do is add 'var', 'itemValue' and 'itemLabel' in your selectItems tag.
The 'var' attribute is the name of the variable of your list elements in every iteration, 'itemValue' is what #{tamanho.tamanho} value will be after selecting an option and 'itemLabel' is what will the user read to identify that option.
<h:panelGroup layout="block" styleClass="tamanho">
<p>Tamanho:</p> <p:selectOneMenu id="tamanhos" value="#{tamanho.tamanho}"
styleClass="tamanho" effect="fold" editable="true">
<f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
<f:selectItems value="#{tamanho.tamanhos_roupas}" var="tamanho"
itemValue="#{tamanho}" itemLabel="#{tamanho.stringNome}" />
</p:selectOneMenu>
See, the way your code is written you are only giving your SelectOneMenu the information you have items to iterate, but you never tell him what is the name and value of those items.

set related values to null after selecting <f:selectItem>

below is my jsf code,
<h:outputText value="SP Id" styleClass="required"/>
<h:selectOneMenu style="padding-left:60px;" class="input" id="spid" required="true" requiredMessage="Select SP Id"
value="#{applicationController.spid}">
<p:ajax listener="#{applicationController.onFromChange()}"
update="fromnames" />
<f:selectItem itemValue="" itemLabel="--Select--" />
<f:selectItems value="#{applicationController.spids}"></f:selectItems>
</h:selectOneMenu>
<h:message for="spid" class="hmsg" />
<h:outputText value="Sp Name" class="left1"/>
<h:inputText class="input" id="fromnames"
value="#{applicationController.spname}" />
<h:message for="fromnames" />
backing bean code is(method),
public void onFromChange() {
if (spid != null && !spid.equals("")) {
int spId = Integer.parseInt(spid);
spname = baseService.getSalesPersonById(spId);
} else {
}
}
//setter-getters
public String getSpname() {
return spname;
}
public void setSpname(String spname) {
this.spname = spname;
}
public List<Integer> getSpids() {
return spids;
}
public void setSpids(List<Integer> spids) {
this.spids = spids;
}
from above code every thing works fine.
problem:if i select f:selectItems values, relating values(spname)
are displaying. after selecting f:selectItem spname should set to null but it's not setting to null, instead of that previous values are displayed.
change jsf code as below,
<h:selectOneMenu style="padding-left:60px;" class="input" id="spid" required="true" requiredMessage="Select SP Id"
value="#{applicationController.spid}">
<p:ajax listener="#{applicationController.onFromChange()}"
update="myForm1" event="change" process="#this"/>
<f:selectItem itemValue="0" itemLabel="--Select--" noSelectionOption="false" />
<f:selectItems value="#{applicationController.spids}"></f:selectItems>
</h:selectOneMenu>
<h:message for="spid" class="hmsg" />
and modify backing bean method as,
public void onFromChange() {
if (spid != null && !spid.equals("")) {
try{
int spId = Integer.parseInt(spid);
spname = baseService.getSalesPersonById(spId);
}
catch(Exception exception){
spname = null;
}
} else {
spname = null;
}
}

JSF Updating p:selectOneMenu from another p:selectOneListbox with Ajax

I've been trying to do something that would be really simple... yet it won't work.
I have 2 "p:selectOneMenu", so that the second one items depends on the chosen item in the first one. They both have their own methods in the bean to list their items.
What I wanted to do was when the first changes its value, update the another so that the component should reload the list considering the choosen object. But the ajax never happens, at least in debug the method in the Bean is never called again.
What is wrong? The code is something like...
Thanks!!! :)
<p:selectOneMenu value="#{itemBean.lotacao}">
<f:selectItem itemLabel="Choose one" />
<f:selectItems value="#{itemBean.listLotacoes()}"
var="lotacao"
itemLabel="#{lotacao.format()}" />
<p:ajax update="localizacaoPorLotacao" />
</p:selectOneMenu>
<p:selectOneMenu id="localizacaoPorLotacao"
value="#{itemBean.localizacao}">
<f:selectItem itemLabel="Choose one" />
<f:selectItems value="#{itemBean.listByLotacao(itemBean.lotacao)}" />
</p:selectOneMenu>
#ManagedBean
#SessionScoped
public class ItemBean {
// attributes, getters and setters here...
public List<Lotacao> listLotacoes() {
LotacaoService lotacaoService = new LotacaoService();
List<Lotacao> lotacoes = lotacaoService.getAll();
return lotacoes;
}
public List<Lotacao> listByLotacao(Lotacao lotacao) {
if (lotacao == null) {
return new ArrayList<Lotacao>();
}
String prefixo = lotacao.getCodigo().substring(0, 1);
LotacaoService lotacaoService = new LotacaoService();
List<Lotacao> lotacoes = lotacaoService.getByPrefix(prefixo);
return lotacoes;
}
}
Found it!
The lack of a converter to the class made the JSF don't trigger the setter.
After creating a converter and adding it to the OneMenu, the code worked just fine.
The final version is:
<p:selectOneMenu value="#{itemQuadroVagasBean.lotacao}"
converter="orgaoLotacaoCodigoConverter">
<p:ajax event="change"
update="localizacaoPorLotacao" />
<f:selectItem itemLabel="Selecione a lotação" />
<f:selectItems value="#{itemQuadroVagasBean.listarLotacoes()}"
var="lotacao"
itemLabel="#{lotacao.formatarLocalizacao()}" />
</p:selectOneMenu>
Maybe you need event:
<p:ajax update="localizacaoPorLotacao" event="change"/>

How to update JSF SelectManyListBox items from selected item in SelectOneMenu?

I have the following components:
<h:selectOneMenu id="company"
value="#{companyController.selected.companyId}"
onchange="?????????">
<f:selectItems value="#{companyController.itemsAvailableSelectOne}"/>
</h:selectOneMenu>
<h:outputLabel value="some value" for="locations" />
<h:selectManyListbox id="locations" >
<f:selectItems value="#{companyController.selected.locationCollection}"/>
</h:selectManyListbox>
Every time a company is selected in the SelectOneMenu i need to update the items in the SelectManyListBox.
Please Help me
Thank you very much!
You are going to want to use <f:ajax>. Perhaps something like this:
<h:selectOneMenu id="company"
value="#{companyController.selected.companyId}">
<f:ajax event="valueChange" execute="#this" render="#this locations" />
<f:selectItems value="#{companyController.itemsAvailableSelectOne}"/>
</h:selectOneMenu>
<h:outputLabel value="some value" for="locations" />
<h:selectManyListbox id="locations" >
<f:selectItems value="#{companyController.selected.locationCollection}"/>
</h:selectManyListbox>
Then you could modify your selected objects setCompanyId method to update the locationCollection:
public void setCompanyId( long companyId ) {
this.companyId = companyId;
// now update your location collection
this.locationCollection = locationCollectionMap.get( companyId );
}

multiple f:ajax calls in multiple h:selectOneMenu

Consider the following form:
1. User selects a value from the selectOneMenu "selectorDB"
2. Ajax call returns items for the second selectOneMenu and makes the whole "second" panelGroup rendered
3. User selects a value from the second selectOneMenu
4. Ajax call returns String which is placed inside of the inputText and panelGroup which contains that inputText rendered.
Everything works fine till im not selecting a value from the second selectOneMenu. Ajax of the fourth action is not even called. I've placed a couple of console System.outs to check if the listener is called and got nothing.
Is that possible to make something like the thing I'm attempting to do?
Thank you for any replies.
*.jsf:
<h:form>
<h:panelGroup id="first">
<h:outputLabel value="Select a database to make queries against" for="selectorDB" />
<h:selectOneMenu id="selectorDB" value="#{sparqlQuerier.chosenRing}">
<f:selectItem itemLabel="Select..." itemValue="0" />
<f:selectItems value="#{sparqlQuerier.ringTitles}" />
<f:ajax render="second third" listener="#{sparqlQuerier.firstListener}"/>
</h:selectOneMenu>
</h:panelGroup>
<br />
<h:panelGroup id="second">
<h:outputLabel rendered="#{sparqlQuerier.doRenderSecond}" value="Select a query to execute" for="selectorQuery" />
<h:selectOneMenu rendered="#{sparqlQuerier.doRenderSecond}" id="selectorQuery" value="#{sparqlQuerier.chosenQuery}">
<f:selectItem itemLabel="Select..." itemValue="0" />
<f:selectItems value="#{sparqlQuerier.queries}" />
<f:ajax render="third" listener="#{sparqlQuerier.secondListener}"/>
</h:selectOneMenu>
</h:panelGroup>
<br />
<h:panelGroup id="third">
<h:inputText rendered="#{sparqlQuerier.doRenderThird}" value="#{sparqlQuerier.queryBody}" onfocus="if (this.value == '#{sparqlQuerier.queryBody}') {this.value = '';}" onblur="if (this.value == '') {this.value ='#{sparqlQuerier.queryBody}';}" />
</h:panelGroup>
</h:form>
bean:
public void firstListener(AjaxBehaviorEvent event)
{
doRenderThird = false;
doRenderSecond = false;
if(chosenRing!=0)
{
doRenderSecond = true;
try
{
// returning rows from the database...
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
public void secondListener(AjaxBehaviorEvent event)
{
doRenderThird=false;
if(chosenQuery!=0)
{
if(chosenQuery==-1)
{
queryBody="body #1";
}
else
{
queryBody="body #2";
}
doRenderThird=true;
}
System.out.println(chosenQuery);
System.out.println(doRenderThird);
}

Categories

Resources