2012-08-07 142 views
0

我有一個關於jsf自動完成元素的問題。我希望我的頁面具有自動完成元素,並且在用戶從字段中選擇一個值後,會自動創建另一個字段。自動完成場的maksimum數爲3,那麼第三個之後,第4一個被禁用:JSF自動完成忽略空值

<h:outputLabel for="fieldsOfStudy" value="#{amsg.fieldsOfStudy}"/> 

<p:outputPanel id="fieldsOfStudy" autoUpdate="true" layout="block"> 
    <ui:repeat value="#{cc.attrs.offer.fieldsOfStudy}" var="studyField" varStatus="status"> 

      <h:panelGroup id="studyField" layout="block"> 
       <h:outputText value="#{amsg.handleGetObject(enumHelper.toMessageKey(studyField))}"/> 
       <p:commandLink action="#{cc.attrs.offer.removeFieldOfStudy(status.index)}" process="@this" 
         update="@([id$=fieldsOfStudyAutocomplete])" styleClass="ui-icon ui-icon-close"/> 
      </h:panelGroup> 

    </ui:repeat> 
</p:outputPanel> 

<h:message for="fieldsOfStudy" errorClass="error"/> 

<h:panelGroup id="fieldsOfStudyAutocomplete"> 
    <p:autoComplete value="#{offerBean.selectedFieldOfStudy}" dropdown="true" required="#{empty cc.attrs.offer.fieldsOfStudy}" 
         completeMethod="#{offerBean.completeFieldsOfStudy}" disabled="#{cc.attrs.offer.fieldsOfStudy.size() &gt;= 3}" 
         itemValue="#{p}" var="p" itemLabel="#{amsg.handleGetObject(enumHelper.toMessageKey(p))}" styleClass="xLargeInput"> 
       <p:ajax event="itemSelect" process="@this" update="@this"/> 
    </p:autoComplete> 
</h:panelGroup> 

一切工作好喜歡這一點,但問題是,每次用戶按下如果保存按鈕「 「學習領域」列表中有1個選定的元素來自動完成,列表中會有另一個具有空值的元素。如果有2個選擇的「研究領域」,第3個將被創建,但是生病值爲空值。如果有3個選擇的「研究領域」,那麼不會是列表中的任何第4個元素。

有沒有什麼辦法可以設置自動完成來忽略空字段?換句話說,如果用戶不從自動完成中選擇任何內容,那麼如何不將空值傳遞給DTO?

回答

0

我找到了一種方法。當在bean中設置值時,可以很容易地放入如下語句:

public void setSelectedFieldOfStudy(FieldOfStudy selectedFieldOfStudy) { 
    if (selectedFieldOfStudy != null) { 
     this.emptyOffer.getFieldsOfStudy().add(selectedFieldOfStudy); 
    } 
}