Gokz
Gokz

Reputation: 3

primeng multiselect dropdown, move the selected value to top

Primeng - Multiselect

Wanted to move the selected to top in the multiselect dropdown of primeng. How to achieve it?

I Have tried to sort the selected values based on index. that's not working.

    items.sort((a:any, b:any) => {
            let IndexA = selected.findIndex(((i:any) => i == a[key]));
            let IndexB = selected.findIndex(((i:any) => i == b[key]));
  return 0;

});

Upvotes: 0

Views: 1774

Answers (1)

Mouayad_Al
Mouayad_Al

Reputation: 707

  1. You can do the following when using (onChange)="onSelectionChange($event)" pass $event to catch array of selected elements.
  2. create function with event parameter onSelectionChange(event:any).
  3. Inside the function you can iterate over selected values and splice the selected value from the list then unshift the selected element that will add the element to the top of the array.
onSelectionChange(event:any):void {
  event?.value.foreach(selectedItem => {
     this.items.splice(this.items.indexOf(selectedItem),1) // remove the item from list
     this.items.unshift(selectedItem)// this will add selected item on the top 
  })
}

Upvotes: 2

Related Questions