Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PrimeNG p-multiSelect: Keep Selected Items on Top #17544

Open
CavidH opened this issue Jan 31, 2025 · 0 comments
Open

PrimeNG p-multiSelect: Keep Selected Items on Top #17544

CavidH opened this issue Jan 31, 2025 · 0 comments
Labels
Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible

Comments

@CavidH
Copy link

CavidH commented Jan 31, 2025

I'm using PrimeNG's p-multiSelect component with multiple selection enabled. Every time I select an item, I don't want to search for it again in the list. Instead, I want the selected items to always appear at the top of the dropdown.

Current Behavior:

When I select multiple items, they stay in their original position in the list.
If the list is long, I have to scroll or search again to find my selected items.
Expected Behavior:

Selected items should automatically move to the top of the list.
The remaining unselected items should stay below the selected ones.
What I've Tried:
I tried sorting the options manually when the selection changes, but I couldn't achieve the desired behavior.

<p-multiSelect 
  [options]="sortedOptions" 
  [(ngModel)]="selectedValues" 
  optionLabel="name" 
  (onChange)="sortSelectedOptions()">
</p-multiSelect>



sortedOptions: any[] = [];
selectedValues: any[] = [];

options = [
  { name: 'Apple', value: 'apple' },
  { name: 'Orange', value: 'orange' },
  { name: 'Banana', value: 'banana' },
  { name: 'Mango', value: 'mango' }
];

ngOnInit() {
  this.sortedOptions = [...this.options];
}

sortSelectedOptions() {
  this.sortedOptions = [
    ...this.selectedValues, 
    ...this.options.filter(opt => !this.selectedValues.includes(opt))
  ];
}


@github-actions github-actions bot added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible
Projects
None yet
Development

No branches or pull requests

1 participant