How do I add a selected value to ice:selectOneMenu? - jsf

I'm facing a problem with the ice:selectOneMenu component in Icefaces..
In more details :
When I fill the SelecItems arrayList I set an empty selcetItem at the first index in the arrayList, like that : mainClassificationsSI.add(new SelectItem(""));, and in My page I have the following :
<ice:selectOneMenu partialSubmit="true" binding="#
{editOutgoingBean.mainClassificationSelect}" id="mBookClass" value="#
{editOutgoingBean.outgoing.main_Classification}" valueChangeListener="#
{editOutgoingBean.mainClassificationChanged}">
<f:selectItems value="#{editOutgoingBean.mainClassificationsSI}"/>
</ice:selectOneMenu>
First of all, I'm sure that the: editOutgoingBean.outgoing.main_Classification has value which is one of the values of selectItems in the SelectItems arrayList, but when the page is displayed, the Select tag select the empty value and it didn't select the value of the main_Classification I set before...
When I remove the first Empty SelectItem from the ArrayList, the Select tag will select the value that I set for main_Classification. Please help me find out what to do...

try this ,may help you.
<ice:selectOneMenu partialSubmit="true" binding="#
{editOutgoingBean.mainClassificationSelect}" id="mBookClass" value="#
{editOutgoingBean.outgoing.main_Classification}" valueChangeListener="#
{editOutgoingBean.mainClassificationChanged}">
<f:selectItem itemLabel="Select" itemValue="-1" />
<f:selectItems value="#{editOutgoingBean.mainClassificationsSI}"/>
</ice:selectOneMenu>`
and don't the black select item from the bean as you were doing

Related

<p:selectOneMenu> with bold labels for some items

I'm trying to have a Primefaces "SelectOneMenu" with items that can either have a "normal" label or a "bold" label. I tried 2 different variants, but both don't work as expected.
Variant 1:
<p:selectOneMenu id="menu" value="#{selected}">
<f:selectItems value="#{mySelectItems}"/>
</p:selectOneMenu>
public List<SelectItem> getMySelectItems() {
List<SelectItem> list = new ArrayList<SelectItem>();
for (...) {
SelectItem item = new SelectItem(value, label);
item.setEscape(false);
if (...)
item.setLabel("<b>" + item.getLabel() + "</b>");
list.add(item);
}
return list;
}
This way I can have some of the items in the menu with a bold label and some with a normal label. The problem: The field that is showing the current selection is displaying the text as "< b >...< /b >". The text doesn't seem to be escaped, but it seems that it just can't be displayed as bold, because it's inside an HTML label. I don't know how I could get rid of the "< b >" and "< /b >" in that label, though.
Variant 2:
<p:selectOneMenu id="menu" value="#{selected}" var="myClass">
<f:selectItems value="#{myClasses}"/>
<p:column>
<h:outputText value="#{myClass.name}" styleClass="bold"/>
</p:column>
</p:selectOneMenu>
public List<MyClass> getMyClasses() {
List<MyClass> list = new ArrayList<MyClass>();
...
return list;
}
With this variant I can also have bold labels for items (currently all of them) in the menu. The problem is: The text showing the current selection doesn't show "myClass.getName()", but instead "myClass.toString(). The labels in the menu are correct. Is there a way to fix this?
If I write...
<f:selectItems value="#{myClasses}" var="myVar" itemValue="#{myVar.value}" itemLabel="#{myVar.name}"/>
instead, then it doesn't display the items in the menu as bold anymore, but it's using getName() instead of toString() in the field of the current selection.
Does anyone know how to get my ideas to work the way I want them to work or maybe have a better idea on how to solve this?
Thanks in advance!
In Variant 2 you need to provide converter for MyClass by implementing javax.faces.convert.Converter. Converter needs only for selection (it generates string representation of the object (unique id of instance) and converts it back to object after processing). And for correct representation of selected value you need add this attributes to selectItems: <f:selectItems value="#{myClasses}" var="myVar" itemValue="#{myVar}" itemLabel="#{myVar.name}"/>.
<h:outputText styleClass="bold"... will effect only for list of selectItems. To change style of selected item you need to add styleClass to p:selectOneMenu. In your case:
<p:selectOneMenu id="menu" value="#{view.selection}" converter="#{view.myClassConverter}"
var="v" style="#{(view.selection.name eq 'bold-name') ? 'font-style: bold' : null}">
<f:selectItems value="#{view.myClasses}" var="mc"
itemLabel="#{mc.name}" itemValue="#{mc}"/>
<p:column>
<h:outputText value="#{v.name}" style="#{(v.name eq 'bold-name') ? 'font-style: bold' : null}"/>
</p:column>
<p:ajax event="change" update="menu" process="#this"/>
.

Primefaces dataTable filter selectOneMenu not working

I am using PrimeFaces 5.1, In my project dataTable to filter used.In text filter is work fine but dropdown filter is not working properly (i.e) In dropdown I show department,First time I choose any value from dropdown is work fine anothertime I choose dropdown It not return any value show in dataTable.I choose select one first value from dropdown also throw null pointer exception.
<p:dataTable id="datalist" widgetVar="datalist" var="user" value=#{beanList.userList}>
<p:column headerText="Department" filterBy="#{user.deptname}"
filterMatchMode="exact" >
<f:facet name="filter">
<p:selectOneMenu onchange="PF('datalist').filter()">
<f:selectItem itemLabel="ALL" itemValue="#{null}"
noSelectionOption="true" />
<f:selectItems value="#{datalist.deptList}" />
</p:selectOneMenu>
</f:facet>
<h:outputText value="#{user.depatname}" />
</p:column>
</p:dataTable>
My doubt is default value ALL click and second time select any value return null or no data show in dataTable.
Since I don't know the scope of your Managed Bean: Try a scope longer than request (see PrimeFaces 5.1 User Documentation), and provide a value "filteredValue" for your table, like this:
<p:dataTable id="datalist" widgetVar="datalist" var="user"
value="#{beanList.userList}" filteredValue="#{beanList.filteredUserList}">
With that, you make sure to keep your filtered table/list in a field in your managed bean and the contents won't get lost.
Also, make sure your Managed Bean class is serializable (See this stackoverflow post)

h:selectmanyListbox make "choose" option unselectable when at least one option is selected

Is it possible to have in <h:selectManyListbox> a default option like "--choose--" which can be selected when no option is selected. When the some value is chosen, then it must be unselectable.
<h:selectManyListbox value="#{bean.value}"
class="form-control">
<f:selectItems value="#{bean.dropdownValues}" var="value" itemLabel="#{value}" itemValue="#{value}"/>
</h:selectManyListbox>
Just add it as another <f:selectItem> and ask assistance of a bit of JavaScript to disable it when any value is selected during the change event.
<h:selectManyListbox ... onchange="options[0].disabled=!!value">
<f:selectItem itemLabel="--choose--" itemValue="#{null}" />
<f:selectItems ... />
</h:selectManyListbox>
The options[0] refers to the first option of the selection element. The !!value basically converts the selected item value to a boolean (which will be true when it's not empty/null), suitable for disabled attribute.
See also:
Best way to add a "nothing selected" option to a selectOneMenu in JSF

h:selectOneMenu in p:dataTable doesn't submit its value

I have a question about selectOneMenu and settting the values. I have an Object SampleDesc that has and ID, Text, and a List<SampleDescValues>. For each datatable row the Text is the output label and the select one menu values are the List<SampleDescValues>.
XHTML:
<h:panelGroup id="tables">
<p:dataTable resizableColumns="true"
var="sampleDesc" id="SampleDescTable" rowIndexVar="rowIndex"
value="#{sampleBean.sampleDescList.list}"
rendered="#{sampleBean.sampleDescList.list.size() gt 0}">
<p:column>
<h:outputLabel value="#{sampleDesc.sampleDescText}"/>
</p:column>
<p:column>
<h:selectOneMenu required="#{sampleBean.sampleDescList.list.size() gt 0}" converter="#{sampleDescValueConverter}"
id="SampleDescValue" value="#{sampleBean.selectedSampleDescList.get(rowIndex)}">
<f:selectItem itemLabel="Select One" itemValue="#{null}"/>
<f:selectItems value="#{sampleDesc.sampleDescValues}" var="sdv"
itemLabel="#{sdv.sampleDescValuesText}" itemValue="#{sdv}" />
</h:selectOneMenu>
</p:column>
</p:dataTable>
</h:panelGroup>
I have the converter setup and it works because ive set it to a single SampleDescValue and it set the value.
The problem is when i try and populate the form with a Sample from the database it can only set one of the dropdowns when there could be an infinite number of selectonemenu's
I set the value selected to private List<SampleDescValue> selectedSampleDescList;
When i try and submit it does nothing, it works when the datatable is not rendered.
Your menu value is wrong:
<h:selectOneMenu value="#{sampleBean.selectedSampleDescList.get(rowIndex)}">
It's not possible to perform a set operation on this EL expression.
Use the brace notation instead:
<h:selectOneMenu value="#{sampleBean.selectedSampleDescList[rowIndex]}">
Note that this expects a non-null selectedSampleDescList. So make sure that you've already properly initialized it with a new ArrayList<>() beforehand. EL won't do that for you. It will only set the list items using List#add(index, object) method.
See also:
Our EL wiki page
Unrelated to the concrete problem, this expression
#{sampleBean.sampleDescList.list.size() gt 0}
can be simplified as follows
#{not empty sampleBean.sampleDescList.list}
And this is unnecessary in the required attribute of the <h:selectOneMenu> as it would always evaluate true at that point. Just use required="true" directly instead.

How to call action when select noSelectionLabel?

I have this code above which works perfectly when i select some of the items on him... The a4j:support works fine and rerender my another field correctly...
The problem is if i choose one item, and then i back to "noSelectionLabel"...
When i do this for some reason my a4j:support dont work, i dont get into my method "setarFormulario" and i dont rerender my another field...
<s:decorate template="layout/form.xhtml">
<ui:define name="label">Evento:</ui:define>
<h:selectOneMenu value="#{home.instance.evento}" required="true">
<s:selectItems value="#{eventoService.obterTodos()}" var="evento" label="#{messages[evento.nome]}" noSelectionLabel="#{messages['br.com.message.NoSelection']}" />
<s:convertEntity />
<a4j:support event="onchange" action="#{home.setarFormulario}" reRender="camposFormulario" ajaxSingle="true" />
</h:selectOneMenu>
</s:decorate>
How can i get into my method even if i select the noSelectionLabel? Then my home.instance.evento must be null.. or something like this...
Yor field h:selectOneMenu is required then selecting noSelectionLabel value will cause a validation error and if you had validation error, then the action="#{home.setarFormulario}" would never be called.
As a workaround you can set to true the attribute hideNoSelectionLabel for your s:selectItems then the noSelectionLabel will be hidden when a value is selected
<h:message for="id of the selectonemenu component " ></h:message>
or
remove the required =true from the selectonemenu tag

Resources