2012-02-16 32 views
0

我遇到了關於primeface組件樹的SelectListener的問題。Primefaces Tree SelectListener重置其他表單輸入

更新:我想要做的是創建一個組,並從樹中選擇一個路徑。所以我有組名和路徑樹。選擇路徑後,在樹下面,應該顯示所選路徑並通過AJAX請求進行更新。

這裏是我的代碼片段:

<h:form id="groupCreate"> 

    <p:inputText id="createGroupName" value="#{groupContainer.name}" /> 

    <p:tree id="pathTree" update="groupCreate" selectionMode="single" 
     selection="#{groupContainer.selectedPath}" dynamic="false" 
     value="#{groupContainer.rootNode}" var="node" cache="false" 
     nodeSelectListener="#{groupContainer.onNodeSelect}"> 
     <p:treeNode> 
      <h:outputText value="#{node[1]}" title="#{node[0]}" /> 
     </p:treeNode> 
    </p:tree> 

    <!-- Display selected Path from tree --> 
    <h:outputText value="#{groupContainer.chosenPathString}/> 

    <p:commandLink id="createButton" .../> 

</h:form> 

在我豆的onNodeSelect功能如下:

public void onNodeSelect(NodeSelectEvent event) { 
    //get the selected data and set it 
    this.chosenPathString = //selected Text; 
} 

這一般工程 - 是指顯示所選擇的路徑中的AJAX請求後。但是,如果我在createGroupName輸入字段中輸入了一些文本,然後選擇了一個節點,在AJAX請求之後createGroupName再次設置爲空。

因此,我的AJAX請求更新樹的選定路徑名稱將重置所有我當前鍵入的值。經過一些調試後,我發現AJAX請求忽略了我所有的類型值,因爲它們還沒有被提交(就像他們會,如果我提交表單)。但是,我怎樣才能改變我的代碼,使其發揮作用?

任何幫助將不勝感激,並提前抱歉我的英語不好!

回答

4

您的p:tree更新整個表單。這意味着表單被重新渲染。爲了保存其他表單輸入值,您需要提交整個表單onNodeSelect或僅更新真正需要更新的表單元素。

分配一個ID,h:outputText所選路徑和改變p:treeupdate屬性,如:

<h:form id="groupCreate"> 

    <p:inputText id="createGroupName" value="#{groupContainer.name}" /> 

    <p:tree id="pathTree" update="choosenPath" selectionMode="single" 
     selection="#{groupContainer.selectedPath}" dynamic="false" 
     value="#{groupContainer.rootNode}" var="node" cache="false" 
     nodeSelectListener="#{groupContainer.onNodeSelect}"> 
     <p:treeNode> 
      <h:outputText value="#{node[1]}" title="#{node[0]}" /> 
     </p:treeNode> 
    </p:tree> 

    <!-- Display selected Path from tree --> 
    <h:outputText id="choosenPath" value="#{groupContainer.chosenPathString}/> 

    <p:commandLink id="createButton" .../> 

</h:form> 
+0

哦,我的上帝,你不知道有多少工作時間在這個問題上。現在它可以工作。在我的實際形式中,我使用了更多的元素,所以在我編寫所有元素ID之前我想,我只是提供表單ID以節省打字時間。 非常感謝你! – thunderhook 2012-02-16 11:49:56

+0

很高興聽到它的作品! – 2012-02-16 11:57:43