2012-07-02 38 views
0

在我的應用程序中,我有一些dataTables的對話框,用戶可以編輯值,所以我使用primefaces rowEditor ...有時我在dataTable中有必填字段,您可以在此屏幕中看到:Primefaces rowEditor只是活動行驗證

dataTable with required fields

在該畫面中,不要求用戶填寫的所有行,所以,如果我把需要= 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。 有沒有辦法讓這些字段只有在編輯模式下才需要?我usgin primefaces 2.2.1用於MyFaces

回答

0

我解決創造我自己的複合材料部件......這樣,我必須在驗證發生了更多的控制權問題。

cellEditor的:

<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> 

rowEditor:

<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> 

唯一的問題是,我不能在表格只更新一排......也許我將創造另一個組件...

+0

以下鏈接可能對您的問題有用, http://forum.primefaces.org/viewtopic.php?f=3&t=6015 – Jitesh

2

以下的解決方法爲我工作:

添加到您的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> 

然後修改您的OK /保存按鈕:

<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檢查,當用戶單擊確定/保存以查看用戶是否處於「編輯模式」時,會發生這種情況。編輯模式檢查檢查是否有任何複選標記圖標可見;如果它是可見的,那麼用戶必須處於編輯模式(如果您在頁面的其他地方使用檢查圖標,則需要修改此解決方法)。如果用戶處於編輯模式,則表單提交時立即=「false」,因此將進行驗證。如果用戶未處於編輯模式,則表單將以immedidate =「true」提交,因此不會進行驗證。對於「notImmediate」按鈕單擊,更新屬性設置爲update =「form」,以便可以使用與任何驗證錯誤關聯的任何錯誤消息來更新表單。

+0

注意,替代品y,你可能會考慮不使用required =「true」,而是編寫自己的Validator或者添加驗證邏輯到你的Action方法,該方法可以處理你處於編輯模式還是不處於編輯模式。 – BestPractices

+0

這種方法有點令人困惑...所以我最好創建我自己的組件 – brevleq

0

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