Reputation: 261
How can I implement multiple select drop down with grouping in my angular 2 application? I need the drop down like the images linked in this question How can implement grouping in ng-select of Angular2?. Kindly help me out.....I am stuck on this from last few days
I have tried angular-ngselect like the following but its :
Component Html:
<form [formGroup]="form" class="selector-form">
<div class="marTop20">
<ng-select [options]="options1"
[multiple]="multiple1"
placeholder="Select multiple"
formControlName="selectMultiple"
(opened)="onMultipleOpened()"
(closed)="onMultipleClosed()"
(selected)="onMultipleSelected($event)"
(deselected)="onMultipleDeselected($event)">
</ng-select>
</div>
</form>
Component code in ts file:
export class FilterClientSelectorComponent implements OnInit {
form: FormGroup;
multiple1: boolean = true;
options1: Array<any> = [];
selection: Array<string>;
@ViewChild('preMultiple') preMultiple;
logMultipleString: string = '';
constructor() {
let numOptions = 100;
let opts = new Array(numOptions);
for (let i = 0; i < numOptions; i++) {
opts[i] = {
value: i.toString(),
label: i.toString(),
groupname:'a'
};
}
this.options1 = opts.slice(0);
}
ngOnInit() {
this.form = new FormGroup({});
this.form.addControl('selectMultiple', new FormControl(''));
}
private scrollToBottom(elem) {
elem.scrollTop = elem.scrollHeight;
}
}
And its giving me multiple select dropdown but not grouping:
Current Output:
Required output:
Upvotes: 4
Views: 14553
Reputation: 344
I created two demos using most used UI components at angular
Using angular material you can check this demo https://stackblitz.com/edit/moa-option-group-multi-select?file=src%2Fapp%2Fselect-optgroup-example.html
Using primeng you can check this demo also https://stackblitz.com/edit/moa-primeng-multiselect-grouped
Upvotes: 0
Reputation: 150
Here is Angular's very light weight library for Multiselect-dropdwon. It is provide a wide range of features like Group-By, search, custom-search, Virtual-scrolling etc. You can refer: https://cuppalabs.github.io/angular2-multiselect-dropdown/#/groupby
Upvotes: 0
Reputation: 741
I have used material design to implement same functionality.. And This is how i did it.. In "fileName.component.html"
<mat-form-field>
<mat-select placeholder="Pokemon" [formControl]="pokemonControl" multiple>
<mat-option>-- None --</mat-option>
<mat-optgroup *ngFor="let group of pokemonGroups" [label]="group.name"
[disabled]="group.disabled">
<mat-option *ngFor="let pokemon of group.pokemon" [value]="pokemon.value">
{{pokemon.viewValue}}
</mat-option>
</mat-optgroup>
</mat-select>
</mat-form-field>
Don't Forget to add "multiple".. You can see that in second line of my .html file.. And In "fileName.component.ts"
import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';
export interface Pokemon {
value: string;
viewValue: string;
}
export interface PokemonGroup {
disabled?: boolean;
name: string;
pokemon: Pokemon[];
}
/** @title Select with option groups */
@Component({
selector: 'select-optgroup-example',
templateUrl: 'select-optgroup-example.html',
styleUrls: ['select-optgroup-example.css'],
})
export class SelectOptgroupExample {
pokemonControl = new FormControl();
pokemonGroups: PokemonGroup[] = [
{
name: 'Grass',
pokemon: [
{value: 'bulbasaur-0', viewValue: 'Bulbasaur'},
{value: 'oddish-1', viewValue: 'Oddish'},
{value: 'bellsprout-2', viewValue: 'Bellsprout'}
]
},
{
name: 'Water',
pokemon: [
{value: 'squirtle-3', viewValue: 'Squirtle'},
{value: 'psyduck-4', viewValue: 'Psyduck'},
{value: 'horsea-5', viewValue: 'Horsea'}
]
},
{
name: 'Fire',
disabled: true,
pokemon: [
{value: 'charmander-6', viewValue: 'Charmander'},
{value: 'vulpix-7', viewValue: 'Vulpix'},
{value: 'flareon-8', viewValue: 'Flareon'}
]
},
{
name: 'Psychic',
pokemon: [
{value: 'mew-9', viewValue: 'Mew'},
{value: 'mewtwo-10', viewValue: 'Mewtwo'},
]
}
];
}
If you want more information on this just checkout this link
Upvotes: 2
Reputation: 1681
You can use angular-2-dropdown-multiselect, it has group option as well.
you have to format data according to this
// Labels / Parents
myOptions: IMultiSelectOption[] = [
{ id: 1, name: 'Car brands', isLabel: true },
{ id: 2, name: 'Volvo', parentId: 1 },
{ id: 3, name: 'Honda', parentId: 1 },
{ id: 4, name: 'BMW', parentId: 1 },
{ id: 5, name: 'Colors', isLabel: true },
{ id: 6, name: 'Blue', parentId: 5 },
{ id: 7, name: 'Red', parentId: 5 },
{ id: 8, name: 'White', parentId: 5 }
];
https://plnkr.co/edit/3Ett6sL2emzyrM3YiHkP?p=preview
https://github.com/softsimon/angular-2-dropdown-multiselect
Upvotes: 1
Reputation: 86790
Try this one primeng's Multi Select Dropdown with number of attributes and features may help you.
http://www.primefaces.org/primeng/#/multiselect
http://www.primefaces.org/primeng/#/picklist
Upvotes: 2