1

I am using ng-multiselect-dropdown

 <ng-multiselect-dropdown [placeholder]="'Choose workers'" [data]="allWorkers"
                        [(ngModel)]="workers.selectedItems" [settings]="dropdownSettings"
                        (onSelect)="onItemSelect($event)" (onSelectAll)="onSelectAll($event)">
                      </ng-multiselect-dropdown>
                      {{selectedItems}}

I got my workers from api

allWorkers: string[] = [];

in my ngOnInit

this.workersService.getUsers().subscribe(
  (val: any[]) => {
    this.allWorkers = val.map(user => user.name + ' ' + user.city);
    console.log(this.allWorkers);
  }
)

all works fine but.. If I got

  onItemSelect(item: any) {

  }

I want to delete value from list if is selected

So if I have input:

  • Name Surname
  • Name Surname2
  • Name Surname3

and I select Name Surname2 I want to have only to choose

  • Name Surname
  • Name Surname3

I tried something like this

onItemSelect(item: any) {
 const index = this.allWorkers.indexOf(item, 0);
    if (index > -1) {
      this.allWorkers.splice(index, 1);
    }
    }

But doesn't work.

1
  • 1
    please get rid of your any types and replace them with whatever type is needed (if you use a good editor like VS Code, it will even show you what type will be inserted), it will make your life so much easier Commented Aug 24, 2020 at 9:30

1 Answer 1

6

You should use the filter method :

this.allWorkers = this.allWorkers.filter(worker => worker !== item);
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.