Open In App

Angular PrimeNG Table Scroll Layout

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 Angular PrimeNG Table Scroll Layout.

The Table Component shows some data to the user in tabular form. To enable scroll layout for a table, set the responsiveLayout property to “scroll”. Resizable columns are not supported when the scroll mode is enabled.

Syntax:

<p-table [value]="persons" responsiveLayout="scroll">
    <ng-template pTemplate="header">
        <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Profession</th>
            ...
        </tr>
    </ng-template>
    <ng-template pTemplate="body" let-person>
        <tr>
            <td> {{ person.name }} </td>
            <td> {{ person.age }} </td>
            <td> {{ person.profession }} </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: This example shows a simple scroll layout in the Angular PrimeNG Table Component.

app.component.html




<div style="text-align: center">
    <h2 style="color: green">GeeksforGeeks</h2>
    <h4>Angular PrimeNG Table Scroll Layout</h4>
  
    <p-table [value]="persons" responsiveLayout="scroll">
        <ng-template pTemplate="header">
            <tr>
                <th>Name</th>
                <th>Age</th>
                <th>Profession</th>
                <th>Salary</th>
                <th>Mobile</th>
                <th>Gender</th>
                <th>Is Suspect</th>
            </tr>
        </ng-template>
        <ng-template pTemplate="body" let-person>
            <tr>
                <td> {{ person.name }} </td>
                <td> {{ person.age }} </td>
                <td> {{ person.profession }} </td>
                <td> {{ person.salary }} </td>
                <td> {{ person.mobile }} </td>
                <td> {{ person.gender }} </td>
                <td> {{ person.isSuspect }} </td>
            </tr>
        </ng-template>
    </p-table>
</div>


app.component.ts




import { Component } from '@angular/core';
  
interface Person {
    name: String,
    age: Number,
    salary: Number,
    profession: String,
    mobile: String,
    gender: String,
    isSuspect: boolean,
}
  
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
  
export class AppComponent {
    persons: Person[] = [
        {
            name: "James",
            age: 21,
            profession: "Doctor",
            mobile: "8731******90",
            gender: "Male",
            isSuspect: true,
            salary: 24000
  
        },
        {
            name: "Sam",
            age: 18,
            profession: "Marketer",
            mobile: "8989******98",
            gender: "Male",
            isSuspect: false,
            salary: 25000
        },
        {
            name: "Thomas",
            age: 27,
            profession: "Businessman",
            mobile: "9645******11",
            gender: "Male",
            isSuspect: true,
            salary: 55000
        },
        {
            name: "Kate",
            age: 24,
            profession: "Teacher",
            mobile: "9345******46",
            gender: "Female",
            isSuspect: false,
            salary: 20000
        },
        {
            name: "Kumar",
            age: 17,
            profession: "Student",
            mobile: "7824******78",
            gender: "Male",
            isSuspect: false,
            salary: 4000
        },
        {
            name: "Ethan",
            age: 22,
            profession: "Delivery Boy",
            mobile: "9090******90",
            gender: "Male",
            isSuspect: true,
            salary: 13000
        },
    ];
}


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 { ButtonModule } from 'primeng/button';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TableModule,
        ButtonModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
  
export class AppModule { }


Output:

 

Example 2: In this example, we used the scroll layout for the table. Here the rows where the isSuspect variable is true have the background color set to red.

app.component.html




<div style="text-align: center">
    <h2 style="color: green">GeeksforGeeks</h2>
    <h4>Angular PrimeNG Table Scroll Layout</h4>
  
    <p-table [value]="persons" responsiveLayout="scroll">
        <ng-template pTemplate="header">
            <tr>
                <th>Name</th>
                <th>Age</th>
                <th>Profession</th>
                <th>Salary</th>
                <th>Mobile</th>
                <th>Gender</th>
                <th>Is Suspect</th>
            </tr>
        </ng-template>
        <ng-template pTemplate="body" let-person>
            <tr [ngClass]="{'suspect': person.isSuspect}">
                <td> {{ person.name }} </td>
                <td> {{ person.age }} </td>
                <td> {{ person.profession }} </td>
                <td> {{ person.salary }} </td>
                <td> {{ person.mobile }} </td>
                <td> {{ person.gender }} </td>
                <td> {{ person.isSuspect }} </td>
            </tr>
        </ng-template>
    </p-table>
</div>


app.component.ts




import { Component } from '@angular/core';
  
interface Person {
    name: String,
    age: Number,
    salary: Number,
    profession: String,
    mobile: String,
    gender: String,
    isSuspect: boolean,
}
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styles: [
        `tr.suspect{
            background-color: red;
            color: white;
        }
        `
    ]
})
  
export class AppComponent {
    persons: Person[] = [
        {
            name: "James",
            age: 21,
            profession: "Doctor",
            mobile: "8731******90",
            gender: "Male",
            isSuspect: true,
            salary: 24000
  
        },
        {
            name: "Sam",
            age: 18,
            profession: "Marketer",
            mobile: "8989******98",
            gender: "Male",
            isSuspect: false,
            salary: 25000
        },
        {
            name: "Thomas",
            age: 27,
            profession: "Businessman",
            mobile: "9645******11",
            gender: "Male",
            isSuspect: true,
            salary: 55000
        },
        {
            name: "Kate",
            age: 24,
            profession: "Teacher",
            mobile: "9345******46",
            gender: "Female",
            isSuspect: false,
            salary: 20000
        },
        {
            name: "Kumar",
            age: 17,
            profession: "Student",
            mobile: "7824******78",
            gender: "Male",
            isSuspect: false,
            salary: 4000
        },
        {
            name: "Ethan",
            age: 22,
            profession: "Delivery Boy",
            mobile: "9090******90",
            gender: "Male",
            isSuspect: true,
            salary: 13000
        },
    ];
}


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 { ButtonModule } from 'primeng/button';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TableModule,
        ButtonModule
    ],
    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