TreeTable dynamic population.

UI Components for JSF
Post Reply
jlferreira
Posts: 43
Joined: 16 Nov 2010, 17:52

25 Jan 2011, 13:11

Hi.
I need to create a TreeTable that contains the values of a List of a class.
In this List I have some attributes, among then, an attribute that will be used to identify the node, and another that will tell who is the node's parent.
Looking at the primefaces showcase, I saw that the nodes names are defined manually.
I need to create dynamically the parents nodes, the children nodes and the contents of this children nodes.
The parents nodes and the children nodes are identified in a class that has a self-referencing attribute that identify its parent node.
The contents of this children nodes are identified in another class, that has an referencing attribute to the nodes class.
I appreciate any help.
If you have an example of a dynamic population of a tree I will be very thankfull.

Thanks again.
Eclipse Helios.
JSF-2.0 (Mojarra 2.1.0-b11).
GlassFish 3.1.
PrimeFaces 2.2.1
EclipseLink 2.1.x - EclipseLink 2.1.x.
PostgreSQL 9.0

jlferreira
Posts: 43
Joined: 16 Nov 2010, 17:52

25 Jan 2011, 17:34

Anyone?
Eclipse Helios.
JSF-2.0 (Mojarra 2.1.0-b11).
GlassFish 3.1.
PrimeFaces 2.2.1
EclipseLink 2.1.x - EclipseLink 2.1.x.
PostgreSQL 9.0

dark_prime
Posts: 46
Joined: 02 Dec 2010, 14:30

25 Jan 2011, 20:02

Hi I had the same problem. Dynamic population is working, I just haven't had time to fix a bug.

You are not allowed to give the same name twice.

xhtml

Code: Select all

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

 <h:panelGrid columns="7">
     <h:outputLabel value="#{msgs.keyword}"/>
     <div style="width: 50px;"/>
	 <h:inputText id="keyword" value="#{keywordView.keyword.keyword}" label="#{msgs.keyword}"/>
	  <div style="width: 50px;"/>
    <h:outputLabel value="#{msgs.parentKeyword}"/>
             <div style="width: 50px;"/>
                 <h:selectOneListbox value="#{keywordView.keyword.parentId}" size="3" immediate="true">  
               	
                     <f:selectItems value="#{keywordController.getAllKeywords()}"/>
                     <f:selectItem itemLabel="Root" itemValue="0"/>  
                 </h:selectOneListbox>  
 
	 
	 <h:commandButton value="#{msgs.save}" action="#{keywordController.save(keywordView)}"/>
     </h:panelGrid>
keywordcontroller bean

Code: Select all

package de.pavs.controller;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.model.SelectItem;

import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.DualListModel;
import org.primefaces.model.TreeNode;

import de.pavs.ejb.CRUDService;
import de.pavs.model.Keyword;

@ManagedBean
@RequestScoped
public class KeywordController {

	@EJB
	CRUDService crudService;

	public KeywordController() {

	}

	DualListModel<String> keywordsDualList;

	public void setKeywordsDualList(DualListModel<String> keywordsDualList) {
		this.keywordsDualList = keywordsDualList;
	}

	public DualListModel<String> getKeywordsDualList() {

		List<String> keywordsSource = new ArrayList<String>();
		List<String> keywordsTarget = new ArrayList<String>();
		for (Keyword key : getAll()) {
			keywordsSource.add(key.getKeyword());

		}

		keywordsDualList = new DualListModel<String>(keywordsSource,
				keywordsTarget);
		return keywordsDualList;
	}

	public void save(KeywordView keywordView) {

		Keyword keyword = keywordView.getKeyword();

		if (keyword.getId() == 0) {
			keyword = crudService.create(keyword);

		}
	}

	public List<Keyword> getAll() {

		return crudService.findWithNamedQuery(Keyword.findAll, Keyword.class);

	}

	public List<SelectItem> getAllKeywords() {

		List<SelectItem> keywords = new ArrayList<SelectItem>();

		for (int i = 0; i < getAll().size(); i++) {
			keywords.add(new SelectItem(getAll().get(i).getId(), getAll()
					.get(i).getKeyword()));
		}

		return keywords;
	}

	private TreeNode root;

	public TreeNode getRoot() {
		root = new DefaultTreeNode("Root", null);
		ArrayList<TreeNode> nodes = new ArrayList<TreeNode>();

		List<Keyword> keywords = getAll();

		for (Keyword k : keywords) {
			if (k.getParentId() == 0) {
				nodes.add(new DefaultTreeNode(k.getKeyword(), root));
			}
		}

		for (Keyword k : keywords) {
			if (k.getParentId() != 0) {
				String parentKeyword = getAll()
						.get((int) (k.getParentId() - 1)).getKeyword();
				for (int i = 0; i < nodes.size(); i++) {
					if (nodes.get(i).getData().equals(parentKeyword))
						nodes.add(new DefaultTreeNode(k.getKeyword(), nodes
								.get(i)));

				}

			}

		}
		return root;
	}
}


Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 48 guests