Angular Tree Table Using PrimeNG

Angular demand is growing day by day and the demand of UI components and third party libraries are increasing rapidly. So one of the advance UI components that mostly we use in our project is Tree Table. So we know that we can create a basic table with pagination using Angular Material or Angular Bootstrap (ngx-bootstrap), but for Tree Table we need to use some specific Angular UI library which supports tree table structure and some of the UI Library which supports tree tables are like Prime NG, Nebular, AG-Grid etc. So in this tutorial we will explain that how to use PrimeNG and create Angular Tree table.

Install Angular and PrimeNG

npm i -g @angular/cli
ng new DemoProject --routing
npm install primeng --save
npm install primeicons --save
npm install @angular/cdk --save

In here first we install the angular cli and created a DemoProject with root routing and also install required dependency for primeng Angular library. Now lets create primeNG tree table. So create a Module name tree-table.

Create Tree Table Module and Component with Lazy Loading Routing

ng generate module tree-table --route tree-table --module app.module

So we have created a component Tree Table and now we will integrate primeng tree table in this component.

Import TreeTable Module in angular.module.ts File

import { BrowserModule } from '@angular/platform-browser';

Next we we will create a response.json file in assets/resource directory where we will store the sample JSOn structure of tree table and it is given below.

response.json Sample Response For Tree Table


Now lets create a NodeService where we will do the response.json api call and the code is given below. We need to import the TreeNode to define the type of the tree structure data and model structure look like this.

TreeNode Model Structure:

export interface TreeNode {

Create Node Service:

import {TreeNode} from 'primeng/api'; //Required 

After creating this service now we will create Table columns which will be dynamic and fetch the response.json data from the response.json and implement the tree table.

Tree Table Component

export class TreeTable implements OnInit {

Now we can see that we have got the data in files variable and we can use the variable in component html to render the Tree Table. In here we have converted the api call to Promise call just for make it simple but if you are using any stream data like if you are getting some realtime data for example from Firebase or Cloud Fires Store database or from any other source then please use observable instead of promise. So now our data is created and let's write the tree table structure in the component HTML.

Tree Table Component HTML

<p-treeTable [value]="files">

In the above HTML code you can see that we have created PrimeNG tree table structure, we have passed our main data which stores in files variable in the value attribute by doing property binding with p-treeTable component and also we have implemented the dynamic column based on the cols json.


So now in this way we can create a basic tree structure with dynamic column but if you want to do create some advance features or custom template then checkout out the documentation from primeNG they provide very robust options to customize the tree table.

I am Full Stack Developer and Data Scientist, I have worked some of the biggest client’s project (eg. Walmart, Cisco, Uber, Apple, JP Morgan, Capital One etc).

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store