【问题标题】:Check box value is coming as false if it is disabled in primefaces如果在 primefaces 中禁用复选框,则复选框值将变为 false
【发布时间】:2017-03-30 13:40:42
【问题描述】:

我有这样的要求。

有一个带有复选框和保存、取消按钮的文件上传器 alopng。

如果文件无效并且选择应该保留,我必须禁用该复选框。

禁用后,如果我单击保存按钮,复选框值在支持 bean 中显示为 false,尽管它在对话框中显示为选中。

在调试时我看到 primeface 框架没有得到禁用复选框的 id,

这是我的 xhtml 文件:

    <h:form id="fileupload_form" enctype="multipart/form-data">
    <h:panelGroup id="fileupload_Panel">
    <p:message for="fileupload_Panel"></p:message>

    <p:panelGrid styleClass="noBorderGrid global_docs" columns="4">

        <h:outputText style="min-width:100px!important; margin-left: 42px;"></h:outputText>
        <p:selectBooleanCheckbox id="tt" value="#{bean.selectedDocs}"/>
        <h:outputText value="xx}"/>


        <h:outputText style="min-width:100px!important; margin-left: 42px;" rendered="#{cc.attrs.bean != null and cc.attrs.bean.currentDocument != null}"></h:outputText>
        <p:selectBooleanCheckbox id="checkBox" value="#{fileBean.tqpReport}"  disabled="${not fileBean.isActive}"/>
        <h:outputText value="xxxx"/>
        <p:tooltip for="t" value="xxxx"/>

    </p:panelGrid>

    <p:fileUpload id="documentFileUploader" fileUploadListener="#{bean.uploadDocumentsListener}" 
                    mode="advanced" dragDropSupport="true" multiple="true" auto="true"  update="@form" process="@form"
                    onstart="PF('ajax').show();"  onerror="PF('ajax').hide();"
                    style="width: 98%; max-width: 480px; max-height: 150px; overflow: auto;" styleClass="file-uploader-drag-drop" label="#{msgs.choose}" />
    </h:panelGroup>


     <p:panelGrid styleClass="noBorderGrid buttonTable" columns="3" style="float:right;">
         <p:commandButton id="save" icon="ui-icon-disk" styleClass="blue_button" value="${msgs.save}" style="float:right;"
        process="@form" update="@form" action="#{bean.save()}"/>
         <p:commandButton id="cancel" icon="ui-icon-close" styleClass="blue_button" value="${msgs.cancel}"
        action="#{bean.cancel()}"  style="float:right;"
        process="@this"/>
</p:panelGrid> 

 </h:form>

在调试时我发现在

   public void decode(FacesContext context, UIComponent component)

方法

   SelectBooleanCheckboxRenderer.java class 

该语句中已禁用复选框的 ID 为空。我不明白为什么?如果复选框未被禁用,我将获得正确的值。

  String submittedValue = (String) context.getExternalContext().getRequestParameterMap().get(clientId + "_input"); 

【问题讨论】:

  • 如果我正确理解您的问题,您的复选框处于禁用状态,并且值将变为 false,因为禁用字段未在提交按钮上提交,默认值为 false。

标签: primefaces jsf-2


【解决方案1】:

禁用的元素不会作为部分表单提交发布;您可以查看此answer 了解更多详细信息。假设您使用的是 ViewScoped bean,您可以在值更改时使用 AJAX 将值发布回 bean。

【讨论】:

    【解决方案2】:

    我尝试了隐藏变量技巧,它对我有用

    xhtm 文件中的隐藏变量,以及支持 bean 中的 getter 和 setter。

    <h:inputHidden id="checkBoxSelection" value="#{fileBean.checkBoxSelection}"/>
    

    在保存时,我得到了选择,并设置为隐藏变量,例如,

     <p:commandButton id="save" icon="ui-icon-disk" styleClass="blue_button" value="${msgs.save}" style="float:right;"
        process="@form" update="@form" action="#{fileBean.saveUploadedDocuments(cc.attrs.id)}" 
     onclick="sendCheckBoxSelection('fileupload_form_widget_tqpReportCheckBox','fileupload_form:checkBoxSelection');"/>
    

    而js函数为:

    function sendCheckBoxSelection(checkBoxId,hiddenparamId){
    var checkBoxSelection = false;
    var tqpCheckBox = getWidgetVarById(checkBoxId);
    if (tqpCheckBox != null){
        checkBoxSelection = PrimeFaces.widgets[checkBoxId].input.is(':checked')
        document.getElementById(hiddenparamId).value = checkBoxSelection;
      }
    }
    
     function getWidgetVarById(id) {
    for (var propertyName in PrimeFaces.widgets) {
      if (propertyName === id) {
        return PrimeFaces.widgets[propertyName];
       }
      }
    }
    

    因此,复选框值将在保存时设置为支持 bean。

    http://blog.hatemalimam.com/intro-to-primefaces-widgetvar

    【讨论】:

      猜你喜欢
      • 2014-07-11
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多