我有这样的要求。
有一个文件上传器,其中包含复选框和保存,取消按钮。
如果文件无效且必须保留选择,我必须禁用复选框。
如果单击保存按钮禁用后,复选框值在支持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");
答案 0 :(得分:0)
禁用元素不会作为零件表单提交过帐;您可以查看此answer了解更多详情。假设您正在使用ViewScoped bean,您可以使用AJAX在值更改时将值发布回bean。
答案 1 :(得分:-1)
我尝试使用隐藏变量技巧,它为我工作
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。