3

I have the following code to get the list of selected options of a multiple selection component.

<select multiple class="custom-select" [(ngModel)]="selectedOptions">
     <option *ngFor="let option of all_options" [value]="option">                  
         {{option.name}}
     </option>
</select>

The binding to the selectedOptions array is working fine however, does anyone know if it is possible to save the selected objects in the order clicked by the user?

Thank you!

Raissa Amaral
  • 31
  • 1
  • 3
  • try change event, make a handler function for the change event, and in that method push the changed value into an array if it is not present in the array. – Abhishek Kumar Jul 17 '18 at 09:46

3 Answers3

2

You can use

(ngModelChange)="someFunction(selectedValue)"

for emitting an event when select value changes. Pass selected value to it. Then you can push the value into an array. In this way you will have an array in the clicked order.

Dan Soper
  • 639
  • 8
  • 18
Sujay
  • 613
  • 1
  • 5
  • 16
1

You may use something like below.

<select (change)="onChange($event.target.value)">

then you can implement onChange method and store responses as required.

onChange(Value) {
    this.valuesArray.push(Value); // or do whatever as required
}
Malindu Sandaruwan
  • 1,477
  • 2
  • 13
  • 27
  • Thanks for the response! However I did think on this option but I was wondering if there was another way to do this that didn't rely on the selection change event. – Raissa Amaral Jul 17 '18 at 14:03
0

You should to use (valueChange): my example is:

<mat-select (valueChange)="functionToChangeValue($event)">
<mat-option *ngFor="let item of items" [value]="item">
{{item.name}}
</mat-option>
</mat-select>

then:

functionToChangeValue(event: any) {
this.data = event;
}

event works with arrays in multiple select.

I hope this help anybody.

Chester mi niño
  • 71
  • 1
  • 2
  • 7