angular2-multiselect-dropdown

repository·master·Indexed 43 days ago

https://github.com/cuppalabs/angular2-multiselect-dropdown

An Angular multiselect dropdown component supporting Angular versions 2 through 18. It features customizable settings for single selection, search filtering, and lazy loading, as well as support for custom menu item and badge templates via <c-item> and <c-badge>. Version 10.0.0 is compatible with Angular 18.X.X.

Tokens
4.5K
Snippets
18
Records
22
Agent score
55%

What's inside angular2-multiselect-dropdown

  1. Customize menu item and badge templates

    master

    You can override the default HTML for both the dropdown menu items and the selected item badges using <c-item> and <c-badge> tags inside the component.

    Custom Menu Item

    Use <c-item> with an <ng-template> to define how each item in the list appears (e.g., adding images or extra labels).

    Custom Selected Item (Badge)

    Use <c-badge> with an <ng-template> to define how selected items appear in the input field after selection.

    <!-- Custom 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>
    
    <!-- Custom 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>
  2. Install angular2-multiselect-dropdown

    master

    To use the multiselect dropdown in your Angular project, you must import AngularMultiSelectModule into your NgModule. Additionally, FormsModule from @angular/forms is a required dependency for the component to function correctly.

    Ensure you select the version of angular2-multiselect-dropdown that matches your Angular version:

    • Angular 18.X.X: use angular2-multiselect-dropdown v10.0.0
    • Angular 17.X.X: use angular2-multiselect-dropdown v9.0.0
    • Angular 16.X.X: use angular2-multiselect-dropdown v8.0.0
    • Angular 15.X.X: use angular2-multiselect-dropdown v7.0.0
    • Angular 14.X.X: use angular2-multiselect-dropdown v6.0.0
    import { AngularMultiSelectModule } from 'angular2-multiselect-dropdown';
    import { FormsModule } from '@angular/forms';
    
    @NgModule({
      // ...
      imports: [
        AngularMultiSelectModule,
        FormsModule
      ]
      // ...
    })
  3. Include default theme CSS

    master

    From version 3.0.0 onwards, basic styling requires the default.theme.css file.

    1. Locate the file in node_modules/angular2-multiselect-dropdown/themes/default.theme.css.
    2. Include it in your project configuration:
      • For Angular 6+: Add it to the styles array in angular.json.
      • For Angular < 6: Add it to angular-cli.json.
  4. Import AngularMultiSelectModule into your Angular module

    master

    After installing the package, you must import AngularMultiSelectModule into your Angular application module (e.g., app.module.ts) to make the component available.

    import { AngularMultiSelectModule } from 'angular2-multiselect-dropdown';
    
    @NgModule({
      imports: [
        AngularMultiSelectModule.forRoot(),
        // ... other imports
      ],
      // ...
    })
    export class AppModule { }
  5. Configure the dropdown settings

    master

    The [settings] input accepts an object to customize the behavior and appearance of the dropdown. Common settings include:

    • singleSelection: Boolean to restrict selection to one item.
    • text: Placeholder text when no items are selected.
    • enableSearchFilter: Boolean to enable the search input.
    • selectAllText / unSelectAllText: Custom labels for select/unselect all actions.
    • labelKey: The property name used for the display label (defaults to itemName).
    • primaryKey: The property used for identification (defaults to id).
    • maxHeight: Maximum height of the dropdown list in pixels.
    • classes: Custom CSS classes applied to the component selector.
    this.dropdownSettings = {
      singleSelection: false, 
      text: "Select Countries",
      selectAllText: 'Select All',
      unSelectAllText: 'UnSelect All',
      enableSearchFilter: true,
      classes: "myclass custom-class"
    };
  6. Use angular2-multiselect in templates

    master

    To implement the dropdown, use the <angular2-multiselect> component tag. You can bind data using [data], manage selection with [(ngModel)], and apply configurations via [settings]. The component also supports various event bindings for selection changes.

    Basic Usage

    <angular2-multiselect 
        [data]="dropdownList" 
        [(ngModel)]="selectedItems" 
        [settings]="dropdownSettings" 
        (onSelect)="onItemSelect($event)" 
        (onDeSelect)="OnItemDeSelect($event)" 
        (onSelectAll)="onSelectAll($event)" 
        (onDeSelectAll)="onDeSelectAll($event)">
    </angular2-multiselect>
    <angular2-multiselect [data]="dropdownList" [(ngModel)]="selectedItems" 
        [settings]="dropdownSettings" 
        (onSelect)="onItemSelect($event)" 
        (onDeSelect)="OnItemDeSelect($event)" 
        (onSelectAll)="onSelectAll($event)" 
        (onDeSelectAll)="onDeSelectAll($event)"></angular2-multiselect>
  7. Configure the Angular Multiselect Dropdown with DropdownSettings

    master

    The DropdownSettings interface defines the configuration options for the multiselect dropdown component. You can use these properties to control selection behavior, UI text, filtering, searching, and layout.

    Key configuration categories include:

    • Selection Behavior: singleSelection (boolean), enableCheckAll (boolean), limitSelection (number), clearAll (boolean), and selectGroup (boolean).
    • Text & Labels: text (string), selectAllText (string), unSelectAllText (string), filterSelectAllText (string), filterUnSelectAllText (string), searchPlaceholderText (string), noDataLabel (string), and addNewButtonText (string).
    • Search & Filtering: enableSearchFilter (boolean), searchBy (string array of keys to search), searchAutofocus (boolean), addNewItemOnFilter (boolean), and enableFilterSelectAll (boolean).
    • Data Mapping: labelKey (string for display text), primaryKey (string for unique identifier), and groupBy (string for grouping items).
    • UI & Layout: maxHeight (number), badgeShowLimit (number), classes (string for custom CSS), showCheckbox (boolean), position (string), autoPosition (boolean), and disabled (boolean).
    • Loading & State: loading (boolean) and lazyLoading (boolean).
    const settings: DropdownSettings = {
      singleSelection: false,
      text: 'Select Items',
      enableCheckAll: true,
      selectAllText: 'Select All',
      unSelectAllText: 'Unselect All',
      enableSearchFilter: true,
      searchPlaceholderText: 'Search...',
      labelKey: 'name',
      primaryKey: 'id',
      showCheckbox: true
    };
  8. Configure VirtualScrollerDefaultOptions

    master

    The VirtualScrollerDefaultOptions interface allows you to tune the performance and appearance of the virtual scroller within the multiselect dropdown. This is useful for adjusting how scroll events are handled (throttling/debouncing), animation speeds, and scrollbar dimensions.

    export interface VirtualScrollerDefaultOptions {
    	scrollThrottlingTime: number;
    	scrollDebounceTime: number;
    	scrollAnimationTime: number;
    	scrollbarWidth?: number;
    	scrollbarHeight?: number;
    	checkResizeInterval: number;
    	resizeBypassRefreshThreshold: number;
    	modifyOverflowStyleOfParentScroll: boolean;
    	stripedTable: boolean;
    }
  9. Reference: Dropdown Settings options

    master

    The following settings are available for the [settings] input:

    SettingTypeDescriptionDefault Value
    singleSelectionBooleanSet for single item selection onlyfalse
    textStringText shown when no items are selected'Select'
    enableCheckAllBooleanEnable option to select all itemsfalse
    selectAllTextStringLabel for select all optionSelect All
    unSelectAllTextStringLabel for unselect all optionUnSelect All
    enableSearchFilterBooleanEnable filter option for the listfalse
    enableFilterSelectAllBoolean'select all' checkbox for filtered resultstrue
    filterSelectAllTextStringLabel for select all filtered resultsSelect all filtered results
    filterUnSelectAllTextStringLabel for unselect all filtered resultsUnSelect all filtered results
    maxHeightNumberMax height of dropdown list in px300
    badgeShowLimitNumberLimit number of badges shown in inputAll
    classesStringCustom classes for the dropdown selector''
    limitSelectionNumberLimit number of items that can be selectednone
    disabledBooleanDisable the dropdownfalse
    searchPlaceholderTextStringCustom text for search placeholder'Search'
    groupByStringField name to group the list bynone
    selectGroupBooleanSelect a group at once (requires groupBy)false
    searchAutofocusBooleanAutofocus search input fieldtrue
    labelKeyStringProperty name rendered as labelitemName
    primaryKeyStringProperty used to identify the objectid
    positionStringPosition of dropdown ('top' or 'bottom')bottom
    noDataLabelStringLabel when no data is available'No Data Available'
    searchByArrayProperties to search by (e.g. ['itemName', 'id'])[]
    lazyLoadingBooleanEnable lazy loading for large datasetsfalse
    showCheckboxBooleanShow or hide checkboxes in the listtrue
    addNewItemOnFilterBooleanAdd text as new item if not found during filterfalse
    addNewButtonTextStringText for the 'Add' button'Add'
    escapeToClosebooleanPress escape key to close dropdowntrue
    autoPositionbooleanEnable automatic top/bottom positioningtrue
    tagToBodybooleanAppend dropdown to body or nottrue
  10. Reference: Dropdown Events

    master

    The following events are emitted by the <angular2-multiselect> component:

    • onSelect: Returns the selected item.
    • onDeSelect: Returns the un-selected item.
    • onSelectAll: Returns the list of all selected items.
    • onDeSelectAll: Returns an empty array.
    • onGroupSelect: Returns the selected group items as an array.
    • onGroupDeSelect: Returns the un-selected group items as an array.
    • onOpen: Fired after the dropdown opens.
    • onClose: Fired when the dropdown is closed.
    • onScrollToEnd: Fired when the list is scrolled to the end (useful for lazy loading).
    • onAddFilterNewItem: Fired when clicking the 'Add' button during filtering.
    • onFilterSelectAll: Fired when all filtered items are selected.
    • onFilterDeSelectAll: Fired when all filtered items are de-selected.