0

我有这个 Primefaces 树:

JSF 代码

<h:form id="form">  

    <p:tree id="tree" value="#{TreeViewController.root}" var="node" dynamic="true" cache="true" animate="true"
            selectionMode="single"  selection="#{TreeViewController.selectedNode}" highlight="true">  
        <p:ajax event="select" listener="#{TreeViewController.onNodeSelect}" update=":form:outputComponent" />

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

    <p:outputPanel id="outputComponent">
        <h:outputText value="#{TreeViewController.data}" />
    </p:outputPanel>

</h:form> 

托管豆

private TreeNode root; 

    private TreeNode selectedNode;  

    public TreeView() {  
        root = new DefaultTreeNode("Root", null);  
        TreeNode node0 = new DefaultTreeNode("Node 0", root);  
        TreeNode node1 = new DefaultTreeNode("Node 1", root);  
        TreeNode node2 = new DefaultTreeNode("Node 2", root);
        TreeNode node3 = new DefaultTreeNode("Node 3", root);
        TreeNode node4 = new DefaultTreeNode("Node 4", root);  

        TreeNode node00 = new DefaultTreeNode("Node 0.0", node0);  
        TreeNode node01 = new DefaultTreeNode("Node 0.1", node0);
        TreeNode node02 = new DefaultTreeNode("Node 0.2", node0);  
        TreeNode node03 = new DefaultTreeNode("Node 0.3", node0);
        TreeNode node04 = new DefaultTreeNode("Node 0.4", node0);  
        TreeNode node05 = new DefaultTreeNode("Node 0.5", node0);
        TreeNode node06 = new DefaultTreeNode("Node 0.6", node0);
        TreeNode node07 = new DefaultTreeNode("Node 0.7", node0);  
        TreeNode node08 = new DefaultTreeNode("Node 0.8", node0);
        TreeNode node09 = new DefaultTreeNode("Node 0.9", node0);

        TreeNode node10 = new DefaultTreeNode("Node 1.0", node1);  
        TreeNode node11 = new DefaultTreeNode("Node 1.1", node1);  

        TreeNode node000 = new DefaultTreeNode("Node 0.0.0", node00);  
        TreeNode node001 = new DefaultTreeNode("Node 0.0.1", node00);  
        TreeNode node010 = new DefaultTreeNode("Node 0.1.0", node01);  

        TreeNode node100 = new DefaultTreeNode("Node 1.0.0", node10);  
    }  

    public TreeNode getRoot() {  
        return root;  
    } 

    public String data;

    public void onNodeSelect(NodeSelectEvent event) {
        String node = event.getTreeNode().getData().toString();

        data = node;
    }

    public String getData(){
        return data;
    }

    public TreeNode getSelectedNode() {  
        return selectedNode;  
    }  

    public void setSelectedNode(TreeNode selectedNode) {  
        this.selectedNode = selectedNode;  
    }  

我需要传递一个名为序列号的值,稍后将用于 SQL 查询以获取所选节点的信息。这个想法是为节点中的每个组件创建具有隐藏序列号的树。稍后此 SN 将用于从数据库中获取数据。如何为每个组件隐藏此 SN?现在我只能得到树节点的名称。

4

1 回答 1

0

尽管我回复它的时间很晚,但它仍然可能对其他人有帮助。这是我所做的,

我做了以下课程

class CustomizedTree{
    private Integer id;
    private String name;

    public CustomizedTree(Integer id, String name){
        this.id = id;
        this.name = name;
    }

    /**
     * @return the id
     */
    public Integer getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
}

在为树节点对象添加类型时,

CustomizedTree t = null;

        t = new CustomizedTree(device.getId(), device.getName());
TreeNode node0 = new TreeNodeImpl(t, root);

现在这样,我也可以将 Id 传递给树节点。

当一个节点被选中时,我同样检索它的 id。

CustomizedTree treeNode = (CustomizedTree)event.getTreeNode().getData();
    FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected",treeNode.getName());
于 2013-09-19T20:15:08.283 回答