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
Related
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 >
I'm using primefaces and have a datatable that has 2 columns. One column is outputtext another one is selectOneMenu. I made editable selectOneMenu. Actually it's work but not properly. Default value of selectItem is shown null but ı want it to show first value as default value. How can I make it?
<p:dataTable id="cellEditingTable" var="message"
value="#{messageTableController.menuList}" paginator="true"
paginatorPosition="bottom" editable="true" editMode="cell">
<p:column>
<p:selectOneMenu id="menu" value="#{messageTableController.selected}"
style="width:96%" effect="fold" editable="true">
<f:selectItem itemLabel="#{message.assign}"
itemValue="#{message.assign}" />
<f:selectItem itemLabel="#{message.combo}"
itemValue="#{message.combo}" />
</p:selectOneMenu>
</p:column>
</p:dataTable>
I change selectOneMenu's value to first ItemValue. So when run the program, first ItemValue picked selected item as a default.
<p:selectOneMenu id="menu" value="#{message.assign}"
style="width:97%" editable="true">
<f:selectItem itemLabel="#{message.assign}"
itemValue="#{message.assign}" />
<f:selectItem itemLabel="#{message.combo}"
itemValue="#{message.combo}" />
</p:selectOneMenu>
Add noSelectionOption to the first selectItem
<f:selectItem itemLabel="#{message.assign}"
itemValue="#{message.assign}" noSelectionOption="true" />
May be late to answer this.
If you use editable="true" then while rendering in the browse, it will render as textbox.
Remove the editable="true" then the default value will be selected.
If you want to provide the filter option in the selectOneMenu, use filter="true"
<p:selectOneMenu id="menu" value="#{message.assign}"
style="width:97%" filter="true">
<f:selectItem itemLabel="#{message.assign}"
itemValue="#{message.assign}" />
<f:selectItem itemLabel="#{message.combo}"
itemValue="#{message.combo}" />
</p:selectOneMenu>
Based on the editable property, it will render in browser as below.
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 would like to load list of <f:selectItems> only when user opens <p:selectOneMenu>.
I tried this way but doesn't work:
<p:selectOneMenu id="bases" value="#{sucesoBB.suceso.base}" converter="EntitiesCachedConverter" >
<f:selectItem itemLabel="" itemValue="" />
<f:selectItems value="#{sucesoBB.bases}" var="base" itemValue="#{base}" itemLabel="#{base.id} - #{base.descripcion}" />
<p:ajax event="click" onstart="#{sucesoBB.bases == null or sucesoBB.bases.size() ==1 or sucesoBB.bases.empty()?'cargaBases();':''}" />
</p:selectOneMenu>
<p:remoteCommand name="cargaBases" update="bases" actionListener="#{sucesoBB.cargaBases}" />
How can I achieve this?
A "dynamic" attribute was recently added to the selectOneMenu component.
Just add it like this: <p:selectOneMenu dynamic="true" ... >
https://github.com/primefaces/primefaces/issues/2399
Demo:
https://www.primefaces.org/showcase/ui/input/oneMenu.xhtml
You need to have at least Primefaces version 6.0.20, 6.1.2 or 6.2
My selectOneMenu isn't fireing an onchange event. Here is the code:
<p:selectOneMenu id="select_preset"
value="#{JobMgmtBean.presetGroupName}"
valueChangeListener="#{PFJobMgmtBean.PresetGroupChangeEvent}"
onchange="submit();">
<f:selectItems value="#{JobMgmtBean.presetGroupList}"/>
</p:selectOneMenu>
The selectOneMenu is nicely populated, and I can select different values. But I would expect that after I change a selection, page would be refreshed, i.e. backing bean (RequestScoped) would be recreated (onchange="submit();")? But nothing happens when selection in the selectOneMenu is changed.
Also, value change listener PresetGroupChangeEvent() is not called.
Did I go wrong somewhere?
I'm using Tomcat 7.0.25 + MyFaces 2.1.6 + PrimeFaces 3.2.
try to remove
onchange="submit();"
and to add
<p:ajax update="#this"/>
from your p:selectOneMenu when you using primefaces
<p:selectOneMenu id="select_preset"
value="#{JobMgmtBean.presetGroupName}"
valueChangeListener="#{PFJobMgmtBean.PresetGroupChangeEvent}">
<f:selectItems value="#{JobMgmtBean.presetGroupList}"/>
<p:ajax update="#this"/>
</p:selectOneMenu>
check your listener signature (starts from big "P" ?)
import javax.faces.event.ValueChangeEvent;
public void PresetGroupChangeEvent(ValueChangeEvent event) { }
why you are not adding update attribute using p:ajax? I think rerendering components more better solution than refreshing, also you can just rerender needed components. But if you still want to refresh page you can also use javascript:
<p:selectOneMenu id="select_preset" value="#{JobMgmtBean.presetGroupName}" onchange="window.location.reload();">
<f:selectItems value="#{JobMgmtBean.presetGroupList}"/>
</p:selectOneMenu>
and just for updating necessary components:
<p:selectOneMenu id="select_preset" value="#{JobMgmtBean.presetGroupName}">
<f:selectItems value="#{JobMgmtBean.presetGroupList}"/>
<p:ajax event="change" update="#form" />
</p:selectOneMenu>
this works for me
<p:selectOneMenu id="select_preset"
value="#{JobMgmtBean.presetGroupName}"
valueChangeListener="#{PFJobMgmtBean.PresetGroupChangeEvent}">
<f:selectItems value="#{JobMgmtBean.presetGroupList}"/>
<p:ajax process="select_preset" partialSubmit="true" event="valueChange" update="yourComponentName"/>
</p:selectOneMenu>
try to use ajax from primefaces and refresh only what you want to refresh