Open In App

Angular PrimeNG Table pFrozenColumn Properties

Last Updated : 07 Sep, 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. This article will show us how to use pFrozenColumn Properties in Angular PrimeNG.

The Table component shows some data to the user in tabular form. The pFrozenColumn properties are used to freeze a column. There are two properties for the pFrozenColumn directive which are listed below:

  • frozen: This is a boolean property used to state whether the column is frozen or not. 
  • alignFrozen: This property defined the alignment of the frozen column. It defines whether the frozen column should stick to the left or the right of the table. The default value is left.

Syntax:

<p-table [value]="cars" [scrollable]="true" scrollDirection="horizontal">
    <ng-template pTemplate="header">
        <tr>
            <th style="width: 300px" pFrozenColumn>
                Car
            </th>
            <th style="width: 300px">Company</th>
                ...
        </tr>
    </ng-template>
    <ng-template pTemplate="body" let-car>
        <tr>
            <td style="width: 300px" pFrozenColumn>
               {{car.name}}
            </td>
            <td style="width: 300px">{{car.company}}</td>
                ...
            </tr>
    </ng-template>
</p-table>

Creating Angular application and Installing the Modules:

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

ng new myapp

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

cd myapp

Step 3: Install PrimeNG in your given directory.

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

Project Structure: After completing the above steps the project structure will look like the following:

Project Structure

Example 1: In this example, we used the pFrozenColumn directive to freeze the first column of the table.

app.component.html




<div style="text-align: center">
    <h2 style="color: green">GeeksforGeeks</h2>
    <h4>Angular PrimeNG Table pFrozenColumn Properties</h4>
  
    <p-table 
        [value]="cars" 
        [scrollable]="true" 
        scrollDirection="horizontal">
        <ng-template pTemplate="header">
            <tr>
                <th 
                    style="width: 300px" 
                    pFrozenColumn 
                    class="frozen-col">
                    Car
                </th>
                <th style="width: 300px">Company</th>
                <th style="width: 300px">Vehicle Number</th>
                <th style="width: 300px">Manufactured year</th>
                <th style="width: 300px">KM Driven</th>
                <th style="width: 300px">Price</th>
                <th style="width: 300px">Rating</th>
            </tr>
        </ng-template>
        <ng-template pTemplate="body" let-car>
            <tr>
                <td 
                    style="width: 300px" 
                    pFrozenColumn 
                    class="frozen-col">
                    {{car.name}}
                </td>
                <td style="width: 300px">{{car.company}}</td>
                <td style="width: 300px">{{car.number}}</td>
                <td style="width: 300px">{{car.manufacturedYear}}</td>
                <td style="width: 300px">{{car.kmDriven}}</td>
                <td style="width: 300px">{{car.price}}</td>
                <td style="width: 300px">{{car.rating}}</td>
            </tr>
        </ng-template>
    </p-table>
</div>


app.component.ts




import { Component } from '@angular/core';
  
interface Car {
    name: String,
    company: String,
    number: String,
    manufacturedYear: Number,
    kmDriven: Number,
    price: String,
    rating: Number
}
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styles: [
        `.frozen-col{
            font-weight: bold;
        }`
    ]
})
  
export class AppComponent {
    cars: Car[] = [];
  
    ngOnInit() {
        this.cars = [
            {
                name: "Creta",
                company: "Hyundai",
                number: "MH01BHXX01",
                manufacturedYear: 2016,
                kmDriven: 23456,
                price: "11 Lakhs",
                rating: 4.0
            },
            {
                name: "Audi Q7",
                company: "Audi",
                number: "UP76APXXX2",
                manufacturedYear: 2006,
                kmDriven: 11342,
                price: "90.2 Lakhs",
                rating: 4.7
            },
            {
                name: "Venue",
                company: "Hyundai",
                number: "MH11AHXX01",
                manufacturedYear: 2019,
                kmDriven: 45456,
                price: "7.8 Lakhs",
                rating: 3.5
            },
            {
                name: "Audi A4",
                company: "Hyundai",
                number: "BR01SD1XX0",
                manufacturedYear: 1997,
                kmDriven: 18446,
                price: "49.4 Lakhs",
                rating: 4.5
            },
            {
                name: "Audi e-tron",
                company: "Audi",
                number: "MP11BPXX00",
                manufacturedYear: 2020,
                kmDriven: 9901,
                price: "1.2 Crore",
                rating: 4.9
            },
        ];
    }
}


app.module.ts




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


Output:

 

Example 2: In this example, we used Toggle Buttons to free and unfreeze the columns dynamically.

app.component.html




<div style="text-align: center">
    <h2 style="color: green">GeeksforGeeks</h2>
    <h4>Angular PrimeNG Table pFrozenColumn Properties</h4>
  
    <p-toggleButton 
        class="mr-3"
        [(ngModel)]="isNameFreeze" 
        [onLabel]="'Unfreeze Name'" 
        offLabel="Freeze Name" 
        [onIcon]="'pi pi-lock'" 
        [offIcon]="'pi pi-lock-open'" >
    </p-toggleButton>
    <p-toggleButton 
        class="ml-3" 
        [(ngModel)]="isPriceFreeze" 
        [onLabel]="'Unfreeze Price'" 
        offLabel="Freeze Price" 
        [onIcon]="'pi pi-lock'" 
        [offIcon]="'pi pi-lock-open'">
    </p-toggleButton>
  
    <p-table 
        [value]="cars" 
        [scrollable]="true" 
        scrollDirection="horizontal">
        <ng-template pTemplate="header">
            <tr>
                <th 
                    style="width: 300px" 
                    pFrozenColumn 
                    [frozen]="isNameFreeze" 
                    [ngClass]="{'frozen' : isNameFreeze}">
                    Car
                </th>
                <th style="width: 300px">Company</th>
                <th style="width: 300px">Vehicle Number</th>
                <th style="width: 300px">Manufactured year</th>
                <th style="width: 300px">KM Driven</th>
                <th style="width: 300px">Rating</th>
                <th 
                    style="width: 300px" 
                    pFrozenColumn 
                    [frozen]="isPriceFreeze" 
                    alignFrozen="right" 
                    [ngClass]="{'frozen' : isPriceFreeze}">
                    Price
                </th>
            </tr>
        </ng-template>
        <ng-template pTemplate="body" let-car>
            <tr>
                <td 
                    style="width: 300px" 
                    pFrozenColumn 
                    [frozen]="isNameFreeze" 
                    [ngClass]="{'frozen' : isNameFreeze}">
                    {{car.name}}
                </td>
                <td style="width: 300px">{{car.company}}</td>
                <td style="width: 300px">{{car.number}}</td>
                <td style="width: 300px">{{car.manufacturedYear}}</td>
                <td style="width: 300px">{{car.kmDriven}}</td>
                <td style="width: 300px">{{car.rating}}</td>
                <td 
                    style="width: 300px" 
                    pFrozenColumn 
                    [frozen]="isPriceFreeze" 
                    alignFrozen="right" 
                    [ngClass]="{'frozen' : isPriceFreeze}">
                    {{car.price}}
                </td>
            </tr>
        </ng-template>
    </p-table>
</div>


app.component.ts




import { Component } from '@angular/core';
  
interface Car {
    name: String,
    company: String,
    number: String,
    manufacturedYear: Number,
    kmDriven: Number,
    price: String,
    rating: Number
}
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styles: [
        `.frozen{
            font-weight: bold;
        }`
    ]
})
  
export class AppComponent {
  
    isNameFreeze: boolean = false;
    isPriceFreeze: boolean = false;
    cars: Car[] = [];
  
    ngOnInit() {
        this.cars = [
            {
                name: "Creta",
                company: "Hyundai",
                number: "MH01BHXX01",
                manufacturedYear: 2016,
                kmDriven: 23456,
                price: "11 Lakhs",
                rating: 4.0
            },
            {
                name: "Audi Q7",
                company: "Audi",
                number: "UP76APXXX2",
                manufacturedYear: 2006,
                kmDriven: 11342,
                price: "90.2 Lakhs",
                rating: 4.7
            },
            {
                name: "Venue",
                company: "Hyundai",
                number: "MH11AHXX01",
                manufacturedYear: 2019,
                kmDriven: 45456,
                price: "7.8 Lakhs",
                rating: 3.5
            },
            {
                name: "Audi A4",
                company: "Hyundai",
                number: "BR01SD1XX0",
                manufacturedYear: 1997,
                kmDriven: 18446,
                price: "49.4 Lakhs",
                rating: 4.5
            },
            {
                name: "Audi e-tron",
                company: "Audi",
                number: "MP11BPXX00",
                manufacturedYear: 2020,
                kmDriven: 9901,
                price: "1.2 Crore",
                rating: 4.9
            },
        ];
    }
  
}


app.module.ts




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


Output:

 

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads