2011-04-07 86 views
0

我正在使用primefaces樹組件。有一個樹的上下文菜單(添加一個節點,編輯節點,刪除節點)。執行一些操作後,我需要刷新樹並突出顯示添加或編輯的節點。如何從支持bean中突出顯示primefaces樹節點

這是我的代碼。

的index.xhtml

 <p:treeNode> 
      <h:outputText value="#{node}" /> 
     </p:treeNode> 
    </p:tree> 
    <p:contextMenu for="pTree" id="cmenu"> 
     <p:menuitem value="Add topic as child" update="pTree, cmenu" 
        actionListener="#{treeBean.addChildNode}" /> 
     <p:menuitem value="Add topic Below" update="pTree, cmenu" 
        actionListener="#{treeBean.addTopicBelow}" /> 
     <p:menuitem value="Delete Topic" update="pTree, cmenu" 
        actionListener="#{treeBean.deleteNode}" /> 
    </p:contextMenu> 

treeBean.java

公共類TreeBean實現Serializable {

private TreeNode root; 

public TreeBean() { 
    root = new DefaultTreeNode("Root", null); 
    // GET the root nodes first L0 
    List<TracPojo> rootNodes = SearchDao.getRootNodes111(); 
    Iterator it = rootNodes.iterator(); 

    while (it.hasNext()) { 

     TracPojo t1 = (TracPojo) it.next(); 

     String tid = t1.getTopicID(); 

     TreeNode node1 = new DefaultTreeNode(t1, root); 

    } 


} 
public TreeNode getRoot() { 
    return root; 
} 


public void addChildNode(ActionEvent actionEvent) 
{ 

    List record = NewSearchDao.getRecord(selectedNode); 

    Iterator it = record.iterator(); 
    while (it.hasNext()) { 
     Object[] record1 = (Object[]) it.next(); 
     setParentID_dlg((String) record1[0]); 
     setSortIndex((Integer) record1[2]); 
    } 

} 

public void saveChilddNode() { 
    System.out.println("Save as Child Node ........"); 

} 

}

回答

1

Primef王牌p:treeNode有一個屬性styleClass。你可以從你的支持bean動態地設置它。該視圖將如下所示:

<p:tree> 
    <p:treeNode styleClass="#{treeBean.styleClass}"> 
    <h:outputText value="#{node}" /> 
    </p:treeNode> 
</p:tree> 

然後添加一個成員的styleClass你TreeBean與返回表示樣式類的字符串的get/set方法:

public class TreeBean implements Serializable { 
    private String styleClass; 
    ... 
    public String getStyleClass() { 
    // your style selection logic here 
    } 
    ... 
} 

不要忘記添加樣式類到你的CSS。

+0

嗨馬特,謝謝你的答覆。 StyleClass用於指定節點樣式(mp3Style,圖片等)。但是,我的要求是不同的。在單個選擇樹中,如果我們單擊一個樹節點,它將以某種顏色突出顯示(例如:http://www.primefaces.org/showcase/ui/treeSelectionSingle.jsf)。我想刷新樹,展開父級並突出顯示藍色(Treenode,而不是數據)的新添加的樹節點。 – neni 2011-04-07 10:02:25

+0

爲了給出清晰的圖像:1)我有一個帶有上下文菜單的singleSelect樹(添加子項,編輯,刪除)。 2)當我點擊添加孩子時,會打開一個對話框,其中包含必需的字段和一個按鈕(保存)。 3)當我點擊保存按鈕時,我將節點插入數據庫並重新加載樹。我失去了選定的節點並且樹正在崩潰。 4)我只需刷新樹並突出顯示新添加的節點。 – neni 2011-04-07 10:12:55

+0

在這個問題上的任何幫助..? – neni 2011-05-12 12:43:51

1

除非您將您聲明爲selection =「#{treeBean.selectedNode}」的selectedNode設置爲null,否則它已被選中,並且唯一要做的就是更新樹中的樹組件觸發組件;在你的情況下它是:

<p:menuitem update=":yourForm:pTree" /*rest of the stuff*/ /> 
相關問題