Open In App

Angular PrimeNG Tree Filter

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. In this article, we will see how to use Tree Filter in Angular PrimeNG.

Angular PrimeNG Tree Filter is used to enable Filter options in the Tree component. So, using filtering options, we can easily search and filter out the required data according to our needs.



Angular PrimeNG Tree Filter Property:

 



Syntax:

<p-tree [value]="..." [filter]="true"></p-tree>

Creating Angular application & Module Installation:

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

ng new appname

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

cd appname

Step 3: Install PrimeNG in your given directory.

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

Project Structure: It will look like the following:

 

Example 1: Below is a simple example demonstrating the use of the Angular PrimeNG Tree Filter.




<h2 style="color: green">
    GeeksforGeeks
</h2>
<h4>
    Angular PrimeNG Tree Filter
</h4>
<p-tree [value]="files1" 
        [filter]="true">
</p-tree>




import { Component, OnInit } from '@angular/core';
import { TreeNode } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
export class AppComponent {
    files1: TreeNode[] = [];
  
    constructor() { }
  
    ngOnInit() {
        this.files1 = [
            {
                label: 'A',
                icon: 'pi pi-folder',
  
                children: [
                    {
                        label: 'B',
                        icon: 'pi pi-folder',
  
                        children: [
                            {
                                label: 'C',
                                icon: 'pi pi-folder',
                            },
                            {
                                label: 'D',
                                icon: 'pi pi-folder',
                            },
                        ],
                    },
                    {
                        label: 'E',
                        icon: 'pi pi-folder',
  
                        children: [
                            {
                                label: 'F',
                                icon: 'pi pi-folder',
                            },
                        ],
                    },
                ],
            },
            {
                label: 'G',
                icon: 'pi pi-folder',
  
                children: [
                    {
                        label: 'H',
                        icon: 'pi pi-folder',
                    },
                    {
                        label: 'I',
                        icon: 'pi pi-folder',
                    },
                    {
                        label: 'J',
                        icon: 'pi pi-folder',
                    },
                ],
            },
        ];
    }
}




import { NgModule } from '@angular/core';
import { BrowserModule } 
    from '@angular/platform-browser';
import { BrowserAnimationsModule }
    from '@angular/platform-browser/animations';
      
import { AppComponent } from './app.component';
import { TreeModule } from 'primeng/tree';
import { FormsModule } from '@angular/forms';
import { InputTextModule } from 'primeng/inputtext';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TreeModule,
        InputTextModule,
        FormsModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [],
})
export class AppModule { }

Output:

 

Example 2: Below is another example demonstrating the use of the Angular PrimeNG Tree Filter. In this example, we are applying a strict filter mode, so after searching the descendants are ignored.




<h2 style="color: green">
    GeeksforGeeks
</h2>
<h4>
    Angular PrimeNG Tree Filter
</h4>
<p-tree [value]="files1" 
        [filter]="true" 
        filterMode="strict">
</p-tree>




import { Component, OnInit } from '@angular/core';
import { TreeNode } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
export class AppComponent {
    files1: TreeNode[] = [];
  
    constructor() { }
  
    ngOnInit() {
        this.files1 = [
            {
                label: 'A',
                icon: 'pi pi-folder',
  
                children: [
                    {
                        label: 'B',
                        icon: 'pi pi-folder',
  
                        children: [
                            {
                                label: 'C',
                                icon: 'pi pi-folder',
                            },
                            {
                                label: 'D',
                                icon: 'pi pi-folder',
                            },
                        ],
                    },
                    {
                        label: 'E',
                        icon: 'pi pi-folder',
  
                        children: [
                            {
                                label: 'F',
                                icon: 'pi pi-folder',
                            },
                        ],
                    },
                ],
            },
            {
                label: 'G',
                icon: 'pi pi-folder',
  
                children: [
                    {
                        label: 'H',
                        icon: 'pi pi-folder',
                    },
                    {
                        label: 'I',
                        icon: 'pi pi-folder',
                    },
                    {
                        label: 'J',
                        icon: 'pi pi-folder',
                    },
                ],
            },
        ];
    }
}




import { NgModule } from '@angular/core';
import { BrowserModule } 
    from '@angular/platform-browser';
import { BrowserAnimationsModule }
    from '@angular/platform-browser/animations';
      
import { AppComponent } from './app.component';
import { TreeModule } from 'primeng/tree';
import { FormsModule } from '@angular/forms';
import { InputTextModule } from 'primeng/inputtext';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TreeModule,
        InputTextModule,
        FormsModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [],
})
export class AppModule { }

Output:

 

Reference: https://primefaces.org/primeng/tree/filter


Article Tags :