【问题标题】:Primefaces rowEditor just active row validationPrimefaces rowEditor 只是活动行验证
【发布时间】:2023-04-10 16:03:01
【问题描述】:

在我的应用程序中,我有一些带有 dataTables 的对话框,用户可以编辑值,所以我使用 primefaces rowEditor... 有时我在 dataTable 中有必填字段,如您在此屏幕中看到的那样:

在此屏幕中,用户不需要填写所有行,因此,如果我设置 required=true,则所有字段(包括停用的字段)都会显示必需的验证消息。这样,如果用户单击“确定”按钮,他将无法关闭弹出窗口,因为存在很多必填字段验证错误。 我尝试以这种方式在必需属性中使用参数:

<f:facet name="input">
    <h:selectOneMenu value="#{cfop.idTipoTributacao}" required="#{param['EDITAVEL']}"
                                             style="width: 100px;">
        <f:selectItems value="#{selectTipoTributacao.itens}"/>
    </h:selectOneMenu>
</f:facet>

但不幸的是,当用户单击 p:rowEditor 的保存按钮时,我没有找到传递此参数的方法。 有没有一种方法可以使这些字段仅在它们处于编辑模式时才需要?我用 MyFaces 使用 primefaces 2.2.1

【问题讨论】:

    标签: jsf jsf-2 primefaces myfaces


    【解决方案1】:

    以下解决方法对我有用:

    将此添加到您的 JSF 页面:

    <script type="text/javascript">
    function validateIfEditing()
    {
        var isEditing = false;
        var checkmark= $(".ui-icon-check");
        jQuery.each(checkmark, function() {             
            if ('' == this.style.display) {
                isEditing = true;   
            }
        });
        if (isEditing) {
            $("#form\\:notImmediate").click();          
        }
        else {  
            $("#form\\:immediate").click(); 
        }
    }
    </script>
    

    然后修改你的确定/保存按钮:

    <p:commandButton id="okButton" value="OK" onclick="validateIfEditing();"/>
    <p:commandButton id="immediate" action="#{controller.save}" immediate="true" style="display:none"/> 
    <p:commandButton id="notImmediate" action="#{controller.save}" update="form" style="display:none"/>
    

    实际上,这是一个 javscript 检查,当用户单击 OK/Save 以查看用户是否处于“编辑模式”时发生。编辑模式检查检查是否有任何复选标记图标可见;如果它可见,则用户必须处于编辑模式(如果您在页面的其他位置使用复选图标,则需要修改此解决方法)。如果用户处于编辑模式,那么表单会以 immediate="false" 提交,因此会进行验证。如果用户未处于编辑模式,则表单以 immediadate="true" 提交,因此不会发生验证。对于“notImmediate”按钮单击,更新属性设置为 update="form",以便可以使用与任何验证错误相关的任何错误消息来更新表单。

    【讨论】:

    • 注意,或者,您可能会考虑不使用 required="true" 而是编写自己的验证器或将验证逻辑添加到能够处理您是否处于编辑模式的 Action 方法中处于编辑模式。
    • 这种方法有点混乱......所以我更喜欢创建自己的组件
    【解决方案2】:

    我解决了创建自己的复合组件的问题...这样我可以更好地控制验证发生的时间。

    单元格编辑器:

    <c:interface>
        <c:attribute name="id" required="false" targets="celula"/>
        <c:attribute name="desabilitado" required="false" default="true"/>
        <c:facet name="entrada" required="true"/>
        <c:facet name="saida" required="true"/>
    </c:interface>
    <c:implementation>
        <h:panelGroup id="celula" layout="block" styleClass="hrgi-celula">
            <h:panelGroup layout="block" styleClass="hrgi-celula-entrada" style="display: none">
                <c:renderFacet name="entrada" required="true"/>
            </h:panelGroup>
            <h:panelGroup layout="block" styleClass="hrgi-celula-saida">
                <c:renderFacet name="saida" required="true"/>
            </h:panelGroup>
        </h:panelGroup>
    </c:implementation>
    

    行编辑器:

    <c:interface>
        <c:attribute name="action" method-signature="void method(java.lang.Object)"/>
        <c:attribute name="indice" required="true"/>
        <c:attribute name="update" required="false" default=""/>
    </c:interface>
    <c:implementation>
        <h:outputScript library="js" name="hrgiRowEditor.js" target="head"/>
        <h:panelGroup layout="block">
            <h:panelGroup id="painelBotaoEdicao" layout="block" style="display: inline">
                <h:graphicImage library="img" name="pencil.png" onclick="ativarCamposEditaveis('#{cc.clientId}');"
                            style="cursor: pointer;"/>
            </h:panelGroup>
            <h:panelGroup id="painelBotoesSalvamento" layout="block" style="display: none">
                <h:commandLink id="botaoSalvar" style="float: left;" onclick="definirIdRowEditor('#{cc.clientId}')">
                    <h:graphicImage library="img" name="check.png"/>
                    <f:param name="#{cc.attrs.indice}" value="true"/>
                    <p:ajax event="click" update="@form #{cc.attrs.update}" process="@form" listener="#{cc.attrs.action}"
                        oncomplete="desativarCamposEditaveisQuandoSucesso(xhr, status, args, this);"/>
                </h:commandLink>
                <h:graphicImage library="img" name="cancel.png" style="float: left; cursor: pointer;"
                            onclick="desativarCamposEditaveis('#{cc.clientId}');">
                </h:graphicImage>
            </h:panelGroup>
        </h:panelGroup>
    </c:implementation>
    

    唯一的问题是我不能只更新表格中的一行...也许我会创建另一个组件...

    【讨论】:

    【解决方案3】:

    或者

    <p:commandButton id="okButton" value="OK" onclick="return $('.ui-icon-check:visible').length == 0"/>
    

    【讨论】:

      猜你喜欢
      • 2016-03-01
      • 1970-01-01
      • 2012-02-21
      • 1970-01-01
      • 2012-11-29
      • 2013-04-03
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      相关资源
      最近更新 更多