mat-select-autocomplete icon indicating copy to clipboard operation
mat-select-autocomplete copied to clipboard

Single select selection will not unselect multiselects

Open TomasKatz opened this issue 5 years ago • 3 comments

Tag configuratuion:

<mat-select-autocomplete 
[options]="allEvents" 
[multiple]="true" 
[value]="'id'" 
[display]="'name'" 
name='parentEvent' 
ngDefaultControl
formControlName="parentEvent">
</mat-select-autocomplete>	

options Object:

[
    {
       name:'a',
       id:1
    },
    {
       name:'b',
       id:2
     },
    {
       name:'c',
       id:3
     }
    //... about 200 more items
]

Way to reproduce: Select an the "all options checkbox" -> see that all options were selected -> click again on the "select all/ unselect all" checkbox -> result: selected options do not get unselected, they stay selected.

TomasKatz avatar Sep 26 '19 11:09 TomasKatz

I probably have a clue why the issue occurs: When unselecting, this code runs:

  const filteredValues = this.getFilteredOptionsValues();
  this.selectedValue = this.selectedValue.filter(item => !filteredValues.includes(item));
...
    getFilteredOptionsValues() {
        /** @type {?} */
        const filteredValues = [];
        this.filteredOptions.forEach(option => {
            filteredValues.push(option.value);
        });
        return filteredValues;
    }

So for getFilteredOptionsValues to return correct values, the option must have its value filled. You probably want something like option => {filteredValues.push(option[this.value]);}); but it seems like you cannot reach that because of the inner function.

Not sure how to best fix this though, I've not looked into the code deep enough.

HansWouters avatar Nov 29 '19 12:11 HansWouters

I have the same problem, Can you resolve this?

martinoliete avatar Jun 09 '20 14:06 martinoliete

I had the same problem, Try to map your options list like that :

this.filteredGovItems = this.allGovs.map(x => { return { display: x.name, value: x.id }; });

it will work correctly.

Moazmostafaa avatar Oct 26 '21 17:10 Moazmostafaa