Open In App

Angular PrimeNG DataView Events

Last Updated : 05 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. In this article, we will know about Angular PrimeNG DataView Events.

The DataView Component is used to display data in a grid and list layout with pagination and sorting features.

Angular PrimeNG DataView Events:

  • onLazyLoad: It is a callback that is fired when paging, sorting, or filtering happens in lazy mode.
  • onPage: It is a callback that is fired when pagination occurs.
  • onSort: It is a callback that is fired when sorting occurs.
  • onChangeLayout: It is a callback that is fired when changing the layout.

 

Syntax:

<p-dataView
     (event)=function()>
</p-dataView>

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

 

Steps to run the application: Run the below command to see the output

ng serve --save

Example 1: In this example, we will learn about Angular PrimeNG DataView Events onPage.

  • app.component.html:

HTML




<div style="width: 80%;">
    <h1 style="color: green;">GeeksforGeeks</h1>
    <h2>Angular PrimeNG DataView Events</h2>
    <div class="card">
        <p-dataView
            #dv
            [value]="productNames"
            [paginator]="true"
            [rows]="2"
            layout="grid"
            (onPage)="onPage()">
            <ng-template let-product pTemplate="gridItem">
                <p-card header="GeeksforGeeks">
                    {{product}}
                </p-card>
            </ng-template>
        </p-dataView>
    </div>
</div>


  • app.component.ts:

Javascript




import { Component } from "@angular/core";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
    styleUrls: ["./app.component.css"]
})
  
export class AppComponent {
    productNames: string[] = [
        "Java",
        "Python",
        "C++",
        "Angular",
        "React",
        "NodeJS",
        "AWS",
        "R Programming",
        "Machine Learning"
    ];
  
    onPage() {
        alert("Hi Geek!! Pagination is done");
    }
}


  • app.module.ts:

Javascript




import { NgModule } from "@angular/core";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { CardModule } from "primeng/card";
  
import { AppComponent } from "./app.component";
import { DataViewModule } from "primeng/dataview";
  
@NgModule({
    imports: [
      BrowserAnimationsModule,
      DataViewModule,
      CardModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule {}


Output:

 

Example 2: In this example, we will learn about Angular PrimeNG DataView Events onLazyLoad.

  • app.component.html:

HTML




<div style="width: 80%;">
    <h1 style="color: green;">GeeksforGeeks</h1>
    <h2>Angular PrimeNG DataView Events</h2>
    <div class="card">
        <p-dataView
            #dv
            [value]="productNames"
            [paginator]="true"
            [rows]="2"
            layout="grid"
            [lazy]="true"
            (onLazyLoad)="onLazyLoad()">
            <ng-template let-product pTemplate="gridItem">
                <p-card header="GeeksforGeeks">
                    {{product}}
                </p-card>
            </ng-template>
        </p-dataView>
    </div>
</div>


  • app.component.ts:

Javascript




import { Component } from "@angular/core";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
    styleUrls: ["./app.component.css"],
})
  
export class AppComponent {
    productNames: string[] = [
        "Java",
        "Python",
        "C++",
        "Angular",
        "React",
        "NodeJS",
        "AWS",
        "R Programming",
        "Machine Learning"
    ];
  
    onLazyLoad(){
        alert("Hi Geek!! There is Lazy Layout")
    }
}


  • app.module.ts:

Javascript




import { NgModule } from "@angular/core";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { CardModule } from "primeng/card";
  
import { AppComponent } from "./app.component";
import { DataViewModule } from "primeng/dataview";
  
@NgModule({
    imports: [
      BrowserAnimationsModule,
      DataViewModule,
      CardModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule {}


Output:

 

Reference: http://primefaces.org/primeng/dataview



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads