p:SelectOneMenu default value in edit mode - jsf

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.

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.

p:selectOneMenu ajax change event not working

I have the following component on the xhtml:
<p:outputLabel for="concept" value="Concept" />
<p:selectOneMenu id="concept" value="#{dIGRCController.digrc.qconce}"
requiredMessage="Debe de seleccionar un concepto."
required="#{request.getParameter('validate')}">
<f:selectItem itemLabel="Concept" itemValue="" />
<f:selectItems value="#{cCRX1Controller.ccrx1.concepts.entrySet()}"
var="concepts" itemValue="#{concepts.key}"
itemLabel="#{concepts.value}" />
<p:ajax event="change" execute="#this" listener="#{dIGRCController.testing()}" />
</p:selectOneMenu>
The values are coming correctly from the cCRX1Controller class. However, when I select an option, I am trying to display the value. the #{dIGRCController.testing()} looks like this:
public void testing()
{
System.out.println("sdfsd");
}
What am I doing wrong? I checked this question but could't figure out what the problem is.
Try with this
<p:ajax process="#this" listener="#{dIGRCController.testing()}" />
I think the default of ajax event of Primefaces select one menu is valueChange event. So, try with using also
<p:ajax event="valueChange" process="#this" listener="#{dIGRCController.testing()}" />

Why does p:poll do not start until I click a row of p:dataTable?

I have a p:poll that updates a p:dataTable but the problem is that it does not start updating until I click on a row of the p:dataTable. When the page loads nothing happend until I click a row, after that everything works fine.
I have read about p:poll and have autostart="true" by default.
This is my p:poll
<p:poll interval="5" listener="#{adminUsuarios.actualizarLista()}" update="tblUsuarios" />
This is my p:dataTable
<p:dataTable id="tblUsuarios" var="us" value="#{adminUsuarios.listaUsuarios}" paginator="true" rows="50" paginatorPosition="both" selectionMode="single" selection="#{adminUsuarios.usuarioSeleccionado}" rowKey="#{us.id_User}">
<p:ajax event="rowSelect" oncomplete="PF('editarDialog').show()" update=":myForm:myDialog" />
<p:column headerText="Usuario">
<h:outputText value="#{us.id_User}" />
</p:column>
<p:column headerText="Nombre">
<h:outputText value="#{us.nameU}" />
</p:column>
</p:dataTable>
When a row of a table is clicked I show a dialog. I think the problem could be with this stuff. Everything is in the same h:form and only works (the p:poll) after I click a row of the p:datatable
This is my p:dialog
<p:dialog id="myDialog" widgetVar="editarDialog" header="Editar perfil de usuario #{adminUsuarios.usuarioSeleccionado.id_User}" resizable="false" width="400" showEffect="size" hideEffect="size">
<h:panelGrid columns="2" cellpadding="4" style="margin: 0 auto;">
<h:outputText value="Perfil" />
<p:selectOneMenu id="cboPerfil" value="#{adminUsuarios.usuarioSeleccionado.nivelAlmacen}" >
<f:selectItem itemLabel="..." itemValue="0" />
<f:selectItem itemLabel="2" itemValue="2" />
<f:selectItem itemLabel="1" itemValue="1" />
</p:selectOneMenu>
</h:panelGrid>
</p:dialog>
Everything is showed is the order it is in my code.
It happens because your dialog is not dynamic and fetch it's contents on page load.
At this time no selection has been made and property #{adminUsuarios.usuarioSeleccionado} is null. That cause PropertyNotFoundException.
You can either set p:dialog componet dynamic attribute true or init usuarioSeleccionado variable before page load(for example in bean constructor).

Get selected itemValue of p:selectOneMenu in listener method?

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);
}

show a panel in primefaces depending on a dropdownlist

I have a drop down list with several values and I want to show a panel only if a value has been selected
Here is the code simplified
<p:selectOneMenu id="" value="#{myBean.myElement}" >
<f:selectItem itemLabel="Choose an element itemValue="" />
<f:selectItems value="#{myBean.myElementList}" />
<p:ajax update="myPanel" listener="#{myBean.handleChange}"/>
</p:selectOneMenu>
<p:panel id="myPanel" header="My Header" style="margin-bottom:10px;" rendered="#{myBean.myElement != null}">
But this does not seem to work (it works only if I refresh manually the page).
How could you fix this ?
Since myPanel is not rendered, the component won't be in the component tree, so it cannot be updated later in the view. Use another UIContainer to wrap it like <h:panelGroup> and update this container:
<p:selectOneMenu id="" value="#{myBean.myElement}" >
<f:selectItem itemLabel="Choose an element itemValue="" />
<f:selectItems value="#{myBean.myElementList}" />
<!-- here update to foo instead of myPanel -->
<p:ajax update="foo" listener="#{myBean.handleChange}"/>
</p:selectOneMenu>
<h:panelGroup id="foo">
<p:panel id="myPanel" header="My Header" style="margin-bottom:10px;"
rendered="#{myBean.myElement != null}">
</h:panelGroup>

Resources