I was recently inspired by Power Apps Reorder Gallery Items – April Dunnam – SharePoint Siren, an “oldie but goodie” article by April Dunnam.
I decided to build my own implementation.
I wanted my version to not have a strong dependency on if there was an n-1
or n+1
item in the list, so I modified accordingly.
I used two collections, colAvailableItems
and colSelectedItems
and two galleries, galAvailableItems
and galSelectedItems
.
The items in the collection are made up of two properties: Ordinal
and Name
.
The galAvailableItems
is sorted by name, while galSelectedItems
is sorted by Ordinal
.
The code for moving items from galAvailableItems
to galSelectedItems
is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
UpdateContext( { ctxLastItem: Last( SortByColumns( colSelectedItems, "Ordinal" ) ) } ); Collect( colSelectedItems, { Ordinal: ctxLastItem.Ordinal + 1, Name: galAvailableItems.Selected.Name } ); Remove( colAvailableItems, LookUp( colAvailableItems, Name = galAvailableItems.Selected.Name ) ); |
The code for moving items from galSelectedItems
to galAvailableItems
is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Collect( colAvailableItems, galSelectedItems.Selected ); Remove( colSelectedItems, LookUp(colSelectedItems, Name = galSelectedItems.Selected.Name) ); ForAll( Sequence(CountRows(colSelectedItems)) As Index, Patch( colSelectedItems, Index( colSelectedItems, Index.Value ), {Ordinal: Index.Value} ) ); |
The ForAll
is what renumbers the items in the collection to assure there are no gaps in the Ordinal
.
The code for moving an item up in the galSelectedItems
is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
UpdateContext( { ctxPreviousItem: Last( Filter( SortByColumns( colSelectedItems, "Ordinal" ), Ordinal < ThisItem.Ordinal ) ) } ); Update( colSelectedItems, ctxPreviousItem, { Name: ctxPreviousItem.Name, Ordinal: ThisItem.Ordinal } ); Update( colSelectedItems, ThisItem, { Name: ThisItem.Name, Ordinal: ctxPreviousItem.Ordinal } ); |
The code for moving an item down in galSelectedItems
is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
UpdateContext( { ctxLastItem: First( Filter( SortByColumns( colSelectedItems, "Ordinal" ), Ordinal > ThisItem.Ordinal ) ) } ); Update( colSelectedItems, ctxLastItem, { Name: ctxLastItem.Name, Ordinal: ThisItem.Ordinal } ); Update( colSelectedItems, ThisItem, { Name: ThisItem.Name, Ordinal: ctxLastItem.Ordinal } ); |
You can download the complete solution at https://mattruma.com/wp-content/uploads/2024/03/GallerySolutionMoveBetweenLists_1_0_0_1.zip.
Discover more from Matt Ruma
Subscribe to get the latest posts sent to your email.