Open In App

Angular PrimeNG TreeTable Responsive

Improve
Improve
Like Article
Like
Save
Share
Report

Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. It provides a lot of templates, components, theme design, an extensive icon library, and much more.

In this article, we are going to learn Angular PrimeNG TreeTable Responsive. TreeTable is used to display hierarchical data in tabular format. The Responsive TreeTable is not provided by default but we can implement using the following steps.

Syntax:

app.component.ts: Use a custom CSS as follows:

@Component({
    templateUrl: './app.component.html',
    styles: [`
        :host ::ng-deep .priority-2,
        :host ::ng-deep .priority-3,
        :host ::ng-deep .visibility-sm {
            display: none;
        }
        @media screen and (max-width: 39.938em) {
            :host ::ng-deep .visibility-sm {
                display: inline;
            }
        }
        @media screen and (min-width: 40em) {
            :host ::ng-deep .priority-2 {
                display: table-cell;
            }
        }
        @media screen and (min-width: 64em) {
            :host ::ng-deep .priority-3 {
                display: table-cell;
            }
        }
    `]
})
export class TreeTableResponsiveDemo {
    data: TreeNode[];
    cols: any[];
    constructor(private nodeService: NodeService) { }
    ngOnInit() {
        this.nodeService.getFilesystem()
            .then(data => this.data = data);

        this.cols = [
            { field: 'name', header: 'Name' },
            { field: 'size', header: 'Size' },
            { field: 'data', header: 'Date' }
        ];
    }
}

app.component.html: Set the priorities of components as follows:

<p-treeTable [value]="files">
    <ng-template pTemplate="header">
        <tr>
            <th>Name</th>
            <th class="priority-2">Size</th>
            <th class="priority-3">Date</th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" 
        let-rowNode let-rowData="rowData">
        <tr>
            <td>
                <p-treeTableToggler 
                    [rowNode]="rowNode">
                   </p-treeTableToggler>
                {{rowData.name}}
                <span class="visibility-sm">
                    / {{rowData.size}} - {{rowData.type}}
                </span>
            </td>
            <td class="priority-2">{{rowData.size}}</td>
            <td class="priority-3">{{rowData.date}}</td>
        </tr>
    </ng-template>
</p-treeTable>

Creating Angular application & Module Installation:

Step 1: Create an Angular application using the following command.

ng new geeks_angular

Step 2:  After creating your project folder i.e. geeks_angular, move to it using the following command.

cd geeks_angular

Step 3: Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: The project structure will look like the following:

Project Structure

Example 1: In the following example, we have a responsive TreeTable with two columns

app.component.html




<h1 style="color: green; text-align:center;">
    GeeksforGeeks
</h1>
  
<h3>Angular PrimeNG TreeTable Responsive</h3>
  
<p-treeTable [columns]="cols" [value]="tableData">
    <ng-template pTemplate="header">
        <tr>
            <th>Name</th>
            <th class="priority-2">Age</th>
        </tr>
    </ng-template>
  
    <ng-template pTemplate="body" 
        let-rowNode let-rowData="rowData" 
        let-columns="columns">
        <tr>
            <td>
                <p-treeTableToggler 
                    [rowNode]="rowNode">
                </p-treeTableToggler>
                {{ rowData.name }}
            </td>
            <td class="priority-2">{{ rowData.age }}</td>
        </tr>
    </ng-template>
</p-treeTable>


app.component.ts




import { Component, OnInit, ViewChild } from '@angular/core';
import { NodeService } from './nodeservice';
import { TreeNode } from 'primeng/api';
import { TreeTable } from 'primeng/treetable';
import { MessageService } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [MessageService],
    styles: [
        `
        :host ::ng-deep .priority-2,
        :host ::ng-deep .priority-3,
        :host ::ng-deep .visibility-sm {
            display: none;
        }
  
        @media screen and (max-width: 39.938em) {
            :host ::ng-deep .visibility-sm {
                display: inline;
            }
        }
  
        @media screen and (min-width: 40em) {
            :host ::ng-deep .priority-2 {
                display: table-cell;
            }
        }
  
        @media screen and (min-width: 64em) {
            :host ::ng-deep .priority-3 {
                display: table-cell;
            }
        }
    `,
    ],
})
export class AppComponent {
    tableData: TreeNode[];
  
    cols: any[];
  
    constructor(private messageService: MessageService) { }
  
    ngOnInit() {
        this.cols = [
            { field: 'name', header: 'First Name' },
            { field: 'age', header: 'Age' },
        ];
        this.tableData = [
            {
                data: {
                    name: 'A',
                    age: '40',
                },
                children: [
                    {
                        data: {
                            name: 'B',
                            age: '16',
                        },
                    },
                    {
                        data: {
                            name: 'C',
                            age: '14',
                        },
                    },
                ],
            },
            {
                data: {
                    name: 'D',
                    age: '55',
                },
                children: [
                    {
                        data: {
                            name: 'E',
                            age: '20',
                        },
                    },
                    {
                        data: {
                            name: 'F',
                            age: '24',
                        },
                    },
                ],
            },
            {
                data: {
                    name: 'G',
                    age: '32',
                },
                children: [
                    {
                        data: {
                            name: 'H',
                            age: '20',
                        },
                    },
                    {
                        data: {
                            name: 'I',
                            age: '24',
                        },
                    },
                ],
            },
            {
                data: {
                    name: 'J',
                    age: '64',
                },
                children: [
                    {
                        data: {
                            name: 'K',
                            age: '20',
                        },
                    },
                    {
                        data: {
                            name: 'L',
                            age: '24',
                        },
                    },
                ],
            },
            {
                data: {
                    name: 'M',
                    age: '12',
                },
                children: [
                    {
                        data: {
                            name: 'N',
                            age: '20',
                        },
                    },
                    {
                        data: {
                            name: 'O',
                            age: '24',
                        },
                    },
                ],
            },
            {
                data: {
                    name: 'P',
                    age: '34',
                },
                children: [
                    {
                        data: {
                            name: 'Q',
                            age: '20',
                        },
                    },
                    {
                        data: {
                            name: 'R',
                            age: '24',
                        },
                    },
                ],
            },
            {
                data: {
                    name: 'S',
                    age: '43',
                },
                children: [
                    {
                        data: {
                            name: 'T',
                            age: '20',
                        },
                    },
                    {
                        data: {
                            name: 'U',
                            age: '24',
                        },
                    },
                ],
            },
        ];
    }
}


app.module.ts




import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } from 
    '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { NodeService } from './nodeservice';
import { TreeTableModule } from 'primeng/treetable';
import { ButtonModule } from 'primeng/button';
import { InputTextModule } from 'primeng/inputtext';
import { ToastModule } from 'primeng/toast';
import { RippleModule } from 'primeng/ripple';
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TreeTableModule,
        ButtonModule,
        InputTextModule,
        HttpClientModule,
        FormsModule,
        ToastModule,
        RippleModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [NodeService],
})
export class AppModule { }


Output:

 

Example 2: In the following example, we have three columns.

app.component.html




<h1 style="color: green; text-align:center;">
    GeeksforGeeks
</h1>
  
<h3>Angular PrimeNG TreeTable Responsive</h3>
  
<p-treeTable [columns]="cols" [value]="tableData">
    <ng-template pTemplate="header">
        <tr>
            <th>Name</th>
            <th class="priority-2">Age</th>
            <th class="priority-3">Date of Birth</th>
        </tr>
    </ng-template>
  
    <ng-template pTemplate="body" 
        let-rowNode let-rowData="rowData" 
        let-columns="columns">
        <tr>
            <td>
                <p-treeTableToggler 
                    [rowNode]="rowNode">
                </p-treeTableToggler>
                {{ rowData.name }}
            </td>
            <td class="priority-2">{{ rowData.age }}</td>
            <td class="priority-3">{{ rowData.dob }}</td>
        </tr>
    </ng-template>
</p-treeTable>


app.component.ts




import { Component, OnInit, ViewChild } from '@angular/core';
import { NodeService } from './nodeservice';
import { TreeNode } from 'primeng/api';
import { TreeTable } from 'primeng/treetable';
import { MessageService } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [MessageService],
    styles: [
        `
        :host ::ng-deep .priority-2,
        :host ::ng-deep .priority-3,
        :host ::ng-deep .visibility-sm {
            display: none;
        }
  
        @media screen and (max-width: 39.938em) {
            :host ::ng-deep .visibility-sm {
                display: inline;
            }
        }
  
        @media screen and (min-width: 40em) {
            :host ::ng-deep .priority-2 {
                display: table-cell;
            }
        }
  
        @media screen and (min-width: 64em) {
            :host ::ng-deep .priority-3 {
                display: table-cell;
            }
        }
    `,
    ],
})
export class AppComponent {
    tableData: TreeNode[];
  
    cols: any[];
  
    constructor(private messageService: MessageService) { }
  
    ngOnInit() {
        this.cols = [
            { field: 'name', header: 'First Name' },
            { field: 'age', header: 'Age' },
            { field: 'dob', header: 'Date of Birth' },
        ];
        this.tableData = [
            {
                data: {
                    name: 'A',
                    age: '40',
                    dob: '23-11-1943',
                },
                children: [
                    {
                        data: {
                            name: 'B',
                            age: '16',
                            dob: '14-11-1952',
                        },
                    },
                    {
                        data: {
                            name: 'C',
                            age: '14',
                            dob: '23-11-1943',
                        },
                    },
                ],
            },
            {
                data: {
                    name: 'D',
                    age: '55',
                    dob: '04-02-1963',
                },
                children: [
                    {
                        data: {
                            name: 'E',
                            age: '20',
                            dob: '23-11-1943',
                        },
                    },
                    {
                        data: {
                            name: 'F',
                            age: '24',
                            dob: '23-11-1943',
                        },
                    },
                ],
            },
            {
                data: {
                    name: 'G',
                    age: '32',
                    dob: '12-12-1765',
                },
                children: [
                    {
                        data: {
                            name: 'H',
                            age: '20',
                            dob: '07-01-2002',
                        },
                    },
                    {
                        data: {
                            name: 'I',
                            age: '24',
                            dob: '23-11-1943',
                        },
                    },
                ],
            },
            {
                data: {
                    name: 'J',
                    age: '64',
                    dob: '12-08-1921',
                },
                children: [
                    {
                        data: {
                            name: 'K',
                            age: '20',
                            dob: '23-11-1943',
                        },
                    },
                    {
                        data: {
                            name: 'L',
                            age: '24',
                            dob: '23-11-1943',
                        },
                    },
                ],
            },
            {
                data: {
                    name: 'M',
                    age: '12',
                    dob: '09-11-2007',
                },
                children: [
                    {
                        data: {
                            name: 'N',
                            age: '20',
                            dob: '23-11-1943',
                        },
                    },
                    {
                        data: {
                            name: 'O',
                            age: '24',
                            dob: '23-11-1943',
                        },
                    },
                ],
            },
        ];
    }
}


app.module.ts




import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } from 
    '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { NodeService } from './nodeservice';
import { TreeTableModule } from 'primeng/treetable';
import { ButtonModule } from 'primeng/button';
import { InputTextModule } from 'primeng/inputtext';
import { ToastModule } from 'primeng/toast';
import { RippleModule } from 'primeng/ripple';
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TreeTableModule,
        ButtonModule,
        InputTextModule,
        HttpClientModule,
        FormsModule,
        ToastModule,
        RippleModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [NodeService],
})
export class AppModule { }


Output:

 

Reference: http://primefaces.org/primeng/treetable



Last Updated : 06 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads