Articles in this section
Category / Section

How to get started easily with Syncfusion Angular 11 Multi Select?

8 mins read


Quick Start Guide: Syncfusion Angular 11 MultiSelect Dropdown

This article provides a quick and easy guide to getting started with the Syncfusion Angular 11 MultiSelect Dropdown component. Whether you're new to Syncfusion or Angular 11, this quick start project will help you build a functional MultiSelect Dropdown with minimal code and setup. You'll learn how to install the required packages, configure the component, and integrate it into your Angular app in just a few simple steps.


Project Pre-requisites: 

Make sure that you have the compatible versions of Angular in your machine before starting to work on this project.

 

Introduction:


The Angular 11 MultiSelect Dropdown used in this project is created from the Syncfusion `ej2-angular-dropdowns` package. You can simply define it as <ejs-multiselect> within the template.


Dependencies:

 

Before starting with this project, the Angular 11 Multi-Select Dropdown requires adding the Syncfusion `ej2-angular-dropdowns` package from npmjs, which is distributed in npm as @syncfusion scoped packages.


Creating Angular Project: 


To create the Angular project using the Angular CLI tool, follow the steps.

  1. Install the Angular CLI application in your machine.
npm install -g @angular/cli@11.2.3

 

  1. Now create a new Angular 11 project using the command `ng new` and navigate to that folder.
ng new angular11-app

cd angular11-app

 

  1. Install the ej2-angular-dropdowns package through the npm install command.
npm install @syncfusion/ej2-angular-dropdowns –save

Adding Angular 11 MultiSelect Dropdown

 

You can add the Angular 11 MultiSelect Dropdown component by using the `ej2-multiselect` directive. The attributes used within this tag allow you to define other MultiSelect Dropdown functionalities. To add the Angular 11 MultiSelect Dropdown, follow the steps:

  1. Import the MultiSelectAllModule into the app.module.ts file from the ej2-angular-dropdowns package.
  2. Import and inject the other required modules within the providers section of app.module.ts.

 

[app.module.ts]

import { BrowserModule, } from '@angular/platform-browser';

import { MultiSelectAllModule } from '@syncfusion/ej2-angular-dropdowns';

 

import { NgModule } from '@angular/core';

import { FormsModule }   from '@angular/forms';

import { AppComponent } from './app.component';

 

@NgModule({

  declarations: [

    AppComponent

  ],

  imports: [

    BrowserModule, FormsModule, MultiSelectAllModule

  ],

  bootstrap: [AppComponent]

})

export class AppModule { }

 

  1. Define the Angular MultiSelect Dropdown code within the app.component.html file, which is mapped against the templateUrl option in the app.component.ts file.


 [app.component.html] 

<ejs-multiselect></ejs-multiselect>

 

  1. Refer to the CDN link of CSS reference within the index.html file.


[index.html] 

<link href="https://cdn.syncfusion.com/ej2/material.css" rel="stylesheet" />

 

  1. Run the application with the ng serve command, and  an empty MultiSelectDropdown will be displayed on the browser. Now, you can load the MultiSelect Dropdown with data.

 

Screenshot

 

MultiSelect_app

Loading data

You can populate the empty MultiSelect Dropdown with data by using the JSON data through the `datasource` property.

import { Component } from '@angular/core';

 

@Component({

  selector: 'app-root',

  templateUrl: './app.component.html',

  styleUrls: ['./app.component.css'],

})

export class AppComponent {

     // define the JSON of data

     public countries: { [key: string]: Object; }[] = [

        { Name: 'Australia', Code: 'AU' },

            { Name: 'Bermuda', Code: 'BM' },

            { Name: 'Canada', Code: 'CA' },

            { Name: 'Cameroon', Code: 'CM' },

            { Name: 'Denmark', Code: 'DK' },

            { Name: 'France', Code: 'FR' },

            { Name: 'Finland', Code: 'FI' },

            { Name: 'Germany', Code: 'DE' },          

        ];

        // maps the local data column to fields property

        public localFields: Object = { text: 'Name', value: 'Code' };

        // set the placeholder to MultiSelect Dropdown input element

        public localWaterMark: string = 'Select countries';

}

 

[app.component.html]

 

<ejs-multiselect id='localData' #local [dataSource]='countries' [fields]='localFields' [placeholder]='localWaterMark'></ejs-multiselect>

 

Screenshot

Setting initial select value on MultiSelect Dropdown

Setting initial select value on MultiSelect Dropdown: 


You can populate the empty MultiSelect Dropdown with value by binding the string data through the `value` property initially.

[app.component.ts]

import { Component } from '@angular/core';

 

@Component({

  selector: 'app-root',

  templateUrl: './app.component.html',

  styleUrls: ['./app.component.css'],

})

export class AppComponent {

         public value: string[] = ['AU'];

}

Now, assign this value to the `value` property of Angular MultiSelect Dropdown within the app.component.html file.

[app.component.html]

<ejs-multiselect id='localData' #local [dataSource]='countries' [fields]='localFields' [placeholder]='localWaterMark' [value]='value'

></ejs-multiselect>

 

Screenshot

 

MultiSelect_Fields

Getting the selected value on form submit.


By using the ngModel, you can get the value from the ngform submit.

[app.component.ts]

import { Component } from '@angular/core';

 

@Component({

  selector: 'app-root',

  templateUrl: './app.component.html',

  styleUrls: ['./app.component.css'],

})

export class AppComponent {

        onSubmit(form: NgForm): void {

            console.log(form.value.name);

          }

}

 

[app.component.html]

<form #form='ngForm' (ngSubmit)="onSubmit(form)">

    <div class="form-group"> 

        <ejs-multiselect id='localData' name='name'  #local='ngModel' [(value)]='value'[(ngModel)]='value' [dataSource]='countries' [fields]='localFields' [placeholder]='localWaterMark'></ejs-multiselect>

        <button type="submit" ejs-button>Submit</button>

    </div>

  </form>

 

Run the application with the command ng serve in the command prompt. You can view the Angular MultiSelect Dropdown output with the data and other settings.

command ng serve in the command prompt

 

Conclusion

We hope you enjoyed learning on how to get started easily with Syncfusion Angular 11 MultiSelect.

You can refer to our Angular Dropdown feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Angular Dropdown example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, BoldDesk Support, or feedback portal. We are always happy to assist you!

 


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied