Skip to content
Related Articles
Open in App
Not now

Related Articles

Angular PrimeNG Tree Horizontal Orientation

Improve Article
Save Article
Like Article
  • Last Updated : 30 Aug, 2022
Improve Article
Save Article
Like Article

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. This article will show us how to use the Tree Horizontal Orientation in Angular PrimeNG.

Angular PrimeNG Tree Horizontal Orientation displays Tree Data in Horizontal Order. we can easily modify, manipulate, and show the data in a Horizontal layout using Horizontal Orientation. For that, we use the layout property and set it to “horizontal”.

Syntax:

<p-tree [value]="..." layout="horizontal"></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:

Project Structure

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

app.component.html




<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Tree Horizontal Orientation</h4>
<p-tree 
    [value]="files1" 
    layout="horizontal">
</p-tree>


app.component.ts




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


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 { TreeModule } from 'primeng/tree';
import { ButtonModule } from 'primeng/button';
import { InputTextModule } from 'primeng/inputtext';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TreeModule,
        ButtonModule,
        InputTextModule,
        HttpClientModule,
        FormsModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [NodeService],
})
export class AppModule { }


Output:

 

Example 2: Below is a simple example that demonstrates the use of the Angular PrimeNG Tree Horizontal Orientation, In this example, we are making a Tree Component with icons along with the text.

app.component.html




<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Tree Horizontal Orientation</h4>
<p-tree 
    [value]="files1" 
    layout="horizontal">
</p-tree>


app.component.ts




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


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 { TreeModule } from 'primeng/tree';
import { ButtonModule } from 'primeng/button';
import { InputTextModule } from 'primeng/inputtext';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TreeModule,
        ButtonModule,
        InputTextModule,
        HttpClientModule,
        FormsModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [NodeService],
})
export class AppModule { }


Output:

 

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


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!