Angular PrimeNG表的动态列

Angular PrimeNG表的动态列

Angular PrimeNG是一个开源框架,拥有丰富的原生Angular UI组件,可用于出色的造型,这个框架用于制作响应式网站,非常方便。这篇文章将向我们展示如何在Angular PrimeNG中使用表动态列。

Angular PrimeNG Table Dynamic Columns是用来动态设置和渲染Table组件的列的。有两种方法来呈现动态列,使用组件范围内的cols属性并将其绑定到ngFor指令,另一种方法是将cols数组绑定到columns属性,然后定义一个模板变量来访问你的模板。

语法:

  • 在app.component.ts文件中。
this.cols = [
    {
        field: 'fieldName1',
        header: 'headerName1'
    },
    {
        field: 'fieldName2',
        header: 'headerName2'
    },
];
  • 在app.component.html文件中。
<p-table>
    <ng-template pTemplate="header" let-columns>
        <tr>
            <th *ngFor="let col of columns">
            {{ col.header }}
        </th>
    </tr>
</ng-template>
</p - table >

创建Angular应用程序和模块安装。

第1步:使用以下命令创建一个Angular应用程序。

ng new appname

第2步:创建你的项目文件夹即appname后,使用以下命令移动到它。

cd appname

第3步在你给定的目录中安装PrimeNG。

npm install primeng --save
npm install primeicons --save

项目结构:它将看起来像如下。

Angular PrimeNG表的动态列

例子1:下面是一个简单的例子,演示Angular PrimeNG表动态列的使用。

  • app.component.html
<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>
  • 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',
            },
            {
                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',
            },
        ];
    }
}
  • app.module.ts
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 { }

输出:

Angular PrimeNG表的动态列

例子2:下面是另一个例子,演示Angular PrimeNG表动态列的使用。在这个例子中,我们使用app.component.ts组件的cols属性来动态地渲染列。

  • app.component.html
<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>
  • 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',
            },
            {
                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',
            },
        ];
    }
}
  • app.module.ts
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 { }

输出:

Angular PrimeNG表的动态列

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程