Open In App

Angular PrimeNG Form Listbox Selection Component

Last Updated : 31 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. In this article, we will know how to use the ListBox Selection Component in Angular PrimeNG.

The ListBox component is used to make a list component from which we can select one or more items and can also be discarded if the item is not required by unchecking in the list.

Syntax:

<p-listbox
    [options]="..."
    optionLabel="...."
    [style]="{...}">
</p-listbox>

 

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 Listbox Selection Component using single selection. 

  • app.component.html:

HTML




<h2 style="color: green">GeeksforGeeks</h2>
<h5>Angular PrimeNG ListBox Selection Component</h5>
  
<p-listbox
    [options]="gfg"
    optionLabel="name"
    [style]="{'width':'15rem'}">
</p-listbox>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
import { PrimeNGConfig, SelectItemGroup } from 'primeng/api';
  
interface Course {
  name: string;
}
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styles: [],
})
  
export class AppComponent {
    gfg: Course[];
    selectedCourse: Course;
  
    constructor(private primengConfig: PrimeNGConfig) {
        this.gfg = [
            { name: 'HTML5' },
            { name: 'JavaScript' },
            { name: 'Java' },
            { name: 'ReactJS' },
            { name: 'AngularJS' },
        ];
    }
}


  • app.module.ts:

Javascript




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


Output:

 

Example 2: Below is another example code that illustrates the use of Angular PrimeNG Form Listbox Selection Component using multiple selections with checkboxes and also filtering.

  • app.component.html:

HTML




<h2 style="color: green">GeeksforGeeks</h2>
<h5>Angular PrimeNG ListBox Selection Component</h5>
  
<p-listbox
    [options]="gfg"
    optionLabel="name"
    [style]="{ width: '15rem' }"
    [multiple]="true"
    [metaKeySelection]="true"
    [checkbox]="true"
    filter="true">
</p-listbox>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
import { PrimeNGConfig, SelectItemGroup } from 'primeng/api';
  
interface Course {
  name: string;
}
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styles: [],
})
  
export class AppComponent {
    gfg: Course[];
    selectedCourse: Course;
  
    constructor(private primengConfig: PrimeNGConfig) {
        this.gfg = [
            { name: 'HTML5' },
            { name: 'JavaScript' },
            { name: 'Java' },
            { name: 'ReactJS' },
            { name: 'AngularJS' },
        ];
    }
}


  • app.module.ts:

Javascript




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


Output:

 

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



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

Similar Reads