Open In App

Angular PrimeNG Table TableState

Last Updated : 27 Oct, 2022
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 Table TableState.

Angular PrimeNG Table is used to display data in tabular format. The stateful table allows keeping the state of the table that is page, sorting or filtering either at local storage or session storage so that when the page is visited again, the table would retrieve from the saved state.

Angular PrimeNG Table TableState Fields:

  • stateKey(string): To enable table state, the stateKey is required and unique.
  • stateStorage(string): It is used to specify the type of storage, the types are session for session storage and local for local storage.

 

Syntax: 

<p-table [columns]="cols" 
         [value]="cars" 
         stateStorage="session" 
         stateKey="gfg">
    <ng-template pTemplate="body" 
                 let-rowData 
                 let-columns="columns">
        <tr [pSelectableRow]="rowData">
            <td *ngFor="let col of columns">
                {{rowData[col.field]}}
            </td>
        </tr>
    </ng-template>
</p-table>

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:

 

Example 1: In the following example, we have a Table that saves the state in the session of the browser.

  • app.component.html:

HTML




<h1 style="color:green;text-align:center;">
    GeeksforGeeks
</h1>
<h3>Angular PrimeNG Table TableState</h3>
<h5>Basic</h5>
  
<p-table #dt 
         [value]="tutorials" 
         dataKey="title" 
         [rowHover]="true" 
         [showCurrentPageReport]="true" 
         [filterDelay]="0"
         stateStorage="session" 
         stateKey="gfg-session" 
         [globalFilterFields]="['title', 'category', 'rating']">
    <ng-template pTemplate="caption">
        <div class="table-header">
            List of Tutorials
            <span class="p-input-icon-left">
                <i class="pi pi-search"></i>
                <input 
                    pInputText type="text" 
                    (input)="dt.filterGlobal($event.target.value, 'contains')"
                    placeholder="Global Search" />
            </span>
        </div>
    </ng-template>
    <ng-template pTemplate="header">
        <tr>
            <th style="width: 3rem"></th>
            <th pSortableColumn="title">
                Title 
                <p-sortIcon field="title"></p-sortIcon>
            </th>
            <th pSortableColumn="category">
                Category 
                <p-sortIcon field="category"></p-sortIcon>
            </th>
            <th pSortableColumn="rating">
                Rating 
                <p-sortIcon field="rating"></p-sortIcon>
            </th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" 
                 let-tutorial 
                 let-rowIndex="rowIndex">
        <tr class="p-selectable-row">
            <td>
                <p-tableCheckbox 
                    [value]="tutorial" 
                    [index]="rowIndex" 
                    [disabled]="tutorial.rating < 4">
                </p-tableCheckbox>
            </td>
            <td>
                <span class="p-column-title">
                    Title
                </span>
                {{ tutorial.title }}
            </td>
            <td>
                <span class="p-column-title">
                    Category
                </span>
                <span class="image-text">
                    {{ tutorial.category }}
                </span>
            </td>
            <td>
                <span class="p-column-title">
                    Rating
                </span>
                <span class="image-text">
                    {{ tutorial.rating }}
                </span>
            </td>
        </tr>
    </ng-template>
</p-table>


  • app.component.ts

Javascript




import { Component, OnInit, ViewChild } from '@angular/core';
import { Table } from 'primeng/table';
import { PrimeNGConfig } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
export class AppComponent {
    tutorials: Tutorial[];
    @ViewChild('dt') table: Table;
  
    constructor(private primengConfig: PrimeNGConfig) { }
  
    ngOnInit() {
        this.tutorials = [
            
                title: 'Queue'
                category: 'Data Structure'
                rating: 4 
            },
            
                title: 'Circularly LinkedList'
                category: 'Data Structure'
                rating: 5 
            },
            
                title: 'Doubly LinkedList'
                category: 'Data Structure'
                rating: 3 
            },
            
                title: 'Singly LinkedList'
                category: 'Data Structure'
                rating: 4 
            },
            
                title: 'Doubly Ended Queue'
                category: 'Data Structure'
                rating: 5 
            },
            
                title: 'Binary Search Tree'
                category: 'Data Structure'
                rating: 4 
            },
            
                title: 'Red Black Tree'
                category: 'Data Structure'
                rating: 5 
            },
            
                title: 'Breadth First Search'
                category: 'Graph'
                rating: 4 
            },
            
                title: "Floyd's Cycle"
                category: 'Algorithm', 
                rating: 4 
            },
            {
                title: 'Travelling Salesman Problem',
                category: 'Algorithm',
                rating: 5,
            },
            
                title: 'Bellman Ford', 
                category: 'Graph', 
                rating: 4 
            },
            
                title: 'KMP Algorithm', 
                category: 'String', 
                rating: 5 
            },
        ];
  
        this.primengConfig.ripple = true;
    }
}
export interface Tutorial {
    title?: string;
    category?: string;
    rating?: number;
}


  • app.module.ts

Javascript




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 { TableModule } from 'primeng/table';
import { CalendarModule } from 'primeng/calendar';
import { SliderModule } from 'primeng/slider';
import { DialogModule } from 'primeng/dialog';
import { MultiSelectModule } from 'primeng/multiselect';
import { ContextMenuModule } from 'primeng/contextmenu';
import { ButtonModule } from 'primeng/button';
import { ToastModule } from 'primeng/toast';
import { InputTextModule } from 'primeng/inputtext';
import { ProgressBarModule } from 'primeng/progressbar';
import { DropdownModule } from 'primeng/dropdown';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TableModule,
        CalendarModule,
        SliderModule,
        DialogModule,
        MultiSelectModule,
        ContextMenuModule,
        DropdownModule,
        ButtonModule,
        ToastModule,
        InputTextModule,
        ProgressBarModule,
        HttpClientModule,
        FormsModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
export class AppModule { }


Output:

 

Example 2: In the following example, we have used local storage. So the state of the table will be accessible across different tabs.

  • app.component.html

HTML




<h1 style="color:green;text-align:center;">
    GeeksforGeeks
</h1>
<h3>Angular PrimeNG Table TableState</h3>
<h5>Basic</h5>
  
<p-table #dt [value]="tutorials" 
         dataKey="title" 
         [rowHover]="true" 
         [showCurrentPageReport]="true" 
         [filterDelay]="0"
         stateStorage="local" 
         stateKey="gfg-local" 
         [globalFilterFields]="['title', 'category', 'rating']">
    <ng-template pTemplate="caption">
        <div class="table-header">
            List of Tutorials
            <span class="p-input-icon-left">
                <i class="pi pi-search"></i>
                <input pInputText 
                       type="text" 
                       (input)="dt.filterGlobal($event.target.value, 'contains')"
                       placeholder="Global Search" />
            </span>
        </div>
    </ng-template>
    <ng-template pTemplate="header">
        <tr>
            <th style="width: 3rem"></th>
            <th pSortableColumn="title">
                Title 
                <p-sortIcon field="title"></p-sortIcon>
            </th>
            <th pSortableColumn="category">
                Category 
                <p-sortIcon field="category"></p-sortIcon>
            </th>
            <th pSortableColumn="rating">
                Rating 
                <p-sortIcon field="rating"></p-sortIcon>
            </th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" 
                 let-tutorial 
                 let-rowIndex="rowIndex">
        <tr class="p-selectable-row">
            <td>
                <p-tableCheckbox 
                    [value]="tutorial" 
                    [index]="rowIndex" 
                    [disabled]="tutorial.rating < 4">
                </p-tableCheckbox>
            </td>
            <td>
                <span class="p-column-title">
                    Title
                </span>
                {{ tutorial.title }}
            </td>
            <td>
                <span class="p-column-title">
                    Category
                </span>
                <span class="image-text">
                    {{ tutorial.category }}
                </span>
            </td>
            <td>
                <span class="p-column-title">
                    Rating
                </span>
                <span class="image-text">
                    {{ tutorial.rating }}
                </span>
            </td>
        </tr>
    </ng-template>
</p-table>


  • app.component.ts

Javascript




import { Component, OnInit, ViewChild } from '@angular/core';
import { Table } from 'primeng/table';
import { PrimeNGConfig } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
export class AppComponent {
    tutorials: Tutorial[];
    @ViewChild('dt') table: Table;
  
    constructor(private primengConfig: PrimeNGConfig) { }
  
    ngOnInit() {
        this.tutorials = [
            
                title: 'Queue'
                category: 'Data Structure'
                rating: 4 
            },
            
                title: 'Circularly LinkedList'
                category: 'Data Structure'
                rating: 5 
            },
            
                title: 'Doubly LinkedList'
                category: 'Data Structure'
                rating: 3 
            },
            
                title: 'Singly LinkedList'
                category: 'Data Structure'
                rating: 4 
            },
            
                title: 'Doubly Ended Queue'
                category: 'Data Structure'
                rating: 5 
            },
            
                title: 'Binary Search Tree'
                category: 'Data Structure'
                rating: 4 
            },
            
                title: 'Red Black Tree'
                category: 'Data Structure'
                rating: 5 
            },
            
                title: 'Breadth First Search'
                category: 'Graph'
                rating: 4 
            },
            
                title: "Floyd's Cycle"
                category: 'Algorithm', 
                rating: 4 
            },
            {
                title: 'Travelling Salesman Problem',
                category: 'Algorithm',
                rating: 5,
            },
            
                title: 'Bellman Ford', 
                category: 'Graph', 
                rating: 4 
            },
            
                title: 'KMP Algorithm', 
                category: 'String', 
                rating: 5 
            },
        ];
  
        this.primengConfig.ripple = true;
    }
}
export interface Tutorial {
    title?: string;
    category?: string;
    rating?: number;
}


  • app.module.ts

Javascript




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 { TableModule } from 'primeng/table';
import { CalendarModule } from 'primeng/calendar';
import { SliderModule } from 'primeng/slider';
import { DialogModule } from 'primeng/dialog';
import { MultiSelectModule } from 'primeng/multiselect';
import { ContextMenuModule } from 'primeng/contextmenu';
import { ButtonModule } from 'primeng/button';
import { ToastModule } from 'primeng/toast';
import { InputTextModule } from 'primeng/inputtext';
import { ProgressBarModule } from 'primeng/progressbar';
import { DropdownModule } from 'primeng/dropdown';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TableModule,
        CalendarModule,
        SliderModule,
        DialogModule,
        MultiSelectModule,
        ContextMenuModule,
        DropdownModule,
        ButtonModule,
        ToastModule,
        InputTextModule,
        ProgressBarModule,
        HttpClientModule,
        FormsModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
export class AppModule { }


Output:

 

Reference: http://primefaces.org/primeng/table/state



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads