Angular PrimeNG Table Horizontal Scrolling
Angular PrimeNG is an open-source library that consists 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 Angular PrimeNG Table Horizontal Scrolling.
The Table Component is used to show some data to the user in the tabular form. When the table overflows the width of the viewport, the horizontal scrolling can be enabled by setting the scrollable property to “true” and the scrollDirection property to “horizontal” along with giving fixed width to the columns.
Syntax:
<p-table [value]="cars" [scrollable]="true"
scrollDirection="horizontal">
<ng-template pTemplate="header">
<tr>
<th style="width: 300px">Car</th>
<th style="width: 300px">Company</th>
<th style="width: 300px">Vehicle Number</th>
...
</tr>
</ng-template>
<ng-template pTemplate="body" let-car>
<tr>
<td style="width: 300px">{{car.name}}</td>
<td style="width: 300px">{{car.company}}</td>
<td style="width: 300px">{{car.number}}</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 illustrates how to enable horizontal scrolling on a table whose width is overflowing the viewport.
app.component.html
< div style = "text-align: center" >
< h2 style = "color: green" >GeeksforGeeks</ h2 >
< h4 >Angular PrimeNG Table Horizontal Scrolling</ h4 >
< p-table
[value]="cars"
[scrollable]="true"
scrollDirection = "horizontal" >
< ng-template pTemplate = "header" >
< tr >
< th style = "width: 300px" >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" >{{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' ,
})
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 { }
|
Run the Application:
Execute the below command from the root of your project to run the angular application.
ng serve --open
Output:
Example 2: This example shows a horizontal scrolling table with zebra styling.
app.component.html
< div style = "text-align: center" >
< h2 style = "color: green" >GeeksforGeeks</ h2 >
< h4 >Angular PrimeNG Table Horizontal Scrolling</ h4 >
< p-table
[value]="cars"
[scrollable]="true"
scrollDirection = "horizontal" >
< ng-template pTemplate = "header" >
< tr >
< th style = "width: 300px" >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 [ngClass]="{ 'even-row' : car.id %2 == 0}">
< td style = "width: 300px" >{{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 {
id: Number,
name: String,
company: String,
number: String,
manufacturedYear: Number,
kmDriven: Number,
price: String,
rating: Number
}
@Component({
selector: 'app-root' ,
templateUrl: './app.component.html' ,
styles: [
`tr.even-row{
background-color: green;
color: white;
}
`
]
})
export class AppComponent {
cars: Car[] = [];
ngOnInit() {
this .cars = [
{
id: 1,
name: "Creta" ,
company: "Hyundai" ,
number: "MH01BHXX01" ,
manufacturedYear: 2016,
kmDriven: 23456,
price: "11 Lakhs" ,
rating: 4.0
},
{
id: 2,
name: "Audi Q7" ,
company: "Audi" ,
number: "UP76APXXX2" ,
manufacturedYear: 2006,
kmDriven: 11342,
price: "90.2 Lakhs" ,
rating: 4.7
},
{
id: 3,
name: "Venue" ,
company: "Hyundai" ,
number: "MH11AHXX01" ,
manufacturedYear: 2019,
kmDriven: 45456,
price: "7.8 Lakhs" ,
rating: 3.5
},
{
id: 4,
name: "Audi A4" ,
company: "Hyundai" ,
number: "BR01SD1XX0" ,
manufacturedYear: 1997,
kmDriven: 18446,
price: "49.4 Lakhs" ,
rating: 4.5
},
{
id: 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:
Reference: http://primefaces.org/primeng/table
Last Updated :
07 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...