Open In App

Angular PrimeNG Splitter Stateful

Last Updated : 25 Jan, 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. In this article, we will see how to use Angular PrimeNG Splitter Stateful

The Splitter Component allows a user to split two-element using a splitter & utilized it separately and resize panels.

Angular PrimeNG Splitter Stateful: Splitters can be made stateful meaning when the user visits the page again, the adjusted sizes can be restored back. We need to define a stateKey to enable this feature. We can also set property stateStorage to define the storage location of the state.

 

Syntax:

<p-splitter stateKey="..." 
     stateStorage="...">
    <ng-template pTemplate>
        .....
    </ng-template>
</p-splitter>

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:

 

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

ng serve --save

Example 1: In this example, we will learn about Angular PrimeNG Splitter Stateful. We will set the property stateKey, meaning even if we refresh the page, the system will retain our adjusted size.

  • app.component.html:

HTML




<h1 style="color: green;">
    GeeksforGeeks
</h1>
<h2>
    Angular PrimeNG Splitter Stateful
</h2>
<p-splitter [panelSizes]="[50, 50]" 
     [minSizes]="[20, 40]" stateKey="gfg">
    <ng-template pTemplate>
        <div class="p-col p-ai-center p-jc-center">
            <img alt="gfg" src=
            />
        </div>
    </ng-template>
  
    <ng-template pTemplate>
        <div class="p-col p-ai-center p-jc-center">
            <img alt="gfg" src=
            />
        </div>
    </ng-template>
</p-splitter>


  • app.component.ts:

Javascript




import { Component } from "@angular/core";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html"
})
  
export class AppComponent {}


  • app.module.ts:

Javascript




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


Output:

 

Example 2: In this example, we will see what if we don’t make the splitter stateful. We will see, that after the refresh, the splitter will get back to its original position and it will not retain the adjusted dimensions.

  • app.component.html:

HTML




<h1 style="color: green;">
    GeeksforGeeks
</h1>
<h2>
    Angular PrimeNG Splitter Stateful
</h2>
<p-splitter [panelSizes]="[50, 50]" [minSizes]="[20, 40]">
    <ng-template pTemplate>
        <div class="p-col p-ai-center p-jc-center">
            <img alt="gfg" src=
            />
        </div>
    </ng-template>
  
    <ng-template pTemplate>
        <div class="p-col p-ai-center p-jc-center">
            <img alt="gfg" src=
            />
        </div>
    </ng-template>
</p-splitter>


  • app.component.ts:

Javascript




import { Component } from "@angular/core";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html"
})
  
export class AppComponent {}


  • app.module.ts:

Javascript




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


Output:

 

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads