Skip to content
Advertisement

Remove unselected value from dropdown array – ngx-select-dropdown

context: Angular applications using the ngx-select-dropdown. A user can select multiple values, these values get sorted into “buckets” and sent to the api.

Issue: I am unable to remove a selected item from the end array – this.filterState. I have added a check to ensure that an item cant be added more than once – a click to select the item and a click to deselect the item. I thought that this would be a good time to remove the item with a splice as the user would have clicked to deselect if it already exists in the array – however, the dropdown does not pass the value of a deselected object it just removes it from the value array of the dropdown.

JavaScript

Proposed solution: I think it will need some sort of comparison upon event change against the dropdown value object and the array that was previously sent to the api.

End goal: To be able to add and remove objects from the sorted array

Here is a stackblitz that I put together...

app.component.ts

JavaScript

https://stackblitz.com/edit/ngx-select-dropdown-xkfbyr?file=app%2Fapp.component.ts

Advertisement

Answer

A simple fix in the end – I was trying to over complicate things. I needed to reset the this.filterState as the value from the dropdown will include every value from before, minus the ones that were deselected.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement