MultiSelect dropdown selected values at top?

UI Components for Angular
Post Reply
claycasto
Posts: 1
Joined: 27 Jul 2020, 17:35

27 Jul 2020, 17:44

Hello,

I am wondering if there is a way to move items to the top of the dropdown list when they are selected.
This way, if a user selects many things but needs to unselect one item, they don't have to go searching for that item again just to uncheck.
(The dropdown has thousands of items)

Any help is appreciated! Thank you! =)

shaolinshaosson
Posts: 1
Joined: 21 Aug 2020, 14:27

23 Aug 2020, 18:23

Maybe solved but for others wanting to do the same, I think this will do the trick.

Code: Select all

	sortArrayBySelected(items: any = [], selected: any = [], compareKey: string)
	{
		items.sort((a, b) => {
			let findAIndex = selected.findIndex((i => i[compareKey] == a[compareKey]));
			let findBIndex = selected.findIndex((i => i[compareKey] == b[compareKey]));

			if(findAIndex > -1 && findBIndex == -1)
				return -1;
			else if(findAIndex == -1 && findBIndex > -1)
				return 1;

			return 0;
		});
		return items;
	}
Usage:

Code: Select all

sortArrayBySelected(categories, selectedCategories, 'categoryID');

Post Reply

Return to “PrimeNG”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 20 guests