I'm working on a form and I'm using Angular Reactive Forms. I need to have multi-select in my form, I did get it done by pressing ctrl and selecting multiple options. I would like to be able to use multiselect without pressing the ctrl key. Would be possible?
Form Component:
this.productForm = new FormGroup({
'items': new FormControl(this.product.items, Validators.required),
});
Template:
<div class="form-group">
<select multiple="multiple" class="form-control" id="items" formControlName="items" required>
<option *ngFor="let item of itemList" [value]="item.id">{{item.name}}</option>
</select>
</div>
UPDATE:
I tried the greg95000's solution, but it's only a partially working solution.
<div class="form-group">
<select multiple="multiple" class="form-control" id="items" formControlName="items" required>
<option (mousedown)="onMouseDown($event)" (mousemove)="$event.preventDefault()" *ngFor="let item of items" [value]="item.id">{{item.name}}</option>
</select>
</div>
public onMouseDown(event: MouseEvent) {
event.preventDefault();
event.target['selected'] = !event.target['selected'];
}
THIS SOLUTION BREAKS THE DATA BINDING