我有这个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>
托管Bean
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? 现在我只能获得树节点的名称。
答案 0 :(得分: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());