Checkbox in Angular Multi select component
26 Aug 202513 minutes to read
The MultiSelect component provides built-in support for selecting multiple values through checkboxes when the mode
property is set to CheckBox
.
To enable checkbox functionality, inject the CheckBoxSelection
module into the MultiSelect component.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule } from '@angular/forms'
import { MultiSelectModule } from '@syncfusion/ej2-angular-dropdowns'
import { Component, OnInit} from '@angular/core';
import { CheckBoxSelectionService, FilteringEventArgs } from '@syncfusion/ej2-angular-dropdowns';
import { EmitType } from '@syncfusion/ej2-base';
import { Query } from '@syncfusion/ej2-data';
@Component({
imports: [
FormsModule, MultiSelectModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the MultiSelect component
template: `<ejs-multiselect id='multiselectelement' [dataSource]='sportsData' [fields]='fields'(filtering)='onFiltering($event)' [mode]='mode' [placeholder]='placeholder'></ejs-multiselect>`,
providers: [CheckBoxSelectionService]
})
export class AppComponent {
public mode?: string;
searchData: { [key: string]: Object; }[] | string[] | number[] | boolean[] | undefined;
constructor() {
}
//set the data to dataSource property
public sportsData: Object[] = [
{ id: 'Game1', sports: 'Badminton' },
{ id: 'Game2', sports: 'Basketball' },
{ id: 'Game3', sports: 'Cricket' },
{ id: 'Game4', sports: 'Football' },
{ id: 'Game5', sports: 'Golf' }
];
// maps the appropriate column to fields property
public fields: Object = { text: 'sports', value: 'id' };
// set placeholder to MultiSelect input element
public placeholder: string = 'Select games';
//Bind the filter event
public onFiltering: EmitType<FilteringEventArgs> = (e: FilteringEventArgs) => {
let query = new Query();
//frame the query based on search string with filter type.
query = (e.text != "") ? query.where("country", "startswith", e.text, true) : query;
//pass the filter data source, filter query to updateData method.
e.updateData((this as any).searchData, query);
};
ngOnInit(): void {
// set the type of mode for checkbox to visualized the checkbox added in li element.
this.mode = 'CheckBox';
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Select All
The MultiSelect component has built-in support for selecting all list items using the Select All
option in the header.
When the showSelectAll
property is set to true, the Select All text displays by default. The name attribute of the Select All option can be customized using the selectAllText
property.
For the unSelect All option, the unSelect All text displays by default. The name attribute of the unSelect All option can be customized using the unSelectAllText
property.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule } from '@angular/forms'
import { MultiSelectModule } from '@syncfusion/ej2-angular-dropdowns'
import { Component, OnInit } from '@angular/core';
import { CheckBoxSelectionService } from '@syncfusion/ej2-angular-dropdowns';
@Component({
imports: [
FormsModule, MultiSelectModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the MultiSelect component
template: `<ejs-multiselect id='multiselectelement' [dataSource]='sportsData' [fields]='fields' [mode]='mode' [selectAllText]='selectAllText' showSelectAll=true [placeholder]='placeholder'></ejs-multiselect>`,
providers: [CheckBoxSelectionService]
})
export class AppComponent {
public mode?: string;
public selectAllText: string| any
constructor() {
}
//set the data to dataSource property
public sportsData: Object[] = [
{ id: 'Game1', sports: 'Badminton' },
{ id: 'Game2', sports: 'Basketball' },
{ id: 'Game3', sports: 'Cricket' },
{ id: 'Game4', sports: 'Football' },
{ id: 'Game5', sports: 'Golf' }
];
// maps the appropriate column to fields property
public fields: Object = { text: 'sports', value: 'id' };
// set placeholder to MultiSelect input element
public placeholder: string = 'Select games';
ngOnInit(): void {
// set the type of mode for checkbox to visualized the checkbox added in li element.
this.mode = 'CheckBox';
// set the select all text to MultiSelect checkbox label.
this.selectAllText= 'Select All';
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Selection Limit
The limit for selected items can be defined using the maximumSelectionLength
property.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule } from '@angular/forms'
import { MultiSelectModule } from '@syncfusion/ej2-angular-dropdowns'
import { Component, OnInit } from '@angular/core';
import { CheckBoxSelectionService } from '@syncfusion/ej2-angular-dropdowns';
@Component({
imports: [
FormsModule, MultiSelectModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the MultiSelect component
template: `<ejs-multiselect id='multiselectelement' [dataSource]='sportsData' [fields]='fields' [mode]='mode' [maximumSelectionLength]='maximumSelectionLength' [placeholder]='placeholder'></ejs-multiselect>`,
providers: [CheckBoxSelectionService]
})
export class AppComponent {
public mode?: string;
public maximumSelectionLength?: number;
constructor() {
}
//set the data to dataSource property
public sportsData: Object[] = [
{ id: 'Game1', sports: 'Badminton' },
{ id: 'Game2', sports: 'Basketball' },
{ id: 'Game3', sports: 'Cricket' },
{ id: 'Game4', sports: 'Football' },
{ id: 'Game5', sports: 'Golf' }
];
// maps the appropriate column to fields property
public fields: Object = { text: 'sports', value: 'id' };
// set placeholder to MultiSelect input element
public placeholder: string = 'Select games';
ngOnInit(): void {
// set the type of mode for checkbox to visualized the checkbox added in li element.
this.mode = 'CheckBox';
// Sets limitation to the value selection
this.maximumSelectionLength = 3;
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Selection Reordering
The enableSelectionOrder
property enables reordering of selected items when the popup is visible.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule } from '@angular/forms'
import { MultiSelectModule } from '@syncfusion/ej2-angular-dropdowns'
import { Component, OnInit } from '@angular/core';
import { CheckBoxSelectionService } from '@syncfusion/ej2-angular-dropdowns';
@Component({
imports: [
FormsModule, MultiSelectModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the MultiSelect component
template: `<ejs-multiselect id='multiselectelement' [dataSource]='sportsData' [fields]='fields' [mode]='mode' [enableSelectionOrder]='true' [placeholder]='placeholder'></ejs-multiselect>`,
providers: [CheckBoxSelectionService]
})
export class AppComponent {
public mode?: string;
constructor() {
}
//set the data to dataSource property
public sportsData: Object[] = [
{ id: 'Game1', sports: 'Badminton' },
{ id: 'Game2', sports: 'Basketball' },
{ id: 'Game3', sports: 'Cricket' },
{ id: 'Game4', sports: 'Football' },
{ id: 'Game5', sports: 'Golf' }
];
// maps the appropriate column to fields property
public fields: Object = { text: 'sports', value: 'id' };
// set placeholder to MultiSelect input element
public placeholder: string = 'Select games';
ngOnInit(): void {
// set the type of mode for checkbox to visualized the checkbox added in li element.
this.mode = 'CheckBox';
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));