Angular PrimeNG表可滚动表的列宽

Angular PrimeNG表可滚动表的列宽

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

Angular PrimeNG Table Column Widths of a Scrollable Table是用来设置可滚动的Table组件的列宽。它使表组件在不同屏幕尺寸的设备上更具互动性和响应性。当表格处于可滚动模式时,它使用弹性布局,所以在设置表格的列宽时,有一些要点需要记住。这些要点列在下面。

  • 当表格处于仅垂直滚动模式时,使用min-width属性,以便在更大的屏幕上有更多可用空间时,列可以自行调整,而在更小的屏幕上,为了响应,将显示水平滚动条。
  • 在水平滚动的情况下,使用width属性而不是min-width
  • 当仅在垂直滚动模式下,使用flex属性禁用增长和收缩,而在水平滚动模式下则不需要这样做,因为在水平滚动模式下列不会增长或收缩。

语法:

<p-table>
  <ng-template pTemplate="body">
    <tr>
      <td style="flex: 0 0 15rem">
        ...
      </td>
      <td style="flex: 0 0 10rem">
        ...
      </td>
      <td style="flex: 0 0 15rem">
        ...
      </td>
    </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表可滚动表的列宽

Project Structure

例子1:下面是一个简单的例子,演示了可滚动表的Angular PrimeNG表列宽度的使用。这里我们使用CSS的flex属性来设置列的宽度。

<h2 style="color: green">GeeksforGeeks</h2>
<h4>
    Angular PrimeNG Table Column
    Widths of a Scrollable Table
</h4>
  
<p-table [value]="tableData" 
         [scrollable]="true" 
         scrollHeight="400px">
    <ng-template pTemplate="header">
        <tr>
            <th style="flex: 0 0 15rem">
                First Name
            </th>
            <th style="flex: 0 0 10rem">
                Last Name
            </th>
            <th style="flex: 0 0 15rem">
                Age
            </th>
        </tr>
    </ng-template>
  
    <ng-template pTemplate="body" let-people>
        <tr>
            <td style="flex: 0 0 15rem">
                {{ people.firstname }}
            </td>
            <td style="flex: 0 0 10rem">
                {{ people.lastname }}
            </td>
            <td style="flex: 0 0 15rem">
                {{ 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 { 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 { }

输出:

Angular PrimeNG表可滚动表的列宽

例子2:下面是另一个例子,演示了可滚动表的Angular PrimeNG表列宽度的使用。在这里,我们使用max-width 属性来设置表列的宽度。

<h2 style="color: green">GeeksforGeeks</h2>
<h4>
    Angular PrimeNG Table Column 
    Widths of a Scrollable Table
</h4>
  
<p-table [value]="tableData" 
         [scrollable]="true" 
         scrollHeight="400px">
    <ng-template pTemplate="header">
        <tr>
            <th style="max-width:400px">
                First Name
            </th>
            <th style="max-width:400px">
                Last Name
            </th>
            <th style="max-width:100px">
                Age
            </th>
        </tr>
    </ng-template>
  
    <ng-template pTemplate="body" let-people>
        <tr>
            <td style="max-width:400px">
                {{ people.firstname }}
            </td>
            <td style="max-width:400px">
                {{ people.lastname }}
            </td>
            <td style="max-width:100px">
                {{ 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 { 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 { }

输出:

Angular PrimeNG表可滚动表的列宽

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程