Angular PrimeNG表的水平滚动
Angular PrimeNG是一个开源的库,由原生的Angular UI组件组成,用来做伟大的造型,这个框架用来制作响应式网站,非常容易。在这篇文章中,我们将看到Angular PrimeNG表的水平滚动。
表组件用于以表格的形式向用户显示一些数据。当表格超过视口的宽度时,可以通过设置scrollable属性为 “true “和scrollDirection属性为 “horizontal “来启用水平滚动,同时给列提供固定宽度。
语法:
<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>
HTML
创建Angular应用程序并安装模块:
第1步:使用以下命令创建一个Angular应用程序。
ng new myapp
JavaScript
第2步:创建你的项目文件夹即myapp后,使用以下命令移动到它。
cd myapp
JavaScript
第3步在你给定的目录中安装PrimeNG。
npm install primeng --save
npm install primeicons --save
JavaScript
项目结构:完成上述步骤后,项目结构将如下所示。
Project Structure
例子1:这个例子说明了如何在一个宽度超过视口的表格上启用水平滚动。
<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>
HTML
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
},
];
}
}
JavaScript
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 { }
JavaScript
运行应用程序:
从你的项目根部执行下面的命令来运行angular应用程序。
ng serve --open
JavaScript
输出:
例子2:这个例子显示了一个具有斑马线风格的水平滚动表。
<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>
HTML
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
},
];
}
}
JavaScript
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 { }
JavaScript
输出: