Angular PrimeNG表的加载状态

Angular PrimeNG表的加载状态

Angular PrimeNG是一个开源框架,拥有丰富的原生Angular UI组件,可用于出色的造型,该框架可用于制作响应式网站,非常容易。在这篇文章中,我们将看到Angular PrimeNG表加载状态。

表组件以表格的形式向用户显示一些数据。表组件的loading属性被用来向用户显示一个旋转图标,直到表数据在后台加载。loadingIcon属性可以用来设置一个不同的图标来显示,以指示数据的加载情况。

语法:

<p-table 
    [value]="items" 
    [scrollable]="true" 
    loadingIcon="..." 
    [loading]="...">

    <ng-template pTemplate="header">
        ...
    </ng-template>

    <ng-template pTemplate="body" let-item>
        ...
    </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

项目结构:完成上述步骤后,项目结构将如下所示。

Angular PrimeNG表的加载状态

Project Structure

例子1:本文展示了Angular PrimeNG中表格组件的基本加载状态。

<div style="text-align: center">
    <h2 style="color: green">GeeksforGeeks</h2>
    <h4>Angular PrimeNG Table Loading Status</h4>
  
    <p-table 
        [value]="items" 
        [scrollable]="true" 
        [loading]="isLoading">
        <ng-template pTemplate="header">
            <tr>
                <th>S.No</th>
                <th>Item</th>
                <th>Count</th>
            </tr>
        </ng-template>
        <ng-template pTemplate="body" let-item>
            <tr>
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>{{item.count}}</td>
                  
            </tr>
        </ng-template>
    </p-table>
  
    <button 
        pButton 
        label="Load" 
        (click)="onClick()" 
        style="
            position: absolute; 
            bottom: 50px; 
            right: 0
        ">
    </button>
</div>
HTML
import { Component } from '@angular/core';
  
interface Item {
    id: Number,
    name: String,
    count: Number
}
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
  
export class AppComponent {
  
    isLoading: boolean = true;
  
    items: Item[] = []
  
    ngOnInit() {
  
        this.isLoading = true;
  
        setTimeout(() => {
            this.items = [
                {
                    id: 1,
                    name: "Pen",
                    count: 9
                },
                {
                    id: 2,
                    name: "Rubber Bands",
                    count: 14
                },
                {
                    id: 3,
                    name: "Carrybag",
                    count: 1
                },
                {
                    id: 4,
                    name: "Spoon",
                    count: 1
                },
                {
                    id: 5,
                    name: "Perfume",
                    count: 2
                },
                {
                    id: 6,
                    name: "Pencil",
                    count: 11
                },
                {
                    id: 7,
                    name: "NoteBook",
                    count: 3
                },
                {
                    id: 8,
                    name: "Facewash",
                    count: 1
                },
            ];
  
            // Set isLoading to false after 1 seconds
            this.isLoading = false;
        }, 1000);
  
    }
    //
    onClick() {
        this.items = [];
        this.ngOnInit();
    }
  
}
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';
import { ButtonModule } from 'primeng/button';
  
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TableModule,
        ButtonModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
  
export class AppModule { }
JavaScript

运行应用程序:

从你的项目根部执行下面的命令来运行angular应用程序。

ng serve --open
JavaScript

输出:

Angular PrimeNG表的加载状态

例子2:在这个例子中,我们指定了一个自定义的图标,在项目加载时进行渲染。

<div style="text-align: center">
    <h2 style="color: green">GeeksforGeeks</h2>
    <h4>Angular PrimeNG Table Loading Status</h4>
  
    <p-table 
        [value]="items" 
        [scrollable]="true" 
        loadingIcon="pi pi-star pi-spin"
        [loading]="isLoading">
        <ng-template pTemplate="header">
            <tr>
                <th>S.No</th>
                <th>Item</th>
                <th>Count</th>
            </tr>
        </ng-template>
        <ng-template pTemplate="body" let-item>
            <tr>
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>{{item.count}}</td>
                  
            </tr>
        </ng-template>
    </p-table>
  
    <button 
        pButton 
        label="Load" 
        (click)="onClick()" 
        style="
            position: absolute; 
            bottom: 50px; 
            right: 0
        ">
    </button>
</div>
HTML
import { Component } from '@angular/core';
  
interface Item {
    id: Number,
    name: String,
    count: Number
}
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
  
export class AppComponent {
  
    isLoading: boolean = true;
  
    items: Item[] = []
  
    ngOnInit() {
  
        this.isLoading = true;
  
        setTimeout(() => {
            this.items = [
                {
                    id: 1,
                    name: "Pen",
                    count: 9
                },
                {
                    id: 2,
                    name: "Rubber Bands",
                    count: 14
                },
                {
                    id: 3,
                    name: "Carrybag",
                    count: 1
                },
                {
                    id: 4,
                    name: "Spoon",
                    count: 1
                },
                {
                    id: 5,
                    name: "Perfume",
                    count: 2
                },
                {
                    id: 6,
                    name: "Pencil",
                    count: 11
                },
                {
                    id: 7,
                    name: "NoteBook",
                    count: 3
                },
                {
                    id: 8,
                    name: "Facewash",
                    count: 1
                },
            ];
  
            // Set isLoading to false after 1 seconds
            this.isLoading = false;
        }, 1000);
  
    }
  
    //
    onClick() {
        this.items = [];
        this.ngOnInit();
    }
  
}
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';
import { ButtonModule } from 'primeng/button';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TableModule,
        ButtonModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
  
export class AppModule { }
JavaScript

输出:

Angular PrimeNG表的加载状态

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册