依赖于Primefaces的selectOneMenu和required ="true" [英] Primefaces dependent selectOneMenu and required="true"

查看:106
本文介绍了依赖于Primefaces的selectOneMenu和required ="true"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有三个下拉菜单(p:selectOneMenu),例如A,B,C.其中两个是依赖的,例如B和C.通过更改BI的值可以将值动态加载到C.是一个文本框.当这三个下拉菜单触发on-change事件时,文本框的值由ajax生成.

In my application I have three dropdown menu (p:selectOneMenu), say A, B, C. Among them two are dependent, say B and C. By changing the value of B I am dynamically loading values to C. Also there is a textbox. The value of the textbox is generating by ajax when the on-change event is firing from these three dropdowns.

这是xhtml:

<p:selectOneMenu id="customerMenu" value="#{adminController.activityDTO.customerId}" required="true" label="Customer Name" style="width: 200px">
    <f:selectItem itemLabel="Select One" itemValue="" />
    <f:selectItems value="#{adminController.customers}" var="customer" itemLabel="#{customer.customerName}" itemValue="#{customer.customerId}" />
    <p:ajax listener="#{adminController.generateActivityName}" update="activityId" />                       
</p:selectOneMenu>

<p:selectOneMenu id="activityTypeMenu" value="#{adminController.activityDTO.activityParentType}" required="true" label="Activity Type"
    style="width: 200px">
    <f:selectItem itemLabel="Select One" itemValue="" />
    <f:selectItems value="#{adminController.activityTypes}" var="activityType" itemLabel="#{activityType.parent}" itemValue="#{activityType.parent}" />
    <p:ajax listener="#{adminController.updateDependentActivity}" update="activitySubType" />
</p:selectOneMenu>

<p:selectOneMenu id="activitySubTypeMenu" value="#{adminController.activityDTO.activitySubType}" required="true" label="Activity Sub Type"
    style="width: 200px">
    <f:selectItem itemLabel="Select One" itemValue="" />
    <f:selectItems value="#{adminController.activitySubTypes}" var="activityType" itemLabel="#{activityType.name}" itemValue="#{activityType.id}" />
    <p:ajax listener="#{adminController.generateActivityId}" update="activityId" />
</p:selectOneMenu>

<p:inputText id="activityId" autocomplete="off" readonly="true" value="#{adminController.activityDTO.activityId}"
    label="#{adbBundle['admin.addActivityPanel.addActivityTable.activityId']}" required="true" />

activityTypeMenuactivitySubTypeMenu取决于我正在填充activitySubTypeMenuactivityTypeMenu的选定值.

The activityTypeMenu and activitySubTypeMenu are dependent, by the selected value of the activityTypeMenu I am populating the activitySubTypeMenu.

现在我面临的问题是:

  • 说我已经在activityTypeMenu中选择了外部"和内部",默认是选择一个".如果我从activityTypeMenu中选择外部",则activitySubTypeMenu将具有项目"和服务".但是,如果我选择默认的选择一个",则activitySubTypeMenu仍保留以前动态填充的值.这是因为required="true"属性拒绝触发我从中加载动态值的后端方法.
  • 我尝试将<f:selectItem itemLabel="Select One" itemValue="" />itemValue设置为#{null},然后选择选择一个"选项时启动了后端方法,因此我可以将空列表设置为activitySubTypes,这样activitySubTypeMenu清空.但是在那种情况下,required="true"变得毫无意义.我的意思是,我也有保存按钮,然后单击该按钮时未从activityTypeMenuactivitySubTypeMenu中选择任何选项(即选择选择一个"),而没有抛出ValidatorException并且组件没有被错误CSS设置样式Primefaces类.
  • 此外,如果我没有将<f:selectItem itemLabel="Select One" itemValue="" />itemValue设置为#{null},那么将所选值更改为默认选项(选择一个")不会清除activityId p:inputText.如果使用#{null},则可以触发后端方法,从该方法可以将文本框的值设置为空.
  • Say I have select "External" and "Internal" in activityTypeMenu and default "Select One". If I choose "External" from activityTypeMenu the activitySubTypeMenu will have "Project" and "Service". But then if I choose the default "Select One" the activitySubTypeMenu is still holding the previously dynamically populated values. This is because the required="true" attribute resisting to fire the backend method from which I am loading the dynamic value.
  • I have tried to set the itemValue of <f:selectItem itemLabel="Select One" itemValue="" /> to #{null} and then the backend method is firing on selecting the "Select one" option and I can set an empty list to activitySubTypes and this way the activitySubTypeMenu get empty. But in that case the required="true" is getting meaningless. I mean, I also have save button and on clicking that button without selecting any option (that is selecting "Select one") from activityTypeMenu and activitySubTypeMenu not throwing ValidatorException and the components are not getting styled by the error css class of Primefaces.
  • Also if I don't set the itemValue of <f:selectItem itemLabel="Select One" itemValue="" /> to #{null} then on-changing to selected value to default option("Select one") does not clearing out the activityId p:inputText. If I use #{null} then I can get the backend method firing from which I can set the value of the textbox to empty.

我如何解决此问题并获得期望的结果.我想要的是:

How ca i solve this issues and get desired result. What I want are:

  • 如果该选项设置为选择一个",则从属菜单将为空,而文本框将为空.
  • 我想使用required="true"属性.
  • If the option is set to "Select one" then the dependent menu will be empty and that of the textbox.
  • I want to use the required="true" attribute.

推荐答案

您可以在required属性中使用EL.仅当按下特定的提交按钮或提交了特定的组件值(并因此通过其在请求参数映射#{param}中的客户端ID)时,才可以让所需的表达式求值true.

You can use EL in the required attribute. You can let the desired expression evaluate true only when a particular submit button is pressed, or when a particular component value is submitted (and thus by its client ID present in the request parameter map #{param}).

以下启动示例应能满足您的需求.

The following kickoff example should do what you need.

<p:selectOneMenu binding="#{menu1}" ... required="#{not empty param[submit.clientId]}">
    ...                       
</p:selectOneMenu>
<p:selectOneMenu ... required="#{not empty param[menu1.clientId]}">
    ...                       
</p:selectOneMenu>
<p:commandButton binding="#{submit}" ... />

这样,仅当按下表单的主提交按钮时才需要第一个菜单(因此,在触发事件侦听器时不需要),而仅当第一个菜单具有值时才需要第二个菜单.

This way, the 1st menu is only required when the form's main submit button is pressed (and thus not when event listeners are fired) and the 2nd menu is only required when the 1st menu has a value.

这篇关于依赖于Primefaces的selectOneMenu和required ="true"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆