Primeng Tree Table Example: Mastering Nested Data

Ashley Aug 09, 2026

Looking for an effective way to display hierarchical data in your PrimeNG application? The TreeTable component is an excellent choice. It allows you to visualize data in a tree structure, similar to a file system explorer. Let's dive into a detailed example to get you started.

Business Rules Management Software
Business Rules Management Software

Before we begin, ensure you've installed PrimeNG and have a basic understanding of Angular. We'll use TypeScript for our examples. Let's build a simple tree table displaying categories and their subcategories.

Prime Factor Tree Example
Prime Factor Tree Example

Setup and Basic Example

The first step is to import the TreeTableModule in your app module:

a tree with a wooden shelf attached to it
a tree with a wooden shelf attached to it

<!--- app.module.ts ---> import { TreeTableModule } from 'primeng/treetable'; // Import TreeTableModule @NgModule({ ... imports: [ ... TreeTableModule, // Add it to the imports array ... ], ... }) export class AppModule { } <!--- /app.module.ts --->

Next, let's create a simple tree table component:

a table with potted plants on it under a tree
a table with potted plants on it under a tree

<!--- tree-table-example.ts ---> import { Component } from '@angular/core'; interface TreeNode { data: string; children?: TreeNode[]; } @Component({ selector: 'app-tree-table-example', template: ` `, }) export class TreeTableExample { cols: any[]; files: TreeNode[] = [ { data: 'Documents', children: [ { data: 'Work' }, { data: 'Home' }, ], }, { data: 'Pictures', children: [ { data: 'barcelona.jpg' }, { data: 'logo.jpg' }, ], }, ]; ngOnInit() { this.cols = [ { field: 'data', header: 'Name' }, ]; } } <!--- /tree-table-example.ts --->

Nesting and Expandable Rows

The example above displays a simple list. To make it a tree, we need to set 'expandable' to true in the column definition:

a wooden picnic table under a tree in the grass
a wooden picnic table under a tree in the grass

<!--- tree-table-example.ts (updated) ---> ... ngOnInit() { this.cols = [ { field: 'data', header: 'Name', expandable: true }, // Add 'expandable: true' ]; } ... <!--- /tree-table-example.ts (updated) --->

Now, let's see how to add more levels to our tree:

<!--- tree-table-example.ts (updated) ---> ... files: TreeNode[] = [ { data: 'Documents', children: [ { data: 'Work', children: [ { data: 'angular.xlsx' }, { data: 'tailwind.css' }, ], }, { data: 'Home' }, ], }, ... ]; ... <!--- /tree-table-example.ts (updated) --->

a table under a large tree in the middle of a garden with flowers around it
a table under a large tree in the middle of a garden with flowers around it

Styling and Customization

You can easily customize the appearance of your tree table. Here's how to add some styling:

"Luxury Meets Nature: The Stunning Indoor Tree Table You’ll Want in Your Home 🌿✨"
"Luxury Meets Nature: The Stunning Indoor Tree Table You’ll Want in Your Home 🌿✨"
DIY Live-Edge Table
DIY Live-Edge Table
a picnic table under a tree in front of a red truck parked on the street
a picnic table under a tree in front of a red truck parked on the street
a wooden table and benches under a tree
a wooden table and benches under a tree
How to Find The Prime Factors Using Factor Tree - A Plus Topper
How to Find The Prime Factors Using Factor Tree - A Plus Topper
A wooden table around a tree.🌳 DIY backyard.🏡
A wooden table around a tree.🌳 DIY backyard.🏡
white and green
white and green
MACRÆ MARKÆ
MACRÆ MARKÆ
an outdoor table set up in the woods with plates and cups on it, surrounded by tree stumps
an outdoor table set up in the woods with plates and cups on it, surrounded by tree stumps
Wooden bench under the tree
Wooden bench under the tree
two people sitting at a table under a tree in a courtyard with tables and chairs around it
two people sitting at a table under a tree in a courtyard with tables and chairs around it
18+ Inspiring Outdoor Tree Stump Table Ideas
18+ Inspiring Outdoor Tree Stump Table Ideas
a wooden table with a glass on it next to a tree
a wooden table with a glass on it next to a tree
the screenshot shows an image of different types of bank accounts and their names
the screenshot shows an image of different types of bank accounts and their names
a potted plant sitting on top of a wooden table surrounded by stumps and flowers
a potted plant sitting on top of a wooden table surrounded by stumps and flowers
a christmas tree is in the center of a room with two chairs and a table
a christmas tree is in the center of a room with two chairs and a table
16+ Creative DIY Outdoor Table Ideas - Little Makes
16+ Creative DIY Outdoor Table Ideas - Little Makes
30 Creative and Inspirational Ideas for Tree Bench Designs
30 Creative and Inspirational Ideas for Tree Bench Designs

<!--- tree-table-example.css ---> p-treeTable { font-family: 'Arial', sans-serif; } p-treeTable .p-tree-table-tbody td { padding: 10px; } p-treeTable .p-tree-table-tbody td.p-highlight { background-color: #f2f2f2; border-bottom: 1px solid #ddd; } <!--- /tree-table-example.css --->

Lazy Loading

Large datasets can be lazy-loaded to improve performance. PrimeNG's TreeTable supports lazy loading out of the box. To use it, you need to use the TreeNodeService:

Using the TreeNodeService

The TreeNodeService helps in lazy loading. Here's how you can use it:

First, update your app module:

<!--- app.module.ts ---> import { TreeNodeService } from 'primeng/treetable'; ... providers: [ ... TreeNodeService, // Add TreeNodeService to the providers array ... ], ... <!--- /app.module.ts --->

Next, inject TreeNodeService into your component:

<!--- lazy-tree-table-example.ts ---> import { Component, OnInit } from '@angular/core'; import { TreeNodeService } from 'primeng/treetable'; @Component({ ... }) export class LazyTreeTableExample implements OnInit { files: TreeNode[] = [ /* ... */ ]; constructor(private treeNodeService: TreeNodeService) { } ... } <!--- /lazy-tree-table-example.ts --->

Finally, update your getLazyLoadNodes entrou:

<!--- lazy-tree-table-example.ts (updated) ---> ... getLazyLoadNodes(event: LazyLoadEvent) { setTimeout(() => { if (eventdeep && event.globalFilter) { this.treeNodeService.resolveNodes(this.files[event.index], (data) => { // Simulate lazy load delay setTimeout(() => { for (let i = 0; i < 5; i++) { data.push({ data: `New item ${i}`, children: null }); } }, Math.random() * 500 + 250); }); } }, Math.random() * 1000 + 500); } <!--- /lazy-tree-table-example.ts (updated) --->

That's it! You now have a fully functional, lazy-loaded tree table. PrimeNG's TreeTable offers many more features like sorting, row expanding, and selection that you can explore in the official documentation.

Tree tables are an excellent way to display hierarchical data in your applications. With PrimeNG's intuitive API and extensive features, you can create engaging and user-friendly tree tables in no time. Happy coding!

© 2026 Ashley Ideas. All rights reserved.