Open In App

Angular PrimeNG PickList Events

Last Updated : 20 Jan, 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. It provides a lot of templates, components, theme design, an extensive icon library, and much more. In this article, we will see the Angular PrimeNG PickList Events.

The PickList Component is used to reorder items between different categories and lets us pick and select items. It acts as a drag-and-drop component with two tables and contents in it.

Angular PrimeNG PickList Events:

  • onMoveToTarget: It is invoked when items are moved from source to target.
  • onMoveToSource: It is invoked when items are moved from target to source.
  • onMoveAllToTarget: It is invoked when all items are moved from source to target.
  • onMoveAllToSource: It is invoked when all items are moved from target to source.
  • onSourceReorder: It is invoked when items are reordered within the source list.
  • onTargetReorder: It is invoked when items are reordered within the target list.
  • onSourceSelect: It is invoked source is selected.
  • items: It is invoked when items are selected within the source list.
  • onTargetSelect: It is invoked when the target is selected.
  • items: It is invoked when items are selected within the target list.
  • onSourceFilter: It is invoked when there is a Filter value.
  • items: It is invoked when the source list is filtered.
  • onTargetFilter: It is invoked when there is a Filter value.
  • items: It is invoked when the target list is filtered.

 

Syntax:

<p-pickList 
    sourceHeader="...." 
    targetHeader="....">

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
npm install @angular/cdk --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: Below is the example code that illustrates the use of Angular PrimeNG Picklist Events. In this example, we will learn about onMoveToTarget and onMoveToSource.

  • app.component.html:

HTML




<h1 style="color: green;">
    GeeksforGeeks
</h1>
<h3>
    Angular PrimeNG Picklist Events
</h3>
<p-pickList
    ="sourceTutorials"
    [target]="targetTutorials"
    sourceHeader="Tutorials Available in GeeksforGeeks"
    targetHeader="Selected Tutorials from GeeksforGeeks"
    [dragdrop]="true"
    [sourceStyle]="{ height: '500px' ,background:'pink'}"
    [targetStyle]="{ height: '500px',background:'yellow' }"
    (onMoveToTarget)="moveToTarget()"
    (onMoveToSource)="moveToSource()">
    <ng-template let-tutorial pTemplate="item">
        <div>
            <div style="background: 'green'; color: 'white';">
                <h5 class="mb-2">
                    {{ tutorial.title }}
                </h5>
                <h5>{{ tutorial.category }}</h5>
                <h6 class="mb-2">
                    Rating: {{ tutorial.rating }}
                </h6>
            </div>
        </div>
    </ng-template>
</p-pickList>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
import { PrimeNGConfig } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
  
export class AppComponent {
    sourceTutorials: Tutorial[]=[];
    targetTutorials: Tutorial[]=[];
  
    constructor(private primengConfig: PrimeNGConfig) { }
  
    ngOnInit() {
        this.sourceTutorials = [
            {
                title: 'Queue',
                category: 'Data Structure',
                rating: 8,
            },
            {
                title: 'Circularly LinkedList',
                category: 'Data Structure',
                rating: 1,
            },
            {
                title: 'Doubly LinkedList',
                category: 'Data Structure',
                rating: 3,
            },
              
            {
                title: 'Red Black Tree',
                category: 'Data Structure',
                rating: 9,
            },
            {
                title: 'Breadth First Search',
                category: 'Graph',
                rating: 6,
            },
            {
                title: "Floyd's Cycle",
                category: 'Algorithm',
                rating: 7,
            },
            {
                title: 'Travelling Salesman Problem',
                category: 'Algorithm',
                rating: 4,
            },
            {
                title: 'Bellman Ford',
                category: 'Graph',
                rating: 8,
            },
            {
                title: 'KMP Algorithm',
                category: 'String',
                rating: 10,
            },
        ];
        this.targetTutorials = [];
        this.primengConfig.ripple = true;
    }
  
    moveToTarget(){
        alert('Hi Geek! You are moving me to Target')
    }
    moveToSource(){
        alert('Hi Geek! You are moving me to Source')
    }
}
export interface Tutorial {
    category?: string;
    title?: string;
    rating?: number;
}


  • app.module.ts:

Javascript




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 { PickListModule } from "primeng/picklist";
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        PickListModule,
        HttpClientModule,
        FormsModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule {}


Output:

 

Example 2: Below is another example code that illustrates the use of Angular PrimeNG Picklist Events. In this example, we will learn about onSourceSelect ad onTargetSelect.

  • app.component.html:

HTML




<h1 style="color: green;">
    GeeksforGeeks
</h1>
<h3>
  Angular PrimeNG Picklist Events
</h3>
<p-pickList 
    ="sourceTutorials" 
    [target]="targetTutorials" 
    sourceHeader="Tutorials Available in GeeksforGeeks"
    targetHeader="Selected Tutorials from GeeksforGeeks" 
    [dragdrop]="true" 
    [sourceStyle]="{ height: '500px' ,background:'pink'}" 
    [targetStyle]="{ height: '500px',background:'yellow' }"
    (onSourceSelect)=onSourceSelect()
    (onTargetSelect)=onTargetSelect()>
    <ng-template let-tutorial pTemplate="item">
        <div>
            <div style="background:'green';color:'white'">
                <h5 class="mb-2">
                    {{ tutorial.title }}
                </h5>
                <h5>{{ tutorial.category }}</h5>
                <h6 class="mb-2">
                    Rating: {{ tutorial.rating }}
                </h6>
            </div>
        </div>
    </ng-template>
</p-pickList>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
import { PrimeNGConfig } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
  
export class AppComponent {
    sourceTutorials: Tutorial[]=[];
    targetTutorials: Tutorial[]=[];
  
    constructor(private primengConfig: PrimeNGConfig) { }
  
    ngOnInit() {
        this.sourceTutorials = [
            {
                title: 'Queue',
                category: 'Data Structure',
                rating: 8,
            },
            {
                title: 'Circularly LinkedList',
                category: 'Data Structure',
                rating: 1,
            },
            {
                title: 'Doubly LinkedList',
                category: 'Data Structure',
                rating: 3,
            },
              
            {
                title: 'Red Black Tree',
                category: 'Data Structure',
                rating: 9,
            },
            {
                title: 'Breadth First Search',
                category: 'Graph',
                rating: 6,
            },
            {
                title: "Floyd's Cycle",
                category: 'Algorithm',
                rating: 7,
            },
            {
                title: 'Travelling Salesman Problem',
                category: 'Algorithm',
                rating: 4,
            },
            {
                title: 'Bellman Ford',
                category: 'Graph',
                rating: 8,
            },
            {
                title: 'KMP Algorithm',
                category: 'String',
                rating: 10,
            },
        ];
        this.targetTutorials = [];
        this.primengConfig.ripple = true;
    }
  
    onSourceSelect(){
        alert('Hi Geek! You have selected source')
    }
    onTargetSelect(){
        alert('Hi Geek! You have selected target')
    }
}
export interface Tutorial {
    category?: string;
    title?: string;
    rating?: number;
}


  • app.module.ts:

Javascript




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 { PickListModule } from "primeng/picklist";
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        PickListModule,
        HttpClientModule,
        FormsModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule {}


Output:

 

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads