Open In App

Angular PrimeNG Form MultiSelect Virtual Scrolling Component

Last Updated : 02 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 how to use the Form MultiSelect Virtual Scrolling Component in Angular PrimeNG.

The Multiselect Component provides the user with a list of options where the user can select one or more options. The properties of the MultiSelect Component are listed below.

By displaying only a small portion of the data in the viewport at any given time, VirtualScrolling is an effective method for rendering the options. To avoid performance issues when dealing with a large number of options, VirtualScrolling should be enabled. Setting the virtualScroll property to true and defining the virtualScrollItemSize property to specify an item’s height is all that is required for use.

 

Syntax:

<p-multiSelect
    [options]="..."
    placeholder="..."
    [virtualScroll]="true"
    [filter]="..."
    [itemSize]="...">
</p-multiSelect>

Angular PrimeNG Form MultiSelect  Virtual Scrolling Properties:

  • options: It is an array object representing select items to display as the available options. It is of array data type, the default value is null.
  • virtualScroll: It is used to define the virtual scroll.
  • virtualScrollItemSize: It is used to define the height of an item in the list for VirtualScrolling.
  • filter: It is used to display an input field to filter the items on keyup.

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: After completing the above processes, 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 Form MultiSelect Virtual Scrolling Component.

  • app.component.html:

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG Form MultiSelect Virtual Scroll</h5>
  
<p-multiSelect
    [options]="geeks"
    placeholder="Select any item Geek"
    [virtualScroll]="true"
    [filter]="true"
    [itemSize]="34"
    class="multiselect-custom-virtual-scroll">
</p-multiSelect>


  • app.component.ts:

Javascript




import { Component } from "@angular/core";
import { SelectItem, PrimeNGConfig } from "primeng/api";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
    styleUrls: ["./app.component.scss"],
})
  
export class AppComponent {
    geeks: SelectItem[];
  
    constructor() 
      {
        this.geeks = [];
  
        for (let i = 0; i < 10000; i++) {
            this.geeks.push(
                
                    label: 'Item ' + i, 
                    value: 'Item ' + i 
                }
            );
        }
    }
}


  • app.module.ts:

Javascript




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


Output:

 

Example 2: Below is another example code that illustrates the use of Angular PrimeNG Form MultiSelect Virtual Scrolling Component using templating.

  • app.component.html:

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG Form MultiSelect Virtual Scrolling</h5>
  
<p-multiSelect
    [options]="geeks"
    placeholder="Search any Geek"
    [virtualScroll]="true"
    [filter]="true"
    [itemSize]="34"
    class="multiselect-custom-virtual-scroll">
    <ng-template let-geek pTemplate="item">
        <div class="country-item">
              <div>{{ geek.label }}</div>
        </div>
    </ng-template>
</p-multiSelect>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss'],
})
  
export class AppComponent {
    geeks: any[];
  
    constructor( ) {
      this.geeks = [];
  
      for (let i = 0; i < 10000; i++) {
        this.geeks.push(
            
                label: 'Geek ' + i, 
                value: 'Geek Name ' + i 
            }
        );
      }
    }
}


  • app.module.ts:

Javascript




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


Output:

 

Reference: https://primefaces.org/primeng/multiselect



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads