2012-03-14 46 views
3

我想爲應用程序創建設置面板。應用程序會將設置值存儲到數據庫表中。設置面板將用於顯示設置並更改值。我要代表這樣的值:如何在選擇特定的selectOneMenu選項時顯示inputText?

enter image description here

enter image description here

這樣,用戶就可以只輸入固定值。我想改變用戶輸入自定義值。就像這樣:

enter image description here

我想與選項custom創建selectOneMenu。當用戶選擇custom時,selectOneMenu將替換爲inputText字段,在那裏他將能夠輸入自定義值。 SAVE按鈕將數據保存到數據庫中。這可能沒有頁面重新加載?也許用AJAX?

這是如何實現的?

回答

3

每當當前選項等於"custom"時,使用<f:ajax>顯示<h:inputText>

<h:selectOneMenu value="#{bean.type}"> 
    <f:selectItem itemValue="one" itemLabel="Option one" /> 
    <f:selectItem itemValue="two" itemLabel="Option two" /> 
    <f:selectItem itemValue="three" itemLabel="Option three" /> 
    <f:selectItem itemValue="custom" itemLabel="Define custom value" /> 
    <f:ajax render="input" /> 
</h:selectOneMenu> 
<h:panelGroup id="input"> 
    <h:inputText value="#{bean.customType}" rendered="#{bean.type == 'custom'}" required="true" /> 
</h:panelGroup> 
+0

很好的回答!這完成了我需要的代碼嗎? – 2012-03-14 20:04:16

+2

是的。支持bean代碼應該足夠明顯。上面的例子假設兩個'String'屬性''type''和'customType'。如果需要在Bean中動態預填充項目,您當然也可以使用''。有關'h:selectOneMenu'用法的更多詳細信息,請查看標記wiki頁面:http://stackoverflow.com/tags/selectonemenu/info – BalusC 2012-03-14 20:04:58

+0

最後一個問題:用戶輸入數據後,JSF頁面必須如何將值傳遞給託管的bean爲了將它們更新到數據庫中?我想我需要表格。像這樣:http://pastebin.com/Fq19q5z9 – 2012-03-14 23:11:13

相關問題