I am using prime faces in my project. I am generating a pull down by using p:ajax and its working fine for me. But i have notice a problem that is if i select ---select one-- my pull down which i have generated by p:ajax is not resetting to default .
My code is:
<p:selectOneMenu id="propExam" value="#{rollNoBean.examination}"
converter="omnifaces.SelectItemsConverter" editable="false"
required="true" label="Examination Applied For" styleClass="dropdownWidth">
<f:selectItem itemLabel="---Select One---" itemValue="0" />
<f:selectItems value="#{rollNoBean.testExamNames}" var="test" itemLabel="#{test.name}" itemValue="#{test}" noSelectionOption="true" />
<p:ajax listener="#{rollNoBean.readFilterByExamination(rollNoBean.examination.id)}"
update="degreeList centerId" />
</p:selectOneMenu>
<h:outputText value="Select Filter Category :" />
<p:selectOneMenu id="degreeList"
value="#{rollNoBean.filter}" editable="false"
converter="omnifaces.SelectItemsConverter" required="true"
label="Please Select degree" styleClass="dropdownWidth">
<f:selectItem itemValue="" itemLabel="---Select One---" />
<f:selectItems value="#{rollNoBean.degreeNames}"
var="degree" itemLabel="#{degree.name}" itemValue="#{degree}" />
</p:selectOneMenu>
and my bean code is:
public List<Filter> readFilterByExamination(int id)
{
if(id!=0)
{
return degreeNames=examinationDetailsService.readFilterByExamination(id);
}
return degreeNames=new ArrayList<Filter>();
}
Please give suggetion
Related
I have a select and I want that when the user doesn't select anything it prints an error. So I use validator and my xhtml code is:
<p:selectOneMenu id="students"
value="#{studentsBean.selectedStudent}"
converter="studentsConverter" effect="fold">
<f:selectItem itemLabel="Select..." itemValue="" />
<f:selectItems value="#{studentsBean.studentsList}"
var="student" itemValue="#{student}"
itemLabel="#{student.name}" />
<p:ajax event="change" update="students" process="#this" />
<f:validator validatorId="studentNotNull" />
<f:ajax execute="#this" render="studentPanel"/>
</p:selectOneMenu>
<h:panelGroup id="studentPane">
<h:message for="students" style="color:red" />
</h:panelGroup>
The problem is that if I don't select anythin it print me "The students is not selected" while I choice an student the label it doesn't disappear.
the validatior works and I don't think I must write the code.
Anyone can help?
Can you try this
<p:selectOneMenu id="students"
value="#{studentsBean.selectedStudent}"
converter="studentsConverter" effect="fold"
required="true" requiredMessage="Student is required">
<f:selectItem itemLabel="Select..." noSelectionOption="true"/>
<f:selectItems value="#{studentsBean.studentsList}"
var="student"
itemValue="#{student}"
itemLabel="#{student.name}" />
</p:selectOneMenu>
Add in your form
<p:growl id="msgs" showDetail="true" life="3000"/>
and of course, update msgs in form submit
I have a simple JSF form which has three dropdowns,
here is the code:
<h:form>
<p:panel header="Select country" style="margin-bottom:10px;">
<h:panelGrid columns="2" cellpadding="5">
<p:outputLabel for="country" value="Country: " />
<p:selectOneMenu id="country" value="#{phaseOneController.country}" style="width:150px">
<p:ajax listener="#{phaseOneController.onCountryChange}" update="province" />
<f:selectItem itemLabel="Select Country" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{phaseOneController.countries}" />
</p:selectOneMenu>
<p:outputLabel for="province" value="Province: " />
<p:selectOneMenu id="province" value="#{phaseOneController.province}" style="width:150px">
<p:ajax listener="#{phaseOneController.onProvinceChange}" update="city" />
<f:selectItem itemLabel="Select Province" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{phaseOneController.provinces}" />
</p:selectOneMenu>
<p:outputLabel for="city" value="City: " />
<p:selectOneMenu id="city" value="#{phaseOneController.city}" style="width:150px">
<f:selectItem itemLabel="Select City" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{phaseOneController.cities}" />
</p:selectOneMenu>
</h:panelGrid>
<p:separator />
<p:commandButton value="Select" actionListener="#{phaseOneController.submit}" icon="ui-icon-check">
</p:commandButton>
</p:panel>
</h:form>
Now when I select a country, the onCountryChange gets called but when I update the province, the onProvinceChange doesn't get called at all!! It is surprising how one would work and other wouldn't
Here is the piece of code from the controller:
public void onCountryChange() {
System.out.println("On country change called");
}
public void onProvinceChange() {
System.out.println("On province change called");
}
For shortening the question I have replaced the code in the listener methods.
Answer: For anyone who faces this issue, using HashMap to populate the dropdown solved the issue. I am not sure why list is giving so much trouble.
Try to add event="change"...
<p:selectOneMenu id="country" value="#{phaseOneController.country}" style="width:150px">
<p:ajax event="change" listener="#{phaseOneController.onCountryChange}" update="subCategory" />
<f:selectItem itemLabel="Select Country" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{phaseOneController.countries}" />
</p:selectOneMenu>
<p:selectOneMenu id="province" value="#{phaseOneController.province}" style="width:150px">
<p:ajax event="change" listener="#{phaseOneController.onProvinceChange}" update="city" />
<f:selectItem itemLabel="Select Province" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{phaseOneController.provinces}" />
</p:selectOneMenu>
Placing the bean in the view scope by #ViewScoped and Custom Converters to each f:selectItems should solve this problem.
Please See:
Cascading p:selectOneMenu model value not set
I want to display and hide a <h:panelGrid> on the basis of the value selected in <h:selectOneMenu>.
The code is:
<p:dialog id="logClassDlg" header="Log Class Teached" widgetVar="logClass" modal="true" >
<h:form>
<p:growl id="growl" showDetail="true" />
<h:panelGrid id="logClassPanelGrd1" columns="2">
<h:outputLabel for="class" value="Class: " />
<h:selectOneMenu id="class" value="#{logClassBean.classLog.classTitle}">
<f:selectItem itemValue="Reading" itemLabel="Reading" />
<f:selectItem itemValue="Memorization" itemLabel="Memorization" />
<f:selectItem itemValue="Translation" itemLabel="Translation" />
<f:selectItem itemValue="Language" itemLabel="Language" />
<f:selectItem itemValue="Grammer" itemLabel="Grammer" />
<f:ajax render="logClassPanelGrdBook" />
</h:selectOneMenu>
</h:panelGrid>
<h:panelGrid id="logClassPanelGrdBook" columns="2" rendered="#{(logClassBean.classLog.classTitle ne 'Language') and (logClassBean.classLog.classTitle ne 'Grammar')}">
<p:outputLabel value="Book" />
<h:selectOneMenu id="book" value="#{logClassBean.classLog.classStatus}">
<f:selectItem itemValue="first" itemLabel="first" />
<f:selectItem itemValue="second" itemLabel="second" />
<f:selectItem itemValue="third" itemLabel="third" />
<f:selectItem itemValue="fourth" itemLabel="fourth" />
<f:selectItem itemValue="fifth" itemLabel="fifth" />
</h:selectOneMenu>
</h:panelGrid>
As you can see I want to display the panel grid logClassPanelGrdBook only on when the selected values are not Language and Grammar i.e
rendered="#{(logClassBean.classLog.classTitle ne 'Language') and (logClassBean.classLog.classTitle ne 'Grammar')}"
But the panel grid is displayed irrespective of the values selected, it should hide when the other values (Reading, Memorization and Translation) are selected.
Any help/pointers would be appreciated. Thank you.
The part <f:ajax render="logClassPanelGrdBook" /> won't update and recheck the rendered function of the panelGrid this way. You will need to update its parent element instead.
For example, you can wrap it with <h:panelGroup>:
<h:panelGrid id="logClassPanelGrd1" columns="2">
<h:outputLabel for="class" value="Class: " />
<h:selectOneMenu id="class" value="#{logClassBean.classLog.classTitle}">
<f:selectItem itemValue="Reading" itemLabel="Reading" />
<f:selectItem itemValue="Memorization" itemLabel="Memorization" />
<f:selectItem itemValue="Translation" itemLabel="Translation" />
<f:selectItem itemValue="Language" itemLabel="Language" />
<f:selectItem itemValue="Grammar" itemLabel="Grammar" />
<f:ajax render="logClassPanelGrdBook" />
</h:selectOneMenu>
</h:panelGrid>
<h:panelGroup id="logClassPanelGrdBook">
<h:panelGrid columns="2" rendered="#{(logClassBean.classLog.classTitle ne 'Language') and (logClassBean.classLog.classTitle ne 'Grammar')}">
<p:outputLabel value="Book" />
<h:selectOneMenu id="book" value="#{logClassBean.classLog.classStatus}">
<f:selectItem itemValue="first" itemLabel="first" />
<f:selectItem itemValue="second" itemLabel="second" />
<f:selectItem itemValue="third" itemLabel="third" />
<f:selectItem itemValue="fourth" itemLabel="fourth" />
<f:selectItem itemValue="fifth" itemLabel="fifth" />
</h:selectOneMenu>
</h:panelGrid>
<h:panelGroup>
I also changed select item value from Grammer to Grammar because it didn't match the value in rendered attribute.
If this doesn't work, you should also check if bean value is properly set when you click <h:selectOneMenu>.
I want to disabled department field based on employeeno.But,I want department value in bean.When,I using disabled value getting null in bean but without using disabled no problem...
<h:outputText value="Employee No"/>
<p:selectOneMenu value="#{salarypromotionBean.salarypromotiondto.employeeNo}" id="emp" style="width:163px;">
<f:selectItem itemLabel="select" itemValue="0" />
<f:selectItems value="#{salarypromotionBean.empid}"/>
<p:ajax event="change" listener="#{salarypromotionBean.currentSalaryAmount}" update="salaryIncrement,empN,empDoj,basic,da,empNewDesig,commonSalaryIncrement"/>
</p:selectOneMenu>
<h:outputText value="Department" />
<p:selectOneMenu value="#{salarypromotionBean.salarypromotiondto.department}" id="empDept" disabled="#{salarypromotionBean.designationDiaspleValue}"
style="width:163px;">
<f:selectItems value="#{salarypromotionBean.deptname}" />
</p:selectOneMenu>
You can do this.
<p:selectOneMenu value="#{salarypromotionBean.salarypromotiondto.department}" id="empDept" rendered="#{!salarypromotionBean.designationDiaspleValue}"
style="width:163px;">
<f:selectItems value="#{salarypromotionBean.deptname}" />
</p:selectOneMenu>
<p:inputText value="#{salarypromotionBean.salarypromotiondto.department}" readonly="true" rendered="#{salarypromotionBean.designationDiaspleValue}"/>
I have two checkboxes.
<h:panelGrid id="userActivationGridcheckbox">
<p:selectManyCheckbox value="#{user.activationModecheckbox}" layout="pageDirection">
<f:selectItem itemLabel="Account Expiration(in days)" itemValue="byEmailcheckbox" />
<f:selectItem itemLabel="Reset User Password" itemValue="byAdmincheckbox" />
<f:ajax event="change" render="userActivationGridcheckbox" />
</p:selectManyCheckbox>
<h:panelGrid id="manualActivationGridcheckbox" rendered="#{user.manualActivationcheckbox}" columns="2">
<p:selectOneRadio value="#{user.activationModecheckbox}" layout="pageDirection">
<f:selectItem itemLabel="label1" itemValue="value1" />
<f:selectItem itemLabel="label2" itemValue="value2" />
<h:outputLabel>label</h:outputLabel>
<h:inputText value="" />
</p:selectOneRadio>
</h:panelGrid>
</h:panelGrid>
When the first checkbox is checked, then 2 more radio buttons should show up. When the second checkbox is not checked, then the radio buttons should not show up. How can I achieve this?
You need to decide in some listener method whether or not to show the radio group to the user, for example, by keeping a boolean in a backing bean. Then just render the component via AJAX.
Example usage:
The view:
<h:form>
<p:selectManyCheckbox id="check" value="#{bean.options}">
<p:ajax process="#this" update="radio" listener="#{bean.listener}" />
<f:selectItem itemLabel="Option 1" itemValue="Option 1" />
<f:selectItem itemLabel="Option 2" itemValue="Option 2" />
</p:selectManyCheckbox>
<h:panelGroup id="radio" layout="block">
<h:panelGrid rendered="#{bean.needsSuboptions}" columns="2">
<p:selectOneRadio value="#{bean.suboption1}" layout="pageDirection" >
<f:selectItem itemLabel="Select ---" itemValue="" />
<f:selectItem itemLabel="Suboption 1-1" itemValue="Suboption 1-1" />
<f:selectItem itemLabel="Suboption 1-2" itemValue="Suboption 1-2" />
</p:selectOneRadio>
<p:selectOneRadio value="#{bean.suboption2}" layout="pageDirection" >
<f:selectItem itemLabel="Select ---" itemValue="" />
<f:selectItem itemLabel="Suboption 2-1" itemValue="Suboption 2-1" />
<f:selectItem itemLabel="Suboption 2-2" itemValue="Suboption 2-2" />
</p:selectOneRadio>
</h:panelGrid>
</h:panelGroup>
</h:form>
And the bean:
List<String> options;//getter+setter
String suboption1;//getter+setter
String suboption2;//getter+setter
boolean needsSuboptions = false;//getter
public void listener() {
needsSuboptions = options.contains("Option 1");
}
You can also do with f:ajax as the code that skuntsel provided.
<f:ajax render="" execute="" listener=""/>
maps to:
<p:ajax update="" process="" listener=""/>
<p:selectManyCheckbox id="check" value="#{displayPanel.options}">
<f:ajax event="change" render="radio" listener="#{displayPanel.listener}" />
<f:selectItem itemLabel="Option 1" itemValue="Option 1" />
<f:selectItem itemLabel="Option 2" itemValue="Option 2" />
</p:selectManyCheckbox>
<h:panelGroup id="radio" layout="block">
<h:panelGrid rendered="#{displayPanel.n}" columns="2">
<p:selectOneRadio value="#{displayPanel.a}" layout="pageDirection">
<f:selectItem itemLabel="Select ---" itemValue="" />
<f:selectItem itemLabel="Suboption 1-1" itemValue="Suboption 1-1" />
<f:selectItem itemLabel="Suboption 1-2" itemValue="Suboption 1-2" />
</p:selectOneRadio>
<p:selectOneRadio value="#{displayPanel.b}" layout="pageDirection">
<f:selectItem itemLabel="Select ---" itemValue="" />
<f:selectItem itemLabel="Suboption 2-1" itemValue="Suboption 2-1" />
<f:selectItem itemLabel="Suboption 2-2" itemValue="Suboption 2-2" />
</p:selectOneRadio>
</h:panelGrid>
you forgot the provide listener for f:ajax.