Setting a SelectOneListbox default selection, which is populated from a hashmap - jsf

I have a selectOneListBox that is populated with a hashmap's key as the label and the value as the value.
How do i set the first object in the list as the default selection, so it looks highlighted
<!--the list box containing the results from the search-->
<h:form id="newPointsResultList" styleClass="simpleformstyle">
<p:selectOneListbox id="selectedPoints_listbox"
value="#{mapBean.selectedPoint}"
converter="omnifaces.SelectItemsConverter"
scrollHeight="395"
styleClass="simpleformstyle10"
rendered="#{not empty mapBean.newPointsHashMap}" >
<f:selectItems value="#{mapBean.newPointsHashMap}" />
<p:ajax listener="#{mapBean.valueChanged}" update=":newPointsGrid:selectedPoint_grid" process="#this" />
<f:param name="idUser" value="#{mapBean.tipTourUser.idUser}" />
</p:selectOneListbox>
</h:form>
<br />

I set the mapBean.selectedPoint to the value of the first object in the hashmap, and then the selectOneListBox highlights the default selection

Related

Get string behind binding object

I have the following JSF construct:
<c:set var="myVar" value="#{myBean.getMyMap()}" scope="request" />
<h:form>
<p>
<h:outputText value="Output1: " />
<h:selectOneMenu value="#{myMappingsBean.data.attribute1}" binding="#{input1}" required="true">
<f:selectItems var="entry" value="#{myVar}"/>
<p:ajax update="myDropdown"/>
</h:selectOneMenu>
</p>
<p>
<h:outputText value="Output2: " />
<h:selectOneMenu id="myDropdown" value="#{myMappingsBean.data.attribute2}" binding="#{input2}" required="true" converter="javax.faces.Double">
<f:selectItems var="entry" value="#{myVar.get(input1.value)}"/>
</h:selectOneMenu>
</p>
</h:form>
Behind myVar is a map defined like that: Map<String, Collection<Double>>
The first dropdown menu shows a list of all the keys of that map (like desired), but the value behind that is the collection of values.
Here the HTML output of one option of that dropdown:
<option value="[1.0, 2.0]">SomeString</option>
My second dropdown should list the collection of double values stored in the map behind the key, that is selected by the first menu.
The problem now is, when I use value="#{myVar.get(input1.value)}" the value I get from .value is the collection and not the string/key of the map. So I never get the desired result.
How can I get the string/key behind the binded object input1? Is there something like input1.name or .toString? Is somewhere a documentary for this?
Ok, I solved it by applying the solution from here.
The first drop down has to be edited to the following:
<p>
<h:outputText value="Output1: " />
<h:selectOneMenu value="#{myMappingsBean.data.attribute1}" binding="#{input1}" required="true">
<f:selectItems var="entry" value="#{myVar.entrySet()}" itemValue="#{entry.key}" itemLabel="#{entry.key}"/>
</h:selectOneMenu>
</p>
As you can see, the entry set is created from the map and from that you use the key as value and label.
And with that the value behind input1.value now is not the collection of doubles but the key of the map.

How to drag selected item of p:selectOneListBox and drop into p:inputTextarea?

Here is my code:
<p:selectOneListbox id="columnName"
widgetVar="columnName"
value="#{dataTransformBean.column}">
<f:selectItems id="itemDrop"
value="#{dataTransformBean.columnList}"
var="item" itemValue="#{item}" />
<p:ajax update="textarea" />
</p:selectOneListbox>
<p:inputTextarea id="textarea" rows="6" cols="33" />
<p:selectOneListbox id="function"
widgetVar="function"
value="#{dataTransformBean.function}">
<f:selectItems value="#{dataTransformBean.functionValNames}" />
</p:selectOneListbox>
<p:draggable for="columnName" revert="true" helper="clone"></p:draggable>
I want to drag only selected item of <p:selectOneListBox> in <p:inputTextarea> for which for="id" which drags whole list box..How can I drag only selected item from list.
There is a "Drag & Drop - Custom" example in the showcase
http://www.primefaces.org/showcase/ui/dnd/custom.xhtml
In this example, it defines the class ".ui-treenode-leaf" to be draggable and class ".ui-datatable .droppoint" to be droppable.
(I guess) In the case of p:selectOneListBox, you can try to define "ui-selectlistbox-item ui-corner-all ui-state-highlight" as draggable. So only the selected (highlight'ed) selectlistbox item will be draggable. Of course you should also define p:inputTextarea as your droppable and create p:remoteCommands with necessary action listeners in the bean.

p:selectOneMenu, custom content and editable=true

I have the following usage of the p:selectOneMenu:
<p:selectOneMenu id="selectField"
value="#{someBean.someField}"
converter="#{selectItemConverter}" var="x" editable="true">
<f:selectItems
value="#{selectItemsBean.getSelectItems(tab, field)}" var="si"
itemLabel="#{si.label}" itemValue="#{si}" />
<p:column>
<h:outputText value="#{si.label}" />
</p:column>
<p:column>
<h:graphicImage library="images" name="noway_16x16.png"
title="#{si.disabledReason}" rendered="#{si.disabled}" />
</p:column>
<p:ajax event="change" update="#form" partialSubmit="true" process="selectField" />
</p:selectOneMenu>
As you can see, I use custom content in combination with editable=true. When I submit the form, the Converter gets the label of a selected item as the value, not the actual value. In the HTML page, the values are correct, e.g. <option value="C">C-style mounting</option>. With editable=false, the correct value (e.g. C is sent to the converter, with editable=true the converter retrieves C-style mounting.
What I want is that the user can either select one of the pre-defined items in the list and the server submits that value of the item OR the user enters something and that is submitted as value. But the current behavior is a bit strange - or am I just wanting too much?

Make a h:inputText required only when the checkbox is selected

I have a datatable with rows containing some input text fields that are required. Each row also has a check box called delete. I want to have the required = "true" only when the check box is selected. How can I achieve this?
Just let the input's required attribute check the checkbox's value.
Here's a kickoff example:
<h:form>
<h:dataTable value="#{bean.list}" var="item">
<h:column><h:selectBooleanCheckbox binding="#{checkbox}" /></h:column>
<h:column><h:inputText id="input" value="#{item.value}" required="#{checkbox.value == 'true'}" /></h:column>
<h:column><h:message for="input" /></h:column>
</h:dataTable>
<h:commandButton value="submit" action="#{bean.submit}" />
</h:form>

Dynamically changing the visibility of the JSF components

My requirement is like this: I am having a text input and whenever a value change event occurs, a select many list box has to be populated. If there is no matching records found, then a text input has to appear instead of a select many list box.
<h:column>
<h:selectManyListbox size="3" value="#{hostInfoBean.gateKeeperendPointReference}" rendered="#{hostInfoBean.selectManyRendered}" id="gateKeeperendPointReference">
<f:selectItems value="#{hostInfoBean.gateKeeperendPointReferenceItems}" />
</h:selectManyListbox>
<h:inputText id="gateKeeperendPointReferenceText" size="30" rendered="#{!hostInfoBean.selectManyRendered}">
</h:inputText>
</h:column>
Also I am using a4j for the value change listener,
<a4j:support event="onchange" reRender="hostInfo:gateKeeperendPointReference" focus="GFacPath"
ajaxSingle="true" />
'selectManyRendered' is a boolean value which I am determining in the JAVA bean. The program works only for the default value of the boolean variable. If the boolean value is changed during runtime, then the toggle between the visibility of selectManyListbox and inputText is not working. Please help to fix this. Am i missing something?
regards,
Suresh
If the "rendered" attribute resolves to false, then the component isn't in your tree and can't be found as a "rerender" target. When you have components that are rendered conditionally you want to wrap them in a component that is always available as a target, like so:
<h:inputText value="#{myBean.text}" >
<a4j:support event="onkeyup" reRender="listZone" ajaxSingle="true" />
</h:inputText>
<h:panelGroup id="listZone">
<h:selectManyListbox value="#{myBean.list}" rendered="#{myBean.renderList}" >
<f:selectItems value="#{myBean.listItems}" />
</h:selectManyListbox>
<h:inputText size="30" rendered="#{!myBean.renderList}/>
<h:panelGroup id="listZone">

Resources