Open In App

Angular PrimeNG Form MultiSelect Virtual Scrolling Component

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:

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.




<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>




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 
                }
            );
        }
    }
}




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.




<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>




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 
            }
        );
      }
    }
}




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


Article Tags :