Introduction

In this article, we will learn how to add Syncfusion dropdowns in the Angular 10 application.
Prerequisites
  • Basic Knowledge of Angular 2 or higher
  • Visual Studio Code
  • Node and NPM installed
  • Bootstrap

Create an Angular Project

Create an Angular project by using the following command,
  1. ng new Kanbandata
Open this project in Visual Studio Code and install Bootstrap by using the following command,
  1. npm install bootstrap --save
Now open the styles.css file and add Bootstrap file reference. To add a reference in the styles.css file add this line.
  1. @import '~bootstrap/dist/css/bootstrap.min.css';
Now install the Syncfusion dropdown library, To install, use the following command,
  1. npm install @syncfusion/ej2-angular-dropdowns
Now import dropdown module in app.module.ts file, add the following code in this file.
  1. import { NgModule } from '@angular/core';
  2. import { BrowserModule } from '@angular/platform-browser';
  3. import { HttpClientModule } from "@angular/common/http";
  4. import { AppComponent } from './app.component';
  5. import { DropDownListAllModule } from '@syncfusion/ej2-angular-dropdowns';
  6. import { CheckBoxAllModule } from '@syncfusion/ej2-angular-buttons';
  7. import { DropdownDemoComponent } from './dropdown-demo/dropdown-demo.component';
  8. @NgModule({
  9. declarations: [
  10. AppComponent,
  11. DropdownDemoComponent
  12. ],
  13. imports: [
  14. BrowserModule,HttpClientModule,DropDownListAllModule,CheckBoxAllModule
  15. ],
  16. providers: [],
  17. bootstrap: [AppComponent]
  18. })
  19. export class AppModule { }
Now create a new component by using the following command,
  1. ng g c dropdowndemo
Now open dropdown-demo.component.html file and add the following code,
  1. <div class="container" style="margin-top:10px;margin-bottom: 24px;">
  2. <div class="col-sm-12 btn btn-success">
  3. How To Use Syncfusion Dropdown in Angular Application
  4. </div>
  5. </div>
  6. <div class="col-lg-9 content-wrapper" style="height: 350px">
  7. <div style="margin: 0px auto; width:250px; padding-top: 40px;">
  8. <ejs-dropdownlist id='Cities' #sample [dataSource]='CityData' [value]='value' [fields]='fields' [placeholder]='waterMark' [popupHeight]='height'></ejs-dropdownlist>
  9. </div>
  10. </div>
Now open dropdown-demo.component.ts file and add the following lines,
  1. import { Component,ViewChild, OnInit } from '@angular/core';
  2. import { DropDownListComponent } from '@syncfusion/ej2-angular-dropdowns';
  3. @Component({
  4. selector: 'app-dropdown-demo',
  5. templateUrl: './dropdown-demo.component.html',
  6. styleUrls: ['./dropdown-demo.component.css']
  7. })
  8. export class DropdownDemoComponent implements OnInit {
  9. constructor() { }
  10. public listObj: DropDownListComponent;
  11. public CityData: Object[] = [
  12. { Id: '1', City: 'Jaipur' },
  13. { Id: '2', City: 'Delhi' },
  14. { Id: '3', City: 'Noida' },
  15. { Id: '4', City: 'Gurgoan' },
  16. { Id: '5', City: 'Pune' },
  17. { Id: '6', City: 'Mumbai' },
  18. { Id: '7', City: 'Ajmer' },
  19. { Id: '8', City: 'Bangalore' },
  20. { Id: '9', City: 'Hyderabad' },
  21. { Id: '10', City: 'Vizag' }
  22. ];
  23. ngOnInit(): void {
  24. }
  25. public fields: Object = { text: 'City', value: 'Id' };
  26. public height: string = '220px';
  27. public waterMark: string = 'Select a City';
  28. public value: string = '3';
  29. }
Now open app.component.html file and add the following code,
  1. <app-dropdown-demo></app-dropdown-demo>
Now run the application using npm start and check the result.
Now we add a Multiselect dropdown. Let's create a new component by using the following command,
  1. ng g c multi-select dropdowns
Now open multiselectdropdowns.component.html file and add the following code,
  1. <div class="container" style="margin-top:10px;margin-bottom: 24px;">
  2. <div class="col-sm-12 btn btn-success">
  3. How To Use Syncfusion MultiSelect Dropdown in Angular Application
  4. </div>
  5. </div>
  6. <div class="container" style="height: 150px">
  7. <div id="multiselect-sample" class="control-section" style="height: 500px">
  8. <div id='content'>
  9. <div class="control-styles">
  10. <h6>Select City</h6>
  11. <ejs-multiselect id='sample-list1' [dataSource]='CityData' [mode]='default' [fields]='fields' [placeholder]='waterMark'></ejs-multiselect>
  12. </div>
  13. </div>
  14. </div>
  15. </div>
  16. <div class="container" style="height: 350px">
  17. <ejs-multiselect id='multiselect-checkbox' #checkbox [dataSource]='CityData' [placeholder]='checkWaterMark' [fields]='checkFields'
  18. [mode]='mode' [popupHeight]='popHeight' [showDropDownIcon]='true' showSelectAll='true' [filterBarPlaceholder]='filterPlaceholder'></ejs-multiselect>
  19. </div>
Now open multiselectdropdowns.component.tsfile and add the following code,
  1. import { Component,ViewChild, OnInit } from '@angular/core';
  2. import { CheckBoxComponent } from '@syncfusion/ej2-angular-buttons';
  3. import { MultiSelectComponent } from '@syncfusion/ej2-angular-dropdowns';
  4. @Component({
  5. selector: 'app-multiselectdropdowns',
  6. templateUrl: './multiselectdropdowns.component.html',
  7. styleUrls: ['./multiselectdropdowns.component.css']
  8. })
  9. export class MultiselectdropdownsComponent implements OnInit {
  10. mode: string;
  11. filterPlaceholder: string;
  12. constructor() { }
  13. @ViewChild('sample')
  14. public listObj: MultiSelectComponent;
  15. public CityData: Object[] = [
  16. { Id: '1', City: 'Jaipur' },
  17. { Id: '2', City: 'Delhi' },
  18. { Id: '3', City: 'Noida' },
  19. { Id: '4', City: 'Gurgoan' },
  20. { Id: '5', City: 'Pune' },
  21. { Id: '6', City: 'Mumbai' },
  22. { Id: '7', City: 'Ajmer' },
  23. { Id: '8', City: 'Bangalore' },
  24. { Id: '9', City: 'Hyderabad' },
  25. { Id: '10', City: 'Vizag' }
  26. ];
  27. public fields: Object = { text: 'City', value: 'Id' };
  28. public waterMark: string = 'Choose Cities';
  29. public default : string = 'Default';
  30. public box : string = 'Box';
  31. public delimiter : string = 'Delimiter';
  32. public checkWaterMark: string = 'Select City';
  33. public checkFields: Object = { text: 'City', value: 'Id' };
  34. public popHeight: string = '350px';
  35. ngOnInit(): void {
  36. this.mode = 'CheckBox';
  37. this.filterPlaceholder = 'Search city';
  38. }
  39. }
Now open app.module.ts file ,add the following code in this file.
  1. import { NgModule } from '@angular/core';
  2. import { BrowserModule } from '@angular/platform-browser';
  3. import { HttpClientModule } from "@angular/common/http";
  4. import { AppComponent } from './app.component';
  5. import { DropDownListAllModule } from '@syncfusion/ej2-angular-dropdowns';
  6. import { CheckBoxAllModule } from '@syncfusion/ej2-angular-buttons';
  7. import { DropdownDemoComponent } from './dropdown-demo/dropdown-demo.component';
  8. import { AutoCompleteModule } from '@syncfusion/ej2-angular-dropdowns';
  9. import { MultiSelectAllModule } from '@syncfusion/ej2-angular-dropdowns';
  10. import { MultiselectdropdownsComponent } from './multiselectdropdowns/multiselectdropdowns.component';
  11. @NgModule({
  12. declarations: [
  13. AppComponent,
  14. DropdownDemoComponent,
  15. MultiselectdropdownsComponent
  16. ],
  17. imports: [
  18. BrowserModule,HttpClientModule,DropDownListAllModule,CheckBoxAllModule,AutoCompleteModule,MultiSelectAllModule
  19. ],
  20. providers: [],
  21. bootstrap: [AppComponent]
  22. })
  23. export class AppModule { }
Now run the application using npm start and check the result.

Summary

In this article, we learned how to add Syncfusion dropdowns in an Angular 10 Application.