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.

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.

Setup and Basic Example
The first step is to import the TreeTableModule in your app module:

<!--- 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:

<!--- tree-table-example.ts --->
import { Component } from '@angular/core';
interface TreeNode {
data: string;
children?: TreeNode[];
}
@Component({
selector: 'app-tree-table-example',
template: `
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:

<!--- 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) --->

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


















<!--- 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!