2013-03-15 58 views
0

使用JSF和PrimeFaces時遇到了一些問題。我有一個表格,有兩個<p:selectonemenu>和兩個<h:outputText> s。第一個<p:selectonemenu>的內容更新第二個,兩個<h:outputText>更新爲第二個<p:selectonemenu><p:selectonemenu> s都依賴於我的支持bean上的值。問題是,當FIRST <p:selectonemenu>更改其值時,我無法清除<h:outputText>清除JSF上的輸出字段

我嘗試使用<f:ajax>與偵聽器來清除當前顯示的實體的內容。

這裏的XHTML:

<h:outputText value="#{bundle.BailmentFamiliesLabel_bailmentFamCode}"/> 
<p:selectOneMenu id="bailmentFamilyCode" value="#{bailmentsController.selected.bailmentFamId}" required="false"> 
    <f:selectItems value="#{bailmentFamiliesController.itemsAvailableSelectOne}" var="famid"/> 
    <f:ajax event="change" render="description price bailmentCode" listener="${bailmentsController.clearView(famid)}"/> 
</p:selectOneMenu> 
<h:outputText value="#{bundle.BailmentsLabel_bailmentCode}"/> 
<p:selectOneMenu id="bailmentCode" value="#{bailmentsController.selected}" required="false"> 
    <f:selectItems value="#{bailmentsController.itemsAvailableSelectOneByFamId}"/> 
    <f:ajax event="change" render="globalForm" listener="#{bailmentsController.view(bailmentsController.selected.bailmentCode,bailmentsController.selected.bailmentFamId)}" /> 
</p:selectOneMenu> 
<h:outputText value="#{bundle.Label_description}"/> 
<h:outputText value="#{bailmentsController.selected.description}" title="#{bundle.Title_description}" id="description"/> 
<h:outputText value="#{bundle.BailmentsLabel_purchasePriceMsj}"/> 
<h:outputText value="#{bailmentsController.selected.purchasePrice}" title="#{bundle.BailmentsTitle_purchasePrice}" id="price"/> 

而且第<p:selectonemenu>上bailmentsController監聽器:每個保釋被分配一個BailmentFamily,

public void clearView(BailmentFamilies bf) { 
    if (bf != null) { 
     current = new Bailments(); 
     current.setBailmentFamId(bf); 
    } 
} 

在我的數據庫這就是爲什麼我使用的是2步篩選器。第一個<p:selectonemenu>列出了所有可用的BailmentFamilies,第二個<p:selectonemenu>顯示了所選BailmentFamily的所有可用的Bailment。 <h:outputText>顯示所選保釋人的信息,這就是爲什麼當BailmentFamily更改時需要清除它們的原因。

任何人都可以提出另一個行動方案嗎?因爲這沒有被證明是有效的。非常感謝你。

+0

爲什麼你期待''在沒有被列入第一個選擇菜單的''標籤的'render'屬性中時被更新爲ajax? – kolossus 2013-03-15 18:36:19

回答

2

爲什麼你希望<h:outputText>在沒有被列入<f:ajax/>標記的render屬性中的第一個選擇菜單中時被更新爲ajax?您需要

  1. 分配id屬性都<h:outputText>小號

    <h:outputText id="text1" value="#{bundle.BailmentFamiliesLabel_bailmentFamCode}"/> 
    <h:outputText id="text2" value="#{bundle.BailmentsLabel_bailmentCode}"/> 
    
  2. 參考的ID在<f:ajax/>

    <f:ajax event="change" render="description text1 text2 price bailmentCode" listener="${bailmentsController.clearView(famid)}"/> 
    

到目前爲止,這是可行的。對於最佳/最佳實踐立場

  1. 改爲使用<p:ajax/>。它應該(因爲底層的jQuery)執行速度更快,讓你訪問其他功能

    <p:ajax update="description, text1, text2, price, bailmentCode" listener="${bailmentsController.clearView(famid)}"/> 
    
  2. 使用EL符號(#),而不是JSTL($

    <p:ajax update="description, text1, text2, price, bailmentCode" listener="#{bailmentsController.clearView(famid)}"/> 
    
+0

我剛剛解決了這個問題。我會用答案編輯問題。我也忘了補充一切都在''內,所以所有的組件都應該被重新渲染,謝謝 – rubbyrubber 2013-03-15 18:47:45

+0

我假設OP的問題中提供的所有組件都在' h:form id =「globalForm」>'因此得到這個錯誤,請參考OP自己的回答來解決這個問題 – 2013-03-15 20:13:09

+0

@LuiggiMendoza,現在很明顯,沒有那些信息,我被引導相信OP已經清除了價值在後面,但該視圖未能反映出 – kolossus 2013-03-15 20:32:32

2

我剛剛解決了這個問題。原來,我必須清除我的實體內的所有值。愚蠢的perhaphs,但其他開發者可能會被卡住:

我編輯了我的clearView()方法。這是工作的代碼:

public void clearView(BailmentFamilies bf) { 
    if (bf != null) { 
     // Create a new empty object to recieve the new BailmentFamily 
     current = new Bailments(); 
     current.setBailmentFamId(bf); // This property has to be kept. 
     // I pass it as parameter and reassigned it to the current entity. 
     // Otherwise, the <p:selectonemenu> will clear upon rendering because of the new call above. 
    } 

    // Set ALL displayed properties to null 
    current.setBailmentCode(null); 
    current.setDescription(null); 
    current.setPurchasePrice(null); 
} 

希望這可以幫助任何人。

+0

+1的變化,請儘量在將來的問題中包含相關信息位 – kolossus 2013-03-15 20:33:05

0

你也可以使用,

<input type="button" value="Reset Form" onclick="this.form.reset()" /> 

這將重置表單值,但不支持bean,所以如果u使用從形式外一個ActionListener /動作表單數據這種做法並不安全。