Add some action in SelectOneMenu - jsf

I need that the variable inside selectItems be available inside selectOneMenu or other way that I can grab what were selected by the user.
I need this because this item need to pass trough an Hibernate.initialize(item)
So I need to call the view (bean) method that I have build that does it.
<h:selectOneMenu
id="dependenteSalvar"
value="#{pessoaView.registro}"
styleClass="form-control"
converter="indexConverter">
<f:selectItem itemValue="#{null}" itemLabel="Selecione..."></f:selectItem>
<t:selectItems
value="#{pessoaView.pessoasAtivas}"
var="dependente"
itemLabel="#{dependente.nome} (#{dependente.documento})"
itemValue="#{dependente}">
</t:selectItems>
</h:selectOneMenu >
Is it possible?

You can use <f:ajax> tag to listen the events of selectOneMenu in your backend.
<h:selectOneMenu
id="dependenteSalvar"
value="#{pessoaView.registro}"
styleClass="form-control"
converter="indexConverter">
<f:selectItem itemValue="#{null}" itemLabel="Selecione..."></f:selectItem>
<t:selectItems
value="#{pessoaView.pessoasAtivas}"
var="dependente"
itemLabel="#{dependente.nome} (#{dependente.documento})"
itemValue="#{dependente}">
</t:selectItems>
<f:ajax listener="#{bean.listener}" />
</h:selectOneMenu >
Take into account that <f:ajax> requires jsf.js file is included in the HTML <h:head>. Basically, this file contains the Javascript functions to perform the magic.
Another option if you are using primefaces is to use
<p:ajax event="change" listener... /> instead of f:ajax, to listen the value change events as follows:
<p:selectOneMenu
id="dependenteSalvar"
value="#{pessoaView.registro}"
styleClass="form-control">
<f:selectItem itemValue="#{null}" itemLabel="Selecione..."></f:selectItem>
<f:selectItems
value="#{pessoaView.pessoasAtivas}"
var="dependente"
itemLabel="#{dependente.nome} (#{dependente.documento})"
itemValue="#{dependente}">
</f:selectItems>
<p:ajax listener="#{bean.listener}" event="change" />
</p:selectOneMenu >

Related

Update Component Even If SelectOneMenu Value Is Required

I have a required select and my problem is that when the user selects an empty option (null) the validation jumps in and the listener call of the select is being skipped. Some components on my view are depending on this value so in case of null being selected the bean won't get actualized and the components don't change.
Example:
<h:form id="createForm">
<p:selectOneMenu id="produktCreateTypSelect"
converter="omnifaces.SelectItemsIndexConverter"
value="#{productController.selectedProduct.typ}" required="true">
<f:selectItem itemValue="#{null}" itemLabel="-- Select --" />
<f:selectItems var="productType" value="#{productController.findAllProducts()}"
itemLabel="#{productType.name}" itemValue="#{productType}" />
<p:ajax process="#this " event="valueChange" update="createForm"
listener="#{productController.printHelloWorld}" />
</p:selectOneMenu>
<h:panelGroup id="dummy1" rendered="#{productController.selectedProduct.type != null}" />
</h:form>
The question is what the best way is to update that panelGroup
'dummy1'
Thanks for any help.

f:selectItems in a p:selectOneMenu are not triggering the setter for the given field

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.

change the variable JSF from SelectOneMenu

I have setted a JSF Varidable Like this:
<c:set var="selectedech" value="#{ManagedBean.selectedtypeFacturation}" scope="request"/>
After that i putted it on a SelectOneMenu:
<p:selectOneMenu panelStyle="width: 120px" effect="fade" id="typeFacture" value="#{selectedech}" >
<f:selectItem itemValue="Echeancier" itemLabel="Echeancier"/>
<f:selectItem itemValue="Mensuel" itemLabel="Mensuel"/>
<p:ajax event="change" update="#this"/>
</p:selectOneMenu>
I would like to change the value of JSF variable "selectedech" each time i change the selection.
Any idea in order to get it work well
Thx

JSF component loses CSS style after AJAX render call

I have a big complex form with validation on most of the fields. Somewhere I have <h:selectOneMenu> where if I select a certain value, I want another <h:selectOneMenu> to appear under it. The second list is initially invisible (I set it with rerender property to false and I make it true with clickListener() function and render it an ajax call).
<o:form>
<h:panelGroup id="group" >
<label>Status</label>
<h:selectOneMenu id="status" immediate="true" label="Status" value="#{candidateBean.statusVO}" converter="omnifaces.SelectItemsConverter" required="true" requiredMessage="The field is empty">
<f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
<f:selectItems value="#{settingsBean.settings.statusListVO.statusVO}" var="status" itemValue="#{status}" itemLabel="#{status.name}"/>
<f:ajax event="valueChange" listener="#{candidateBean.clickListener}" execute="form" render="group" />
</h:selectOneMenu>
<h:messages for="status" style="color:red" />
<label>Invalidation Reason</label>
<h:selectOneMenu id="invalidationReason" rendered="#{candidateBean.falseTest}" label="Invalidation Reason" value="#{candidateBean.invalidationReasonVO}" converter="omnifaces.SelectItemsConverter" requiredMessage="The field is empty">
<f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
<f:selectItems value="#{settingsBean.settings.invalidationReasonListVO.invalidationReasonVO}" var="invalidation" itemValue="#{invalidation}" itemLabel="#{invalidation.name}"/>
</h:selectOneMenu>
</h:panelGroup>
</o:form>
I used <o:form> instead of <h:form> because somewhere in the form I used <o:ignoreValidationFailed> because I needed to update a dataTable with values from a <h:selectOneMenu> and force update on my model.
Everything works except when I select a value from the list, the elements inside the panelGroup are rendered without any style at all and I can't figure out why. If I refresh the page the CSS is loaded on the elements but this is not an option.
My bean is #SessionScoped.

JSF input to multiple backing bean properties

I have some cases where I have a JSF page and for a single input I'd like to set more than one value on the backing bean without doing code in the backing bean.
I can set a single value:
<h:selectOneRadio id="selectMembershipLevel" class="TODO_SELECT"
value="#{joinBackingBean.map[joinBackingBean.map.primary_memberInfo_membershipType_code]}">
<f:selectItem id="basic" itemLabel="#{overrideMsg.pbBasic}" itemValue="B" />
<f:selectItem id="plus" itemLabel="#{overrideMsg.pbPlus}" itemValue="P" />
<f:selectItem id="plusRV" itemLabel="#{overrideMsg.pbPlusRV}" itemValue="RV" />
But if I wanted to set more than one at once can that be done on the JSF page?
#{joinBackingBean.map[joinBackingBean.map.primary_memberInfo_membershipType_code]}
#{joinBackingBean.map[joinBackingBean.map.primary_memberInfo_membershipType_desc]}//Bdesc
#{joinBackingBean.map[joinBackingBean.map.primary_memberInfo_membershipType_type]}//Btype
Bind the other properties via <h:inputHidden> and use JavaScript during change event of the first input to manipulate the value of those hidden inputs to the same as the first input's current value.
Here's a kickoff example:
<h:form id="form">
<h:selectOneRadio value="#{bean.input1}" onchange="document.getElementById('form:input2').value = document.getElementById('form:input3').value = this.value">
<f:selectItem itemValue="one" />
<f:selectItem itemValue="two" />
<f:selectItem itemValue="three" />
</h:selectOneRadio>
<h:inputHidden id="input2" value="#{bean.input2}" />
<h:inputHidden id="input3" value="#{bean.input3}" />
...
</h:form>
Obviously, feel free to refactor to a JS function or throw in some jQuery. Keep in mind that this all won't work if client has JS disabled, and that the enduser could manipulate JS code and request parameters. A server side solution is more robust if you absolutely need the values to be equal.

Resources