如果在primefaces中禁用复选框值,则该值将为false

问题描述 投票:0回答:2

我有这样的要求。

有一个文件上传以及复选框和保存,取消按钮。

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

如果单击保存按钮禁用后,复选框值在支持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在该语句中为null。我不明白为什么?如果未禁用该复选框,我将获得正确的值。

  String submittedValue = (String) context.getExternalContext().getRequestParameterMap().get(clientId + "_input"); 
primefaces jsf-2
2个回答
0
投票

禁用元素不会作为部分表单提交发布;你可以看到这个answer了解更多细节。假设您正在使用ViewScoped bean,您可以使用AJAX在值更改时将值发布回bean。


-1
投票

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

xhtml文件中的隐藏变量,以及辅助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

© www.soinside.com 2019 - 2024. All rights reserved.