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.
Related
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"/>
I have a javascript code that clears all the p:inputText (after a p:commandButton action) of the form. The problem is that the p:selectOneMenu still has the f:selectItem selected in the option it was selected. I need to put the values in the first f:selectItem of each p:selectOneMenu.
How to I do that? How can I clear the selected values?
The java script code:
<script type="text/javascript">
function limpiarForm()
{
document.getElementById("formularioAltas").reset();
}
</script>
formularioAltas is the form id.
The code of the p:commandButton:
<p:commandButton value="Guardar" action="#{altasBean.agregarRefaccion()}" oncomplete="limpiarForm()" />
And that code does not reset(I dont want to clear the values, I just want to put the first option selected) the values of the p:selectOneMenu
Here it is:
<h:outputText value="Estado de la refacción" />
<p:selectOneMenu value="#{altasBean.refaccion.estado}">
<f:selectItem itemLabel="..." itemValue="0" />
<f:selectItem itemLabel="Ok" itemValue="Ok" />
<f:selectItem itemLabel="Reparar" itemValue="Reparar" />
<f:selectItem itemLabel="Sospechoso" itemValue="Sospechoso" />
</p:selectOneMenu>
The bean:
private RefaccionBean refaccion = null;
/**
* Get the value of refaccion
*
* #return the value of refaccion
*/
public RefaccionBean getRefaccion() {
return refaccion;
}
/**
* Set the value of refaccion
*
* #param refaccion new value of refaccion
*/
public void setRefaccion(RefaccionBean refaccion) {
this.refaccion = refaccion;
}
public void agregarRefaccion() {
I did a lot of things here...
And after those things i clear the p:inputText with the javascript code
-> After that i want to set the values of the p:selectOneMenu in the fist f:selectItem
}
Josef's answer does lead you in the right direction, but since you shared some code, I will just use it in my answer.
First, make sure that your button calls your bean method and updates the selectOneMenu component after it's done. Although there's some ajax going on here, primefaces abstracts that for you.
<p:commandButton value="Guardar" action="#{altasBean.agregarRefaccion}" update="selectOneMenu" />
The update attribute is important as it will look for the component whose id matches whatever is specified there. So you need to give your selectOneMenu an id. If you need to update more than one component, you can add their ids to the update attribute separated by either space or comma.
<h:outputText value="Estado de la refacción" />
<p:selectOneMenu value="#{altasBean.refaccion.estado}" id="selectOneMenu">
<f:selectItem itemLabel="..." itemValue="0" />
<f:selectItem itemLabel="Ok" itemValue="Ok" />
<f:selectItem itemLabel="Reparar" itemValue="Reparar" />
<f:selectItem itemLabel="Sospechoso" itemValue="Sospechoso" />
</p:selectOneMenu>
Now it's just a matter of cleaning up your values inside your action method:
public void agregarRefaccion() {
//If you have other values to clean, clean them here
this.refaccion.estado="0"; //Matches the value for your first item
}
Now I'm not entirely sure if I follow you, but let me know if this helps you out at all. It's entirely JSF and I threw in some ajax. You can play around with your javascript still.
Front-end:
<p:inputText id="input" value="#{bean.value}" />
<p:commandButton value="button" action="#{bean.action}">
<f:ajax execute="input" render="output" />
</p:commandButton>
<p:selectOneMenu id="output" value="#{bean.placeValue}">
<f:selectItems value="#{bean.values}" />
</p:selectOneMenu>
Bean:
#ManagedBean
#RequestScoped
public class Bean {
private String value;
private List<String> values;
#EJB
private ValueService valueService;
#PostConstruct
public void init() {
values = valueService.list();
}
public void action() {
// ... Action taken
}
public String placeValue() {
// ... Validation and clear desired values
return value;
}
// ... (getters, setters, etc)
}
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 );
}
I have the following facelet code which is failing:
<h:form>
<rich:select defaultLabel="Seleccionar región">
<f:selectItems value="#{StaticInfo.regiones}" var="region" itemValue="#{region.reg_Cod}" itemLabel="#{region.reg_Nombre}" />
<a4j:ajax event="change" render="provs" />
</rich:select>
<rich:select id="provs" defaultLabel="Seleccionar provincia">
<f:selectItems value="#{region.provincias}" var="prov" itemValue="#{prov.prov_Cod}" itemLabel="#{prov.prov_Nombre}" />
</rich:select>
</h:form>
Backing bean:
public class StaticInfoBean {
private ArrayList<Region> regiones;
public StaticInfoBean() {
try
{
RegionDAO regDao = new RegionDAO();
regDao.prepareConnection();
ProvinciaDAO provDao = new ProvinciaDAO();
provDao.setCon(regDao.getCon());
ComunaDAO comDao = new ComunaDAO();
comDao.setCon(regDao.getCon());
regiones = regDao.listaRegiones();
for(Region r : regiones)
{
regDao.findProvincias(r);
for(Provincia p : r.getProvincias())
{
provDao.findComunas(p);
for(Comuna c : p.getComunas())
{
comDao.findColegios(c);
}
}
}
regDao.getCon().close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public ArrayList<Region> getRegiones() {
return regiones;
}
public void setRegiones(ArrayList<Region> regiones) {
this.regiones = regiones;
}
public String toString() {
return regiones.toString();
}
}
Class Region:
public class Region {
private String Reg_Cod;
private String Reg_Nombre;
private ArrayList<Provincia> provincias;
//Getters and setters
The problem: The first rich:select tag works just fine. However the second one doesn't display any value. Not just that, but I'm working in NetBeans and it doesn't display the list of methods for class Provincia whenever I type "prov" in the EL.
Any help is appreciated.
EDIT: I edited my code and did the following:
<h:form>
<rich:select defaultLabel="Seleccionar región" value="#{StaticInfo.regionElegida}">
<f:selectItems value="#{StaticInfo.regiones}" var="region" itemValue="#{region.reg_Cod}" itemLabel="#{region.reg_Nombre}" />
<a4j:ajax event="click" render="provs" execute="#this" />
<a4j:ajax event="click" render="texto" execute="#this" />
</rich:select>
<h:outputText id="texto" value="#{StaticInfo.regionElegida.reg_Nombre}" />
<rich:select id="provs" defaultLabel="Seleccionar provincia" value="#{StaticInfo.provinciaElegida}" rendered="#{not empty StaticInfo.regionElegida}">
<f:selectItems value="#{StaticInfo.regionElegida.provincias}" var="prov" itemValue="#{prov.prov_Cod}" itemLabel="#{prov.prov_Nombre}" />
</rich:select>
</h:form>
What's surprising is that the outputText isn't being displayed! Why would this happen?
The variable region set in f:selectItems is only available inside its scope.
You have to bind each rich:select to a backing bean property.
So, you need to change your bean code, adding properties for the chosen region to the bean:
public class StaticInfoBean {
private ArrayList<Region> regiones;
private Region regionElegida;
private Provincia provinciaElegida; // <-- you'll probably want this too...
// ... getters and setters and your initialization code
// and you need something to find the real region object that
// used the reg_Cod value got from rich:select, for now you can try this:
public void updateRegionElegida(AjaxBehaviorEvent e){
// set the chosen region to regionElegida,
// or the poor man's converter
for (Region region : regiones){
if(regionElegida.getReg_Cod() == region.getReg_cod()) {
regionElegida = region;
}
}
}
}
and then change your XHTML to something like this:
<h:form>
<rich:select value="#{StaticInfo.regionElegida}"
defaultLabel="Seleccionar región">
<f:selectItems value="#{StaticInfo.regiones}" var="region"
itemValue="#{region.reg_Cod}" itemLabel="#{region.reg_Nombre}" />
<a4j:ajax event="change" render="provs"
listener="#{StaticInfo.updateRegionElegida}" />
</rich:select>
<rich:select id="provs" value="#{StaticInfo.provinciaElegida}"
defaultLabel="Seleccionar provincia">
<f:selectItems value="#{StaticInfo.regionElegida.provincias}" var="prov"
itemValue="#{prov.prov_Cod}" itemLabel="#{prov.prov_Nombre}" />
</rich:select>
</h:form>
Now, the professional way of doing that conversion thing would be to use a custom converter that can find the appropriate instance of a Region object based on the value of the expression itemValue used for its select widget. Check out this example from another question.
Note that rich:select only adds functionality to the default h:selectOneMenu, you would do good checking out its info page here at SO.
I have a scenario to achieve but I don't know how to do it(I'am using primefaces)
The scenario is:
I have a selectOneMenu that contain options that the user can choose one of them,
when the user choose an option ,a "form" or a "Sub-page" should appear below the selectOneMenu , the user should fill it.
The "Sub-page" change according to the choosen option
eg :option1 ---> Sub-Page1
option2 --->Sub-Page2 etc..
JSF
<h:selectOneMenu style="width:200px" immediate="true" value="#{ToolsKPI.myChoice}" onchange="submit()" valueChangeListener="#{BeanTest.selectChangeHandler}" required="true" >
<f:selectItems value="#{ToolsKPI.getMyListKPI()}" />
</h:selectOneMenu>
<p:panel rendered="#{BeanTest.showPanelBool}" >
<h1>KPI1</h1>
<br></br>
<h:inputText value="test1" />
</p:panel>
<p:panel rendered="#{BeanTest.showPanelBool1}" >
<br></br>
<h1>KPI2</h1>
<h:inputText value="test2" />
<h:inputText value="test3" />
</p:panel>
My Bean
public class BeanTest implements java.io.Serializable {
private String myChoice;
private boolean showPanelBool;
private boolean showPanelBool1;
public BeanTest() {
}
//getters ande setters
public void selectChangeHandler(ValueChangeEvent event){
myChoice = (String) event.getNewValue(); // Must however be the exact page URL. E.g. "contact.jsf".
FacesContext.getCurrentInstance().getExternalContext();
if(myChoice.equals("Number Of Closed issues") ){
this.showPanelBool = true;
} else{
this.showPanelBool = false;
this.showPanelBool1 = true;
}
FacesContext.getCurrentInstance().renderResponse();
}
}
You should really post some semblance of the code: a picture something. What you're probably looking for is a "p:outputPanel" component or something similar.
http://www.primefaces.org/showcase/ui/outputPanel.jsf
You can do something with a ValueChangeListener akin to
public void onSelectItem(ValueChangeEvent evt)
{
if("someValue".equals(evt.getNewValue())
showPageOne = true;//boolean
else
showPageTwo = true;//boolean
}
//JSF
<h:form id="myForm">
<h:selectOneMenu valueChangeListener="#{myBean.onSelectItem}" update="myForm">
<f:selectItems value="items">
</h:selectOneMenu>
<p:outputPanel rendered="#{myBean.showPageOne}">
hi I'm page one
</p:outputPanel>
<p:outputPanel rendered="#{myBean.showPageTwo}">
hi I'm page two
</p:outputPanel>
</h:form>
Sorry for the pseudo code, the examples on Primefaces will really help you here. OutputPanel has saved my bacon often. You'll need to specify an update target, but the example is really clear and easy to run. Play with it.
(Also Primefaces is fantastic you'll likely be very happy with their component suite)