Open In App

Angular PrimeNG Table Dynamic Columns

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 Dynamic Columns in Angular PrimeNG.

Angular PrimeNG Table Dynamic Columns is used to set and render the columns of the Table component dynamically. There are two ways to render dynamic columns, using the cols property in the scope of the component and binding it to the ngFor directive, Another alternative is binding the cols array to the columns property and then defining a template variable to access it within your templates.



Syntax:

this.cols = [
    {
        field: 'fieldName1',
        header: 'headerName1'
    },
    {
        field: 'fieldName2',
        header: 'headerName2'
    },
];
<p-table>
    <ng-template pTemplate="header" let-columns>
        <tr>
            <th *ngFor="let col of columns">
            {{ col.header }}
        </th>
    </tr>
</ng-template>
</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:

 

Example 1: Below is a simple example demonstrating the use of the Angular PrimeNG Table Dynamic Columns.




<h2 style="color: green">
    GeeksforGeeks
</h2>
<h4>
    Angular PrimeNG Table Dynamic Columns
</h4>
  
<p-table
    #myTab
    [value]="tableData"
    [scrollable]="true"
    scrollHeight="400px"
    [columns]="cols"
>
    <ng-template pTemplate="header" 
                 let-columns>
        <tr>
            <th *ngFor="let col of columns">
                {{ col.header }}
            </th>
        </tr>
    </ng-template>
  
    <ng-template pTemplate="body" 
                 let-people>
        <tr>
            <td>
                {{ people.firstname }}
            </td>
            <td>
                {{ people.lastname }}
            </td>
            <td>
                {{ people.age }}
            </td>
        </tr>
    </ng-template>
</p-table>




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',
            },
            {
                firstname: 'Peter',
                lastname: 'raw',
                age: '40',
            },
            {
                firstname: 'Kane',
                lastname: 'James',
                age: '40',
            },
            {
                firstname: 'Peter',
                lastname: 'raw',
                age: '40',
            },
            {
                firstname: 'Kane',
                lastname: 'James',
                age: '40',
            },
            {
                firstname: 'Peter',
                lastname: 'raw',
                age: '40',
            },
            {
                firstname: 'Kane',
                lastname: 'James',
                age: '40',
            },
            {
                firstname: 'Peter',
                lastname: 'raw',
                age: '40',
            },
            {
                firstname: 'Kane',
                lastname: 'James',
                age: '40',
            },
        ];
    }
}




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

Output:

 

Example 2: Below is another example demonstrating the use of the Angular PrimeNG Table Dynamic Columns.  In this example, we use the cols property of the app.component.ts component to render the columns dynamically.




<h2 style="color: green">
    GeeksforGeeks
</h2>
<h4>
    Angular PrimeNG Table Dynamic Columns
</h4>
  
<p-table 
    #myTab 
    [value]="tableData" 
    [scrollable]="true" 
    scrollHeight="400px"
>
    <ng-template pTemplate="header">
        <tr>
            <th *ngFor="let col of cols">
                {{ col.header }}
            </th>
        </tr>
    </ng-template>
  
    <ng-template pTemplate="body" 
                 let-people>
        <tr>
            <td>
                {{ people.firstname }}
            </td>
            <td>
                {{ people.lastname }}
            </td>
            <td>
                {{ people.age }}
            </td>
        </tr>
    </ng-template>
</p-table>




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',
            },
            {
                firstname: 'Peter',
                lastname: 'raw',
                age: '40',
            },
            {
                firstname: 'Kane',
                lastname: 'James',
                age: '40',
            },
            {
                firstname: 'Peter',
                lastname: 'raw',
                age: '40',
            },
            {
                firstname: 'Kane',
                lastname: 'James',
                age: '40',
            },
            {
                firstname: 'Peter',
                lastname: 'raw',
                age: '40',
            },
            {
                firstname: 'Kane',
                lastname: 'James',
                age: '40',
            },
            {
                firstname: 'Peter',
                lastname: 'raw',
                age: '40',
            },
            {
                firstname: 'Kane',
                lastname: 'James',
                age: '40',
            },
        ];
    }
}




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

Output:

 

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


Article Tags :