Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • AngularJS Tutorial
  • AngularJS Directives
  • AngularJS Functions
  • AngularJS Filters
  • AngularJS Examples
  • AngularJS Interview Questions
  • Angular ngx Bootstrap
  • AngularJS Cheat Sheet
  • AngularJS PrimeNG
  • JavaScript
  • Web Technology
Open In App
Next Article:
Angular PrimeNG Tag Styling
Next article icon

Angular PrimeNG Table Styling

Last Updated : 07 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
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 Table Styling in Angular PrimeNG.

The Table component shows some data to the user in tabular form. The table can be styled by adding classes to different sections of the table based on some conditions.

Syntax:

<p-table [value]="books" responsiveLayout="scroll">      <ng-template pTemplate="header">          <tr>              <th>Name</th>              <th>Author</th>          </tr>         </ng-template>         <ng-template pTemplate="body" let-book>             <tr [ngClass]="{Condition-Check-Here}">                 <td>{{book.name}}</td>                 <td>{{book.author}}</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: Below is a simple example that illustrates the use of the Angular PrimeNG Table Styling, here we applied a custom CSS class to the table rows having a year value greater than 2000.

app.component.html
<div style="text-align: center">     <h2 style="color: green">GeeksforGeeks</h2>     <h4>Angular PrimeNG Table Styling</h4>      <p-table [value]="books" responsiveLayout="scroll">         <ng-template pTemplate="header">             <tr>                 <th>Name</th>                 <th>Author</th>                 <th>Year</th>             </tr>         </ng-template>         <ng-template pTemplate="body" let-book>             <tr [ngClass]="{'after-2000': book.year > 2000}">                 <td>{{book.name}}</td>                 <td>{{book.author}}</td>                 <td>{{book.year}}</td>             </tr>         </ng-template>     </p-table> </div> 
app.component.ts
import { Component } from '@angular/core';  interface Book {     name: String,     author: String,     year: Number }  @Component({     selector: 'app-root',     templateUrl: './app.component.html',     styles: [         `tr.after-2000{             background-color: green;             color: white;         }`     ] })  export class AppComponent {     books: Book[] = [];      ngOnInit() {         this.books = [             {                 name: "Clean Code",                 author: "Robert Cecil Martin",                 year: 2008             },             {                 name: "Introduction to Algorithms",                 author: "Thomas H Corman",                 year: 1989             },             {                 name: "Refactoring",                 author: "Martin Fowler",                 year: 1999             },             {                 name: "Code Complete",                 author: "Steve McConnell",                 year: 1993             },             {                 name: "Programming Pearls",                 author: "John Bentley",                 year: 1986             },             {                 name: "The Clean Coder",                 author: "Robert Cecil Martin",                 year: 2011             },             {                 name: "Coders at Work",                 author: "Peter Seibel",                 year: 2009             },             {                 name: "Effective Java",                 author: "Joshua Bloch",                 year: 2001             },             {                 name: "Head First Java",                 author: "Bert Bates",                 year: 2003             }         ];     }  } 
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: We can also check for two conditions on a single element. In this example, we checked for two conditions on the year column and change the color of the text to green if the year is greater than 2000 else the color is changed to red.

app.component.html
<div style="text-align: center">     <h2 style="color: green">GeeksforGeeks</h2>     <h4>Angular PrimeNG Table Styling</h4>      <p-table [value]="books" responsiveLayout="scroll">         <ng-template pTemplate="header">             <tr>                 <th>Name</th>                 <th>Author</th>                 <th>Year</th>             </tr>         </ng-template>         <ng-template pTemplate="body" let-book>             <tr>                 <td>{{book.name}}</td>                 <td>{{book.author}}</td>                 <td [ngClass]="{                     'after-2000': book.year > 2000,                      'before-2000': book.year<= 2000}">{{book.year}}                 </td>             </tr>         </ng-template>     </p-table> </div> 
app.component.ts
import { Component } from '@angular/core';  interface Book {     name: String,     author: String,     year: Number }  @Component({     selector: 'app-root',     templateUrl: './app.component.html',     styles: [         `td.after-2000{             color: green;         }         td.before-2000{             color: red;         }         `     ] })  export class AppComponent {     books: Book[] = [];      ngOnInit() {         this.books = [             {                 name: "Clean Code",                 author: "Robert Cecil Martin",                 year: 2008             },             {                 name: "Introduction to Algorithms",                 author: "Thomas H Corman",                 year: 1989             },             {                 name: "Refactoring",                 author: "Martin Fowler",                 year: 1999             },             {                 name: "Code Complete",                 author: "Steve McConnell",                 year: 1993             },             {                 name: "Programming Pearls",                 author: "John Bentley",                 year: 1986             },             {                 name: "The Clean Coder",                 author: "Robert Cecil Martin",                 year: 2011             },             {                 name: "Coders at Work",                 author: "Peter Seibel",                 year: 2009             },             {                 name: "Effective Java",                 author: "Joshua Bloch",                 year: 2001             },             {                 name: "Head First Java",                 author: "Bert Bates",                 year: 2003             }         ];     }  } 
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


Next Article
Angular PrimeNG Tag Styling

W

writer01
Improve
Article Tags :
  • Web Technologies
  • AngularJS
  • Angular-PrimeNG
  • PrimeNG-Data

Similar Reads

  • Angular PrimeNG Table Style
    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 TreeTable Column Resize in Angular PrimeNG. Angular PrimeNG Tab
    4 min read
  • Angular PrimeNG Table Sorting
    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 Sorting in Angular PrimeNG. Angular PrimeNG Table Sorting
    5 min read
  • Angular PrimeNG TabMenu Styling
    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.
    4 min read
  • Angular PrimeNG TabView Styling
    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. In this article, we will see the Angular PrimeNG TabView Styling.  The TabView Component is used to
    3 min read
  • Angular PrimeNG Tag Styling
    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. In this article, we will see Angular PrimeNG Tag Properties. A tag component is used to create tags
    3 min read
  • Angular PrimeNG Tree Styling
    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.
    5 min read
  • Angular PrimeNG Table Scrolling
    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 Scrolling in Angular PrimeNG. Angular PrimeNG Table Scrol
    5 min read
  • Angular PrimeNG Table Sections
    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 Column Grouping in Angular PrimeNG. Angular PrimeNG Table
    5 min read
  • Angular PrimeNG Table Sticky
    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. In this article, we will see how to use Table Sticky in Angular PrimeNG. The Table Sticky implements
    5 min read
  • Angular PrimeNG Table Size
    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. In this article, we will see how to use Table Size in Angular PrimeNG. Angular PrimeNG Table Size ad
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences