Skip to main content

Notifications

Shift numbered items in a gallery by more than one row

There are many posts on reordering a gallery with up/down buttons, but what if you want to shift an item a large number of places and re-order everything in between in one action ? The assumption here is that the gallery contains a sequential numeric field, is sorted by this field and the user wants to change a row item to another position with the result that the gallery will then be sorted in the new order.
Firstly, there are two possibilities on the number shift - it could be larger or smaller than its original position, so that needs to be dealt with separately. I have also allowed for there being no other unique identifier (such as the ID) in the record, so will use the number field only. There are four basic actions required

  • Capture the old and new positions before anything is moved.
  • Get the item to be moved "out of the way".
  • Add or subtract 1 from the position of the rows "in between" the old and new numbers.
  • Put the shifted row into its new position.

So the resulting code - using a Text Input for the RowNo field and this OnChange of that control

With(
 {
 wOld: ThisItem.RowNo,
 wNew: Value(Self.Text)
 },
 Patch(
 DataSource,
 ThisItem,
 {RowNo: 0}
 );
 If(
 wOld > wNew,
 ForAll(
 Filter(
 Sort(
 DataSource,
 RowNo,
 SortOrder.Descending
 ),
 RowNo >= wNew && RowNo < wOld
 ) As aOrder,
 Patch(
 DataSource,
 LookUp(
 DataSource,
 RowNo = aOrder.RowNo
 ),
 {RowNo: aOrder.RowNo + 1}
 )
 ),
 wNew > wOld,
 ForAll(
 Filter(
 Sort(
 DataSource,
 RowNo
 ),
 RowNo <= wNew && RowNo > wOld
 ) As aOrder,
 Patch(
 DataSource,
 LookUp(
 DataSource,
 RowNo = aOrder.RowNo
 ),
 {RowNo: aOrder.RowNo - 1}
 )
 )
 );
 Patch(
 DataSource,
 LookUp(
 DataSource,
 RowNo = 0
 ),
 {RowNo: wNew}
 )
)

achieves these four steps and re-orders the gallery

 

Comments

*This post is locked for comments