<p:selectOneMenu> generates input field behind select box - jsf

I'm trying to associate a label tag with my dropdown menu to make it accessible to screen readers. I've noticed that when using p:selectOneMenu primefaces generates an input element that should not be there. When applying p:outputLabel, it appears to work fine (ie you click on the label text and focus moves to the dropdown menu). However it turns out that the focus is in fact moving to the input element and the select element is not keyboard accessible (tabindex="-1"). Here's an example taken from the primefaces website:
http://www.primefaces.org/showcase/ui/input/oneMenu.xhtml
<h:form>
<p:messages autoUpdate="true" />
<h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">
<p:outputLabel for="console" value="Basic:" />
<p:selectOneMenu id="console" value="#{selectOneMenuView.console}" style="width:125px">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItem itemLabel="Xbox One" itemValue="Xbox One" />
<f:selectItem itemLabel="PS4" itemValue="PS4" />
<f:selectItem itemLabel="Wii U" itemValue="Wii U" />
</p:selectOneMenu>
<p:outputLabel for="car" value="Grouping: " />
<p:selectOneMenu id="car" value="#{selectOneMenuView.car}">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{selectOneMenuView.cars}" />
</p:selectOneMenu>
<p:outputLabel for="city" value="Editable: " />
<p:selectOneMenu id="city" value="#{selectOneMenuView.city}" effect="fold" editable="true">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{selectOneMenuView.cities}" />
</p:selectOneMenu>
<p:outputLabel for="advanced" value="Advanced:" />
<p:selectOneMenu id="advanced" value="#{selectOneMenuView.theme}" converter="themeConverter" panelStyle="width:180px"
effect="fade" var="t" style="width:160px" filter="true" filterMatchMode="startsWith">
<f:selectItems value="#{selectOneMenuView.themes}" var="theme" itemLabel="#{theme.displayName}" itemValue="#{theme}" />
<p:column style="width:10%">
<h:outputText styleClass="ui-theme ui-theme-#{t.name}" />
</p:column>
<p:column>
<h:outputText value="#{t.displayName}" />
</p:column>
</p:selectOneMenu>
</h:panelGrid>
<p:commandButton value="Submit" update="display" oncomplete="PF('dlg').show()" icon="ui-icon-check" />
<p:dialog header="Values" modal="true" showEffect="bounce" widgetVar="dlg" resizable="false">
<p:panelGrid columns="2" id="display" columnClasses="label,value">
<h:outputText value="Basic:" />
<h:outputText value="#{selectOneMenuView.console}" />
<h:outputText value="Grouping:" />
<h:outputText value="#{selectOneMenuView.car}" />
<h:outputText value="Editable" />
<h:outputText value="#{selectOneMenuView.city}" />
<h:outputText value="Advanced:" />
<h:outputText value="#{selectOneMenuView.theme.displayName}" />
</p:panelGrid>
</p:dialog>
When you look at the page with just the html loaded, you can see each p:selectOneMenu generates an input as well as a select.
<td>
<label id="j_idt88:j_idt91" class="ui-outputlabel ui-widget" for="j_idt88:console_focus">Basic:</label>
</td>
<div id="j_idt88:console" class="ui-selectonemenu ui-widget ui-state-default ui-corner-all" style="width:125px">
<div class="ui-helper-hidden-accessible">
<input id="j_idt88:console_focus" name="j_idt88:console_focus" type="text" autocomplete="off">
</div>
<div class="ui-helper-hidden-accessible">
<select id="j_idt88:console_input" name="j_idt88:console_input" tabindex="-1" data-p-label="Basic" data-p-hl="onemenu">
<option value="">Select One</option><option value="Xbox One">Xbox One</option>
<option value="PS4">PS4</option>
<option value="Wii U">Wii U</option>
</select>
</div>
<label id="j_idt88:console_label" class="ui-selectonemenu-label ui-inputfield ui-corner-all">Select One</label>
<div class="ui-selectonemenu-trigger ui-state-default ui-corner-right">
<span class="ui-icon ui-icon-triangle-1-s ui-c"></span>
</div>
</div>
I'm looking to have the dropdown menu to behave like the JSF one h:selectOneMenu (when it comes to accessibility). Specific to the example, I want the "Basic:" label associated with it's dropdown menu so that when you click "Basic" focus moves to the dropdown menu. I also want a screen reader to actually treat it as a select element rather than an input element. So far I can't find a way to do this because of that input element that's generated. I'm currently using Primefaces 5.2. Does anyone know a way to prevent this input element from being generated?

I gave up on the p:selectOneMenu for two reasons : it is not accessible and it is a pain to use on a mobile device because it doesn't bring the native control picker (the "spinning wheel" on the iPad for instance).
Instead I use the h:selectOneMenu customized with something like :
<h:selectOneMenu
styleClass="ui-inputfield ui-widget ui-state-default ui-corner-all #{component.valid ? '' : 'ui-state-error'}"
onmouseover="$(this).addClass('ui-state-active')"
onmouseout="$(this).removeClass('ui-state-active')"
onfocus="$(this).addClass('ui-state-active ui-state-focus')"
onblur="$(this).removeClass('ui-state-active ui-state-focus')"/>
It doesn't look that bad, it is accessible and it works well with mobile device.

Related

primefaces selectOneMenu not working in Dialog

I have this dialog:
The code for this dialog:
<p:dialog header="Détails du produit" showEffect="fade" modal="true"
widgetVar="manageProductDialog" responsive="true">
<p:outputPanel id="manage-product-content" class="ui-fluid">
<p:outputPanel rendered="#{not empty productMB.selectedProduct}">
<p:graphicImage
name="images/product/#{productMB.selectedProduct.img}"
library="demo" styleClass="product-image"
rendered="#{not empty productMB.selectedProduct.img}" />
<div class="p-field">
<p:outputLabel for="name">Nom</p:outputLabel>
<p:inputText id="name" value="#{productMB.selectedProduct.name}"
required="true" />
</div>
<div class="p-field">
<p:outputLabel for="category">Catégorie</p:outputLabel>
<p:selectOneMenu id="category"
value="#{productMB.selectedProduct.category}"
layout="responsive">
<f:selectItem itemLabel="Select One" itemValue="#{null}" noSelectionOption="true" />
<f:selectItems value="#{categoryMB.categories}" var="cat" itemValue="#{cat}" itemLabel="#{cat.name}" />
</p:selectOneMenu>
</div>
<div class="p-formgrid p-grid">
<div class="p-field p-col">
<p:outputLabel for="price">Prix</p:outputLabel>
<p:inputNumber id="price"
value="#{productMB.selectedProduct.price}" symbol=" DH"
symbolPosition="s" decimalSeparator="." thousandSeparator="," />
</div>
</div>
</p:outputPanel>
</p:outputPanel>
<f:facet name="footer">
<p:commandButton value="Enregister" icon="pi pi-check"
actionListener="#{productMB.saveProduct}"
update="manage-product-content"
process="manage-product-content #this" />
<p:commandButton value="Annuler" icon="pi pi-times"
onclick="PF('manageProductDialog').hide()"
class="ui-button-secondary" />
</f:facet>
</p:dialog>
The problem I have is that when I click on Enregister button the p:selectOneMenu is not validated even that a value was selected.
How can I solve this issue ?

passing the selected object from list of objects that each one has its own button

I’m working on a web-app that displays list of objects, each object has its own modal when clicked inside the modal there are two dropdown menus and a button that is supposed to call a method add in a bean, to perform an action and add the selected object while taking three values, selected object, selected two items from the two drop downs and spinner’s value.
<ui:repeat value="#{ViewTeacher.teacherList}" var="teacher"
varStatus="status">
<h:panelGroup layout="block" styleClass="name">
<p>#{teacher.name}</p>
</h:panelGroup>
<br />
<br />
<a onclick="viewModal(#{status.index})" class="btn btn-success">view</a>
<h:panelGroup id="modal" layout="block" class="modal"
style="max-width: 44em;" tabindex="1">
<h:panelGroup class="modal-content" layout="block">
<span class="close" onclick="modClose()">×</span>
<h:outputText styleClass="description" value="#{teacher.name}" />
<br />
<br />
<br />
students
<h:selectOneMenu value="ok" class="form-control">
<f:selectItems value="#{teacher.students}" var="s"
itemValue="#{s.id}" itemLabel="#{s.name}" />
</h:selectOneMenu>
classes
<h:selectOneMenu value="ok" class="form-control">
<f:selectItems value="#{teacher.classes}" var="c"
itemValue="#{c.id}" itemLabel="#{c.name}" />
</h:selectOneMenu>
<br />
<h:panelGroup layout="block" styleClass="form-group">
<p:spinner min="1" max="10" />
</h:panelGroup>
<h:commandButton id="submitButton" styleClass="btn btn-success" value="ADD">
<!—-this is the button that is supposed to take the values
</h:commandButton>
<a onclick="modClose()" class="btn btn-success">CANCEL</a>
</h:panelGroup>
</h:panelGroup>
</ui:repeat>
I tried :
<f:param name="name1" value="China" />
But the fact that param’s name will get repeated, will make it the same all the time and might be set to the latest object retrieved,
Any recommendation?
UPDATE:
<p:commandLink styleClass="btn btn-success" process="#this"
onclick="viewModal(#{status.index});">
<h:outputText value="ADD" />
<f:setPropertyActionListener
value="#{teacher.id}" target="#{ViewTeacher.selectedTeacher}" />
</p:commandLink>
Made this change to pass the teacher's id but it's still null in the bean
First of all, having a separate modal for each object isn't a good idea. You can achieve (what you are trying) by:
1). Adding a property (with its getter and setter) in bean for selected object.
2). Place single modal (<p:dialog with id="modal" and widgetVar="modal" attributes) outside ui:repeat to display data from selected object.
3). Set selected object on clicking view link, update and display modal, using:
<p:commandLink styleClass="btn btn-success" process="#this" update="modal"
onclick="PF('modal').show();">
<h:outputText value="View" />
<f:setPropertyActionListener
value="#{teacher}" target="#{ViewTeacher.selectedTeacher}" />
</p:commandLink>
Here, f:setPropertyActionListener will be setting your selected modal in bean and thus it will become available in your action listener.
Update: Change the code as following:
<ui:repeat value="#{ViewTeacher.teacherList}" var="teacher" varStatus="status">
<h:panelGroup layout="block" styleClass="name">
<p>#{teacher.name}</p>
</h:panelGroup>
<p:commandLink styleClass="btn btn-success" process="#this" update="modal"
oncomplete="PF('modal').show();">
<h:outputText value="view" />
<f:setPropertyActionListener
value="#{teacher}" target="#{ViewTeacher.selectedTeacher}" />
</p:commandLink>
</ui:repeat>
<p:dialog id="modal" widgetVar="modal" header="Edit Teacher" modal="true"
showEffect="fade" hideEffect="fade" resizable="false" closeOnEscape="true"
styleClass="teachers-dialog">
<h:outputText styleClass="description" value="#{ViewTeacher.selectedTeacher.name}" />
<br />
<br />
<br />
students
<h:selectOneMenu value="#{ViewTeacher.selectedStudentId}" class="form-control">
<f:selectItems value="#{ViewTeacher.selectedTeacher.students}"
var="s" itemValue="#{s.id}" itemLabel="#{s.name}" />
</h:selectOneMenu>
classes
<h:selectOneMenu value="#{ViewTeacher.selectedClassId}" class="form-control">
<f:selectItems value="#{ViewTeacher.selectedTeacher.classes}"
var="c" itemValue="#{c.id}" itemLabel="#{c.name}" />
</h:selectOneMenu>
<br />
<h:panelGroup layout="block" styleClass="form-group">
<p:spinner value="#{ViewTeacher.spinnerValue}" min="1" max="10" />
</h:panelGroup>
<h:commandButton id="submitButton" styleClass="btn btn-success" value="ADD">
</h:commandButton>
</p:dialog>
Where selectedTeacher, selectedStudentId, selectedClassId, spinnerValue are properties from your ViewTeacher bean.

h:inputText between p:selectManyCheckbox items

Is it possible, on some way, to display an h:inputText between the selectItems of a p:selectManyCheckbox?
What I'd like to have is:
checkbox1
checkbox2
inputTextIfCheckbox2IsMarked (disabled if not marked..)
checkbox 3
checkbox 4
I know how to do the enable/disable etc of the checkbox, but outside of the selectItems. I'd like to put it between them.
Is that possible on SOME way?
My solution is (with the inputText not between the selectItems):
<h:panelGrid columns="2">
<p:selectManyCheckbox converter="otherAdmissionReasonConverter"
id="otherAdmissionReasonCheckbox"
value="#{potentialDonorFileBackingBean.potentialDonorFile.generalPatientInformation.otherAdmissionReasons}"
layout="grid" columns="1">
<f:selectItems value="#{patientInformationBackingBean.otherAdmissionReasons}"
var="otherAdmissionReason"
itemValue="#{otherAdmissionReason}"
itemLabel="#{msgs['ar.'.concat(otherAdmissionReason)]}"/>
<p:ajax update="customAdmissionReasonGrid"/>
</p:selectManyCheckbox>
<h:panelGroup id="customAdmissionReasonGrid"
styleClass="customAdmissionReason">
<p:inputText id="customAdmissionReason"
disabled="#{!potentialDonorFileBackingBean.potentialDonorFile.generalPatientInformation.otherAdmissionReasonChecked()}"
value="#{potentialDonorFileBackingBean.potentialDonorFile.generalPatientInformation.customAdmissionReason}"/>
</h:panelGroup>
</h:panelGrid>
PrimeFaces: 5.2
For PrimeFaces < 5.2.3 you can't. For PrimeFaces >=5.2.3 you can use a custom layout like this (© PrimeFaces)
<h3>Custom Layout (since v5.2.3)</h3>
<p:outputPanel id="customPanel" style="margin-bottom:20px">
<p:selectManyCheckbox id="custom" value="#{checkboxView.selectedConsoles2}" layout="custom">
<f:selectItem itemLabel="Xbox SixSixSix" itemValue="Xbox SixSixSix" />
<f:selectItem itemLabel="PS9" itemValue="PS9" />
<f:selectItem itemLabel="Wii Them" itemValue="Wii Them" />
</p:selectManyCheckbox>
<div class="ui-grid ui-grid-responsive">
<div class="ui-grid-row">
<div class="ui-grid-col-4">
<h:outputLabel for="opt1" value="Xbox SixSixSix" style="display:block"/>
<p:checkbox id="opt1" for="custom" itemIndex="0" />
</div>
Some Custom Text between item 1 and 2
<div class="ui-grid-col-4">
<h:outputLabel for="opt2" value="PS9" style="display:block"/>
<p:checkbox id="opt2" for="custom" itemIndex="1" />
</div>
<div class="ui-grid-col-4">
<h:outputLabel for="opt3" value="Wii Them" style="display:block"/>
<p:checkbox id="opt3" for="custom" itemIndex="2" />
</div>
</div>
</div>
</p:outputPanel>

Primefaces p:focus not working

I need focus specify Id(i.e commandbutton,selectonemenu and text) is not working.I try this below code:
<h:form id="mainForm">
<p:inputText id="name"/>
<p:inputText id="Age"/>
<p:selectOneRadio id="Gender" value="#{employee.gender}" layout="custom">
<f:selectItem itemLabel="Male" itemValue="Male" />
<f:selectItem itemLabel="Female" itemValue="Female" />
</p:selectOneRadio>
<p:commandButton id="clear" value="Press" action="{employee.saveAction}">
<p:focus for="Gender"/>
</p:commandButton>
<p:commandButton id="clear" value="clear"/>
</h:form>
I try focus radio button or id="clear" commandbutton or text button is not focus.
Note: I tried context="specifyid" replace for it also not working.
I don't know why you put <p:focus> inside <p:commandButton>. Maybe this is causing problems. According to the docs you can put the tag direct into a <h:form> and set focus on your <p:selectOneRadio>:
<h:form id="mainForm">
<p:focus for="Gender" />
<p:inputText id="name" />
<p:inputText id="Age" />
<p:selectOneRadio id="Gender" value="#{employee.gender}" layout="custom">
<f:selectItem itemLabel="Male" itemValue="Male" />
<f:selectItem itemLabel="Female" itemValue="Female" />
</p:selectOneRadio>
<p:commandButton id="clear" value="Press" action="{employee.saveAction}" />
<p:commandButton id="clear" value="clear" />
</h:form>

Editable p:selectOneMenu

How to set placeholder in editable <p:selectOneMenu>?
In editable <p:selectOneMenu> of PrimeFaces, initial UI looks as below:
http://www.primefaces.org/showcase/ui/input/oneMenu.xhtml
(see the editable menu)
Is there a way to place a placeholder which says text like "Click to enter manual input" or to make user aware that this dropdown is editable.
Find the code snippet below for reference.
<div class="setupDataSection">
<div class="pageCenter pageCenterShortened">
<div style="float: left;" class="sectionTitle sectionTitleInline">
<h:outputLabel value="Tenant "/>
</div>
<div style="float: left; padding-left: 40px;">
<p:selectOneMenu id="tenant"
styleClass="ddlPOS"
style="width: 190px;"
label="Tenant"
value="#{onBoardingAction.selTenant}"
editable="true">
<p:watermark for="tenant"
value="Search with a keyword"
id="watermark" />
<f:selectItem itemLabel="Add new tenant" />
<f:selectItems value="#{onBoardingAction.tenants}" />
<p:ajax event="change"
execute="#this"
update="tenant region POS"
listener="#{onBoardingAction.settingRegions()}" />
</p:selectOneMenu>
</div>
</div>
</div>
Primefaces call it watermark SEE, If your SelectOneMenu is empty this water martk's text automatically appears
<p:watermark for="fileSize" value="Search with a keyword" id="watermark" />
Update
I have tested your code and It work fine for me if you are using template maybe your CSS overriding component try this, I just put p:watermark outside the component code:
<f:selectItem itemLabel="Add new tenant" />
<f:selectItems value="#{onBoardingAction.tenants}" />
<p:ajax event="change"
execute="#this"
update="tenant region POS""
listener="#{onBoardingAction.settingRegions()}" />
</p:selectOneMenu>
<p:watermark for="tenant"
value="Search with a keyword"
id="watermark" />
HOPE RESOLVED :)

Resources