Filtering updates the node based on the constraints.
<div class="card">
<h:form>
<h5>Basic</h5>
<p:tree value="#{treeBasicView.root}" var="node" filterBy="#{node}" filterDelay="400">
<p:treeNode>
<h:outputText value="#{node}"/>
</p:treeNode>
</p:tree>
<h5>Custom</h5>
<p:tree value="#{treeBasicView.root}" var="node" filterFunction="#{treeBasicView.customFilter}">
<p:treeNode>
<h:outputText value="#{node}"/>
</p:treeNode>
</p:tree>
</h:form>
</div>
package org.primefaces.showcase.view.data.tree;
import jakarta.annotation.PostConstruct;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
import java.io.Serializable;
import java.util.Locale;
import io.quarkus.runtime.annotations.RegisterForReflection;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.primefaces.util.LangUtils;
@Named("treeBasicView")
@ViewScoped
@RegisterForReflection(serialization = true)
public class BasicView implements Serializable {
private TreeNode root;
@PostConstruct
public void init() {
root = new DefaultTreeNode("Files", null);
TreeNode node0 = new DefaultTreeNode("Documents", root);
TreeNode node1 = new DefaultTreeNode("Events", root);
TreeNode node2 = new DefaultTreeNode("Movies", root);
TreeNode node00 = new DefaultTreeNode("Work", node0);
TreeNode node01 = new DefaultTreeNode("Home", node0);
node00.getChildren().add(new DefaultTreeNode("Expenses.doc"));
node00.getChildren().add(new DefaultTreeNode("Resume.doc"));
node01.getChildren().add(new DefaultTreeNode("Invoices.txt"));
TreeNode node10 = new DefaultTreeNode("Meeting", node1);
TreeNode node11 = new DefaultTreeNode("Product Launch", node1);
TreeNode node12 = new DefaultTreeNode("Report Review", node1);
TreeNode node20 = new DefaultTreeNode("Al Pacino", node2);
TreeNode node21 = new DefaultTreeNode("Robert De Niro", node2);
node20.getChildren().add(new DefaultTreeNode("Scarface"));
node20.getChildren().add(new DefaultTreeNode("Serpico"));
node21.getChildren().add(new DefaultTreeNode("Goodfellas"));
node21.getChildren().add(new DefaultTreeNode("Untouchables"));
}
public TreeNode getRoot() {
return root;
}
public boolean customFilter(TreeNode treeNode, Object filter, Locale locale) {
if (treeNode.getData() == null || filter == null) {
return true;
}
String filterText = filter.toString().trim().toLowerCase(locale);
if (LangUtils.isBlank(filterText)) {
return true;
}
return ((String) treeNode.getData()).toLowerCase(locale).contains(filterText);
}
}