Home

Awesome

Angular 2/4/6/8 Multiselect Dropdown - Updated to work with Angular 13

npm version downloads license

Angular 2 multiselect dropdown component for web applications. Easy to integrate and use.

Important Notice !!

From v3.0.0 onwards, you need to include default.theme.css file to get the basic styling of the dropdown. Refer to themes and theming section below

Documentation | Demos / Examples.

Table of Contents

1. Getting Started
2. Installation
3. Usage
4 Theming
5. Templates
6. Template Driven Forms support
7. Reactive Forms support
8. Settings configuration
9. Callbacks and events
10. Lazy lodaing - handle large data lists
11. Group By feature
12. Search filter for both plain list and grouped list
13. Custom Search / Search API

Getting Started

Installation

Usage

Import AngularMultiSelectModule into NgModule in app.module.ts. Angular's FormsModule is also required.

import { AngularMultiSelectModule } from 'angular2-multiselect-dropdown';
import { FormsModule } from '@angular/forms';

@NgModule({
  // ...
  imports: [
    AngularMultiSelectModule,
    FormsModule
  ]
  // ...
})

Declare the component data variables and options in your component where you want to consume the dropdown component.


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

export class AppComponent implements OnInit {
    dropdownList = [];
    selectedItems = [];
    dropdownSettings = {};
    ngOnInit(){
        this.dropdownList = [
                              {"id":1,"itemName":"India"},
                              {"id":2,"itemName":"Singapore"},
                              {"id":3,"itemName":"Australia"},
                              {"id":4,"itemName":"Canada"},
                              {"id":5,"itemName":"South Korea"},
                              {"id":6,"itemName":"Germany"},
                              {"id":7,"itemName":"France"},
                              {"id":8,"itemName":"Russia"},
                              {"id":9,"itemName":"Italy"},
                              {"id":10,"itemName":"Sweden"}
                            ];
        this.selectedItems = [
                                {"id":2,"itemName":"Singapore"},
                                {"id":3,"itemName":"Australia"},
                                {"id":4,"itemName":"Canada"},
                                {"id":5,"itemName":"South Korea"}
                            ];
        this.dropdownSettings = { 
                                  singleSelection: false, 
                                  text:"Select Countries",
                                  selectAllText:'Select All',
                                  unSelectAllText:'UnSelect All',
                                  enableSearchFilter: true,
                                  classes:"myclass custom-class"
                                };            
    }
    onItemSelect(item:any){
        console.log(item);
        console.log(this.selectedItems);
    }
    OnItemDeSelect(item:any){
        console.log(item);
        console.log(this.selectedItems);
    }
    onSelectAll(items: any){
        console.log(items);
    }
    onDeSelectAll(items: any){
        console.log(items);
    }
}

Add the following component tag in you template

<angular2-multiselect [data]="dropdownList" [(ngModel)]="selectedItems" 
    [settings]="dropdownSettings" 
    (onSelect)="onItemSelect($event)" 
    (onDeSelect)="OnItemDeSelect($event)"
    (onSelectAll)="onSelectAll($event)"
    (onDeSelectAll)="onDeSelectAll($event)"></angular2-multiselect>

Themes and Theming

You can create your own theme from now on. You can have a look at example scss theming file at Default theme

Template - For custom html of menu item

<angular2-multiselect [data]="dropdownList" [(ngModel)]="selectedItems" [settings]="dropdownSettings">
  <c-item>
          <ng-template let-item="item">
            <label style="color: #333;min-width: 150px;">{{item.itemName}}</label>
            <img [src]="item.image" style="width: 30px; border: 1px solid #efefef;margin-right: 20px;" />
            <label>Capital - {{item.capital}}</label>
          </ng-template>
  </c-item>    
</angular2-multiselect>

Template - For custom html of Selected item - badge

<angular2-multiselect [data]="dropdownList" [(ngModel)]="selectedItems" [settings]="dropdownSettings">
  <c-badge>
           <ng-template let-item="item">
            <label style="margin: 0px;">{{item.itemName}}</label>
            <img [src]="item.image" style="width: 16px; margin-right: 5px;" />
           </ng-template>
  </c-badge>  
</angular2-multiselect>

Template Driven Forms support


<form (ngSubmit)="onSubmit()" #loginForm="ngForm" style="border: 1px solid #ccc; padding: 10px;">
        <div class="form-group">
            <label for="name">Skills</label>
            <angular2-multiselect [data]="itemList" [(ngModel)]="formModel.skills" 
                                  [settings]="settings" 
                                  (onSelect)="onItemSelect($event)"
                                  (onDeSelect)="OnItemDeSelect($event)" 
                                  (onSelectAll)="onSelectAll($event)" 
                                  (onDeSelectAll)="onDeSelectAll($event)" name="skills">
            </angular2-multiselect>
        </div>
</form>


formModel = {
        name: '',
        email: 'ascasc@aa.com',
        skills: [{ "id": 1, "itemName": "Angular" }]
    };

Reactive Forms support


<form [formGroup]="userForm" novalidate style="border: 1px solid #ccc; padding: 10px;">
        <div class="form-group">
            <label for="name">Skills</label>
           <angular2-multiselect [data]="itemList" [(ngModel)]="selectedItems" 
                                  [settings]="settings" 
                                  (onSelect)="onItemSelect($event)"
                                  (onDeSelect)="OnItemDeSelect($event)" 
                                  (onSelectAll)="onSelectAll($event)" 
                                  (onDeSelectAll)="onDeSelectAll($event)" formControlName="skills">
            </angular2-multiselect>
        </div>
</form>

userForm: FormGroup;
this.userForm = this.fb.group({
            name: '',
            email: ['', Validators.required],
            skills: [[], Validators.required]
        });

Settings

The following list of settings are supported by the component. Configure the settings to meet your requirement.

SettingTypeDescriptionDefault Value
singleSelectionBooleanTo set the dropdown for single item selection only.false
textStringText to be show in the dropdown, when no items are selected.'Select'
enableCheckAllBooleanEnable the option to select all items in listfalse
selectAllTextStringText to display as the label of select all optionSelect All
unSelectAllTextStringText to display as the label of unSelect optionUnSelect All
enableSearchFilterBooleanEnable filter option for the list.false
enableFilterSelectAllBooleanA 'select all' checkbox to select all filtered results.true
filterSelectAllTextStringText to display as the label of select all optionSelect all filtered results
filterUnSelectAllTextStringText to display as the label of unSelect optionUnSelect all filtered results
maxHeightNumberSet maximum height of the dropdown list in px.300
badgeShowLimitNumberLimit the number of badges/items to show in the input field. If not set will show all selected.All
classesStringCustom classes to the dropdown component. Classes are added to the dropdown selector tag. To add multiple classes, the value should be space separated class names.''
limitSelectionNumberLimit the selection of number of items from the dropdown list. Once the limit is reached, all unselected items gets disabled.none
disabledBooleanDisable the dropdownfalse
searchPlaceholderTextStringCustom text for the search placeholder text. Default value would be 'Search''Search'
groupByStringName of the field by which the list should be grouped.none
selectGroupBooleanSelect a group at once. GroupBy should be enabled, to use this.false
searchAutofocusBooleanAutofocus search input fieldtrue
labelKeyStringThe property name which should be rendered as label in the dropdownitemName
primaryKeyStringThe property by which the object is identified. Default is 'id'.id
positionStringSet the position of the dropdown list to 'top' or 'bottom'bottom
noDataLabelStringLabel text when no data is available in the list'No Data Available'
searchByArraySearch the list by certain properties of the list item. Ex: ["itemName, "id","name"]. Deafult is , it will search the list by all the properties of list item[]
lazyLoadingBooleanEnable lazy loading. Used to render large datasets.false
showCheckboxBooleanShow or hide checkboxes in the listtrue
addNewItemOnFilterBooleanWhe you filter items and if, the item is not found, you can add the text as new item to the listfalse
addNewButtonTextStringThe text in the button when addNewItemOnFilter is enabled'Add'
escapeToClosebooleanPress excape key to close the dropdowntrue
autoPositionbooleanEnable dropdown to open either on 'top' or 'bottom' Ex: settings = { position: 'bottom', autoPosition: false }; open the dropdown always at bottomtrue
tagToBodybooleanIf the dropdown to be appended to body or not ?true

Events

Run locally

License

MIT License.