3

I am implementing multiple select dropdown in my Angular2 project by following this link. But now i have to show grouping in this multiple select dropdown so how can i implement this? Or is there any other plugin or something like that i can follow.

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;
    }
}

I need the output like the following screen shot:

Output Page:

enter image description here

1 Answer 1

0

Grouping is supported natively by ng-select.

https://ng-select.github.io/ng-select#/grouping

<ng-select [items]="accounts"
           bindLabel="name"
           bindValue="name"
           groupBy="country"
           [multiple]="true"
           [hideSelected]="true"
           [(ngModel)]="selectedAccount">
    <ng-template ng-optgroup-tmp let-item="item">
        {{item.country || 'Unnamed group'}}
    </ng-template>
</ng-select>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.