Open In App

Angular PrimeNG TreeTable Selection

Last Updated : 01 Feb, 2023
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 will see the Angular PrimeNG TreeTable Selection.

TreeTable is used to display hierarchical data in tabular format. The Events help us to know the actions that are taking place and we can keep a check on them and also show relevant updates accordingly.

 

Angular PrimeNG TreeTable Selection:

  • Single Selection: It is the single selection of the TreeTable row.
  • Multiple Selection with MetaKey: It is the multiple selections of the TreeTable row by pressing the ctrl+mouse click.
  • Multiple Selection without MetaKey: It is the multiple selections of the TreeTable row.
  • Checkbox Selection: It is the multiple selections of the TreeTable row using toggle checkboxes.
  • Events: It is the single selection of the TreeTable row by triggering the selected event with toast messages.

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:

 

Steps to run the application: Run the below command to see the output

ng serve --save

Example 1: Below is the example code that illustrates the use of TreeTable Selection in Angular PrimeNG. In the following example, we have a simple TreeTable with a single Selection mode.

  • app.component.html:

HTML




<div style="text-align: center;">
<h1 style="color: green;">GeeksforGeeks</h1>
<h5>Angular PrimeNG TreeTable Selection</h5>
</div>
  
<p-treeTable
    [value]="gfg1"
    selectionMode="single"
    [columns]="mycols"
    dataKey="name">
    <ng-template pTemplate="header" let-columns>
        <tr>
            <th *ngFor="let col of columns">
                {{col.header}}
            </th>
        </tr>
    </ng-template>
    <ng-template
        pTemplate="body"
        let-rowNode
        let-rowData="rowData"
        let-columns="columns">
        <tr [ttRow]="rowNode" 
             [ttRow]="rowNode" [ttSelectableRow]="rowNode">
            <td *ngFor="let col of columns; let i = index">
                <p-treeTableToggler
                    [rowNode]="rowNode"
                    *ngIf="i == 0">
                </p-treeTableToggler>
                {{rowData[col.field]}}
            </td>
        </tr>
    </ng-template>
</p-treeTable>


  • app.component.ts:

Javascript




import { Component, OnInit } from "@angular/core";
import { TreeNode } from "primeng/api";
import { MessageService, MenuItem } from "primeng/api";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
    providers: [MessageService]
})
  
export class AppComponent {
    gfg1: TreeNode[];
    cols: any[];
  
    mycols: any[];
  
    ngOnInit() {
        this.gfg1 = [
            {
                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"
                        }
                    }
                ]
            }
        ];
  
        this.cols = [
            { field: "name", header: "First Name" },
            { field: "age", header: "Age" }
        ];
  
        this.mycols = this.cols;
    }
}


  • app.module.ts:

Javascript




import { NgModule } from "@angular/core";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { NodeService } from "./nodeservice";
import { TreeTableModule } from "primeng/treetable";
  
@NgModule({
    imports: [
      BrowserAnimationsModule,
      TreeTableModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [NodeService]
})
  
export class AppModule {}


Output:

 

Example 2: Below is another example code that illustrates the use of TreeTable Selection in Angular PrimeNG. In the following example, we have a simple TreeTable with a multiple selection mode without  MetaKey.

  • app.component.html:

HTML




<div style="text-align: center;">
    <h1 style="color: green;">GeeksforGeeks</h1>
    <h5>Angular PrimeNG TreeTable Selection</h5>
</div>
  
<p-treeTable
    [value]="gfg1"
    selectionMode="multiple" 
    [metaKeySelection]="false"
    [columns]="mycols"
    dataKey="name">
    <ng-template pTemplate="header" let-columns>
        <tr>
            <th *ngFor="let col of columns">
                {{col.header}}
            </th>
        </tr>
    </ng-template>
    <ng-template
        pTemplate="body"
        let-rowNode
        let-rowData="rowData"
        let-columns="columns">
        <tr [ttRow]="rowNode" 
             [ttRow]="rowNode" [ttSelectableRow]="rowNode">
            <td *ngFor="let col of columns; let i = index">
                <p-treeTableToggler
                    [rowNode]="rowNode"
                    *ngIf="i == 0">
                  </p-treeTableToggler>
                {{rowData[col.field]}}
            </td>
        </tr>
    </ng-template>
</p-treeTable>


  • app.component.ts:

Javascript




import { Component, OnInit } from "@angular/core";
import { TreeNode } from "primeng/api";
import { MessageService, MenuItem } from "primeng/api";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
    providers: [MessageService]
})
  
export class AppComponent {
    gfg1: TreeNode[];
    cols: any[];
  
    mycols: any[];
  
    ngOnInit() {
        this.gfg1 = [
            {
                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"
                        }
                    }
                ]
            }
        ];
  
        this.cols = [
            { field: "name", header: "First Name" },
            { field: "age", header: "Age" }
        ];
  
        this.mycols = this.cols;
    }
}


  • app.module.ts:

Javascript




import { NgModule } from "@angular/core";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { NodeService } from "./nodeservice";
import { TreeTableModule } from "primeng/treetable";
  
@NgModule({
    imports: [
      BrowserAnimationsModule,
      TreeTableModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [NodeService]
})
  
export class AppModule {}


Output:

 

Reference: https://primefaces.org/primeng/treetable/selection



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads