I have an <p:selectOneMenu> which has an <f:selectItems> and in this <f:selectItems> I have set an itemLabel and an itemValue. The Item Value is an Enum Object which contains only a String and the Label is these String. The value of the <p:selectOneMenu> is an Collection of these Enum Objects. The selectOneMenu has an valueChangeListener and when its called it becomes the label as "new Value" and not the value. Has anyone an idea how I can fix this?
<h:form>
<p:selectOneMenu id="changeSpracheMenu"
value="#{sessionManager.currentUser.sprache}"
label="#{messageManager.getMessage('USER')}"
valueChangeListener="#{sessionManager.updateSpracheForCurrenUser}"
converter="sprachConverter">
<f:selectItem itemLabel="#{messageManager.getMessage('LANGUAGE')}"
noSelectionOption="true" />
<f:selectItems value="#{sprachManager.allSprachen}" var="sprache"
itemLabel="#{messageManager.getMessage('LANGUAGE_'.concat(sprache.name))}"
itemValue="#{sprache}" />
<p:ajax event="change" update="#form" />
</p:selectOneMenu>
</h:form>
Instead of using the Event of valueChangeListener, you can send the value of your selectItem to your bean.
For that, you have to modify
<p:ajax event="change" update="#form" />
To:
<p:ajax event="change" listener="#{yourBean.yourMethod(sprache)}" update="#form" />
Now in your bean:
private yourObject sprache;
//getter/setter
public void yourMethod(yourObject typeSprache){
whatever xyz = typeSprache.getWhatEverYouWant();
}
Related
So I have the following code:
<h:inputText value = "#{listAllBookings.searchText}">
<f:ajax listener="#{listAllBookings.printValues()}" event="keyup" render="myTable"/>
</h:inputText>
<h:selectOneMenu value="#{listAllBookings.selectedAttr}">
<f:selectItem itemLabel="GUEST" itemValue="GUEST"/>
<f:selectItem itemLabel="HOTEL" itemValue="HOTEL"/>
</h:selectOneMenu>
And my printValues method:
public void printValues() {
System.out.println("searchText:" + searchText + " and selectedAttr: " + selectedAttr);
}
So as you can see the code above is pretty simple.
The problem is that I can't change the value of the selectedAttr value. I already checked if I have the appropriate getter and setter methods.
The value of the selectedAttr remains null, while the searchText value changes.
The current output looks like this:
You did not specify a component for execution with your ajax requests. So only the textfield is executed.
Add an id to the selectOneMenu and execute it:
<h:inputText value="#{listAllBookings.searchText}">
<f:ajax listener="#{listAllBookings.printValues()}" event="keyup" render="myTable"
execute="#this selectSomething"/>
</h:inputText>
<h:selectOneMenu id="selectSomething" value="#{listAllBookings.selectedAttr}">
<f:selectItem itemLabel="GUEST" itemValue="GUEST"/>
<f:selectItem itemLabel="HOTEL" itemValue="HOTEL"/>
</h:selectOneMenu>
But you could also execute the surrounding form via execute="#form"
See also
Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes
try this:
<h:selectOneMenu value="#{listAllBookings.selectedAttr}">
<f:ajax event="change" listener="#{listAllBookings.printValues()}" />
<f:selectItem itemLabel="GUEST" itemValue="GUEST"/>
<f:selectItem itemLabel="HOTEL" itemValue="HOTEL"/>
</h:selectOneMenu>
This is my current structure for my p:selectOneMenu:
<h:form id="groupSelectionForm">
<p:outputLabel value="Momentane Gruppe:" for="groupSelection" />
<p:selectOneMenu id="groupSelection" value="#{foodPlanManagementBean.selectedGroup}" style="width:150px">
<f:selectItem itemLabel="-" itemValue="#{null}"/>
<f:selectItems value="#{foodPlanManagementBean.getGroups()}" var="group" itemLabel="#{group.name}" itemValue="#{group}"/>
<p:ajax event="change"/>
</p:selectOneMenu>
</h:form>
This results in a checkbox containing a default value given by the single selectItem as well as a few generated options from the selectItems.
However, the setter for the given field "selectedGroup" is only triggering for the selectItem.
The selectItems do not seem to do anything when they are being clicked.
Any ideas?
try to define a listener in ajax component, ex:
<p:ajax id="seasonAjax" event="change" process="#this" listener="#{yourBean.yourMethod}" update="elementThatYouWantToUpdate" />
process = this to process selected element.
In selectItems don't use get method use directly list elements(put get/set in your bean) ex:
<f:selectItems value="#{yourBean.yourList}" var="" itemLabel="" itemValue="" />
If this doesn't work test if you need to use a converter, if selectedGroup is a complex object or pass directly identification of selectGroup( selectedGroup.id)
I hope it helps.
I am using primtefaces 3.5 and below code to poplulate p:selectOneMenu. On selecting an item, I want to know the value of selected itemValue i.e. userStar.userStarId.
Have tried using f:setPropertyActionListener or f:attribute or passing the value in listener method, but no luck.
Thanks for your time and help.
<p:selectOneMenu editable="true" style="width:300px" value="#{starBean.newStarName}">
<f:selectItems value="#{starBean.userStarList}" var="userStar" itemLabel="#{userStar.starName}" itemValue="#{userStar.userStarId}" />
<p:ajax event="change" listener="#{starBean.changeValueListener}" process="#this" partialSubmit="true">
</p:ajax>
</p:selectOneMenu>
Change this
<p:selectOneMenu editable="true" style="width:300px" value="#{starBean.newStarName}">
to
<p:selectOneMenu editable="true" style="width:300px" value="#{starBean.userStarId}">
p:selectOneMenu value is the selected value here.
If you want to avoid using Custom Converter, you can put all your pojo in Map and load that in to p:selectOneMenu and once you get the selected pojo using Map.get(K).
Initialization:
Map<String,UserStar> userStarMap = new LinkedHashMap<String,UserStar>();
UserStar user1 = new UserStar(...);
userStrMap.put(user1.userStarId, user1);
...
...
...
Facelt:
<p:selectOneMenu editable="true" style="width:300px" value="#{starBean.newStarName}">
<f:selectItems value="#{starBean.userStarMap.values()}"
var="userStar" itemLabel="#{userStar.starName}" itemValue="#{userStar.userStarId}" />
<p:ajax event="change" listener="#{starBean.changeValueListener}" process="#this" partialSubmit="true">
</p:ajax>
</p:selectOneMenu>
Listener:
public void changeValueListener(){
UserStar selectedUser = userStarMap.get(newStarName);
}
I have problem with jsf. My jsf code is:
<h:form>
<p:selectOneMenu style="text-align:left;"
value="#{contractBean.selectedCust}" converter="CustomerConverter">
<f:selectItems value="#{classificatorBean.customerList}"
var="customer" itemLabel="#{customer.name} #{customer.sname}" itemValue="#{customer}" />
<p:ajax event="change" update="custTel" />
</p:selectOneMenu>
<p:inputText id="custTel" value="#{contractBean.selectedCust.name} " />
</h:form>
and I have managed bean (Contractbean) with getter and setter functions of selectedCust Customer object. My problem is when menu changed object dont show customers tel number.
try to use ajax like this:
<p:ajax event="change" process="#this" update="custTel" />
Here is my jsp:
<h:selectOneMenu value="#{member.dependentName}" onchange="this.form.submit()"
immediate="true" valueChangeListener="#{member.getDependentAddress}">
<f:selectItems value="#{member.dependentList}" />
</h:selectOneMenu>
<h:inputText value="#{member.personName}" immediate="true" />
<h:inputText value="#{member.dob}" immediate="true" />
And this, the function valuechangelistener fires.
public void getDependentAddress(ValueChangeEvent e) {
setPersonName((getDependentsList().get(e.getNewValue().toString())
.getDependentName()));
setDob(getDependentsList().get(e.getNewValue().toString()).getBirth());
System.out.println("New dob value : " + dob);
System.out.println("New name value : " + personName);
FacesContext.getCurrentInstance().renderResponse();
}
The two sysouts give the new value in the console but once the page loads, the fields are blank. I have tried all scopes for the bean. No go. What am i missing?
Thanks
You missed nothing. You've just something too much. To get it to work, you should remove immediate="true" from the to-be-changed components.
<h:selectOneMenu value="#{member.dependentName}" onchange="this.form.submit()"
immediate="true" valueChangeListener="#{member.getDependentAddress}">
<f:selectItems value="#{member.dependentList}" />
</h:selectOneMenu>
<h:inputText value="#{member.personName}" />
<h:inputText value="#{member.dob}" />
The immediate="true" on an UIInput component will cause its validations phase to take place in apply request values phase instead. This gives you the opportunity to use FacesContext#responseComplete() inside a valueChangeListener method skip other components which doesn't have immediate="true" set from being processed. As you have now, with immediate="true", they are also processed.
Please note that this is essentially a hack from the old JSF 1.x ages. If you're already using JSF 2.x, you should be using <f:ajax listener> instead.
<h:selectOneMenu value="#{member.dependentName}">
<f:selectItems value="#{member.dependentList}" />
<f:ajax listener="#{member.getDependentAddress}" render="name dob" />
</h:selectOneMenu>
<h:inputText id="name" value="#{member.personName}" />
<h:inputText id="dob" value="#{member.dob}" />
with
public void getDependentAddress() {
Dependent dependent = getDependentsList().get(dependentName); // Isn't that actually a Map instead of List?
personName = dependent.getDependentName();
dob = dependent.getBirth();
}