Angular PrimeNG Table Responsive
Last Updated :
29 Sep, 2022
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 Table Responsive in Angular PrimeNG.
Angular PrimeNG Table Responsive is used to make the Table component responsive so that it could be perfect for all screen sizes. It makes the Table component more interactive.
Syntax:
<p-table responsiveLayout="scroll | stack">
...
</p-table>
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 Table Responsive. In this example, we are making the table responsive by making it scrollable.
app.component.html
< div class = "page-content" style = "height: calc(100vh - 149px)" >
< h2 style = "color: green" >GeeksforGeeks</ h2 >
< h4 >Angular PrimeNG Table Responsive</ h4 >
< p-table
responsiveLayout = "scroll"
[value]="tableData"
[scrollable]="true"
scrollHeight = "flex" >
< ng-template pTemplate = "header" >
< tr >
< th >First Name</ th >
< th >Last Name</ th >
< th >Age</ th >
< th >Age</ th >
< th >Age</ th >
< th >Age</ th >
< th >Age</ th >
< th >Age</ th >
< th >Age</ th >
</ tr >
</ ng-template >
< ng-template
pTemplate = "body"
let-rowData = "rowData"
let-people sortMode = "multiple" >
< tr >
< td >
{{ people.firstname }}
</ td >
< td >
{{ people.lastname }}
</ td >
< td >
{{ people.age }}
</ td >
< td >
{{ people.age }}
</ td >
< td >
{{ people.age }}
</ td >
< td >
{{ people.age }}
</ td >
< td >
{{ people.age }}
</ td >
< td >
{{ people.age }}
</ td >
< td >
{{ people.age }}
</ td >
</ tr >
</ ng-template >
</ p-table >
</ div >
|
app.component.ts
import { Component } from '@angular/core' ;
interface People {
firstname?: string;
lastname?: string;
age?: string;
}
@Component({
selector: 'app-root' ,
templateUrl: './app.component.html' ,
})
export class AppComponent {
tableData: People[] = [];
cols: any[] = [];
constructor() { }
ngOnInit() {
this .cols = [
{ field: 'firstname' , header: 'First Name' },
{ field: 'lastname' , header: 'Last Name' },
{ field: 'age' , header: 'Age' },
];
this .tableData = [
{
firstname: 'David' ,
lastname: 'ace' ,
age: '40' ,
},
{
firstname: 'AJne' ,
lastname: 'west' ,
age: '40' ,
},
{
firstname: 'Mak' ,
lastname: 'Lame' ,
age: '40' ,
},
{
firstname: 'Peter' ,
lastname: 'raw' ,
age: '40' ,
},
{
firstname: 'Kane' ,
lastname: 'James' ,
age: '40' ,
},
];
}
}
|
app.module.ts
import { NgModule } from '@angular/core' ;
import { BrowserModule } from '@angular/platform-browser' ;
import { FormsModule } from '@angular/forms' ;
import { BrowserAnimationsModule }
from '@angular/platform-browser/animations' ;
import { AppComponent } from './app.component' ;
import { TableModule } from 'primeng/table' ;
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
TableModule,
FormsModule,
],
declarations: [AppComponent],
bootstrap: [AppComponent],
})
export class AppModule { }
|
Output:
Example 2: Below is another example demonstrating the use of the Angular PrimeNG Table Responsive. In this example, we are using the stack responsive layout.
app.component.html
< div class = "page-content" style = "height: calc(100vh - 149px)" >
< h2 style = "color: green" >GeeksforGeeks</ h2 >
< h4 >Angular PrimeNG Table Responsive</ h4 >
< p-table
responsiveLayout = "stack"
[value]="tableData"
scrollHeight = "flex" >
< ng-template pTemplate = "header" >
< tr >
< th >First Name</ th >
< th >Last Name</ th >
< th >Age</ th >
</ tr >
</ ng-template >
< ng-template
pTemplate = "body"
let-rowData = "rowData"
let-people
sortMode = "multiple" >
< tr >
< td >
< span
class = "p-column-title" >
First Name
</ span >
{{ people.firstname }}
</ td >
< td >
< span
class = "p-column-title" >
last Name
</ span >
{{ people.lastname }}
</ td >
< td >
< span
class = "p-column-title" >
Age
</ span >
{{ people.age }}
</ td >
</ tr >
</ ng-template >
</ p-table >
</ div >
|
app.component.ts
import { Component } from '@angular/core' ;
interface People {
firstname?: string;
lastname?: string;
age?: string;
}
@Component({
selector: 'app-root' ,
templateUrl: './app.component.html' ,
})
export class AppComponent {
tableData: People[] = [];
cols: any[] = [];
constructor() { }
ngOnInit() {
this .cols = [
{ field: 'firstname' , header: 'First Name' },
{ field: 'lastname' , header: 'Last Name' },
{ field: 'age' , header: 'Age' },
];
this .tableData = [
{
firstname: 'David' ,
lastname: 'ace' ,
age: '40' ,
},
{
firstname: 'AJne' ,
lastname: 'west' ,
age: '40' ,
},
{
firstname: 'Mak' ,
lastname: 'Lame' ,
age: '40' ,
},
{
firstname: 'Peter' ,
lastname: 'raw' ,
age: '40' ,
},
{
firstname: 'Kane' ,
lastname: 'James' ,
age: '40' ,
},
];
}
}
|
app.module.ts
import { NgModule } from '@angular/core' ;
import { BrowserModule } from '@angular/platform-browser' ;
import { FormsModule } from '@angular/forms' ;
import { BrowserAnimationsModule }
from '@angular/platform-browser/animations' ;
import { AppComponent } from './app.component' ;
import { TableModule } from 'primeng/table' ;
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
TableModule,
FormsModule,
],
declarations: [AppComponent],
bootstrap: [AppComponent],
})
export class AppModule { }
|
Output:
Reference: https://primefaces.org/primeng/table/responsive
Share your thoughts in the comments
Please Login to comment...