Tuesday, February 14, 2023

PowerApps: Re-order Items In Gallery

Hello Friends,
Welcome back with another post on PowerApps. Today, we will learn about re-ordering feature in gallery. Surprised, as there is no such built-in feature in gallery. right, therefore, we will build the logic here.
We will apply re-ordering in 2 ways-
  1. Swap the position with Previous / Next item. Example: Swap 2nd position item with 3rd position item or vice-versa.
  2. Position the item at a particular number. Example: Bring the 10th position item to 2nd position.
Let's start.
  1. Open the PowerApps maker portal and create a new app and give a suitable name.
  2. Now click on App and choose the OnStart property and add logic for creating a collection.
    1. ClearCollect(collSampleData,
      {SeqNo:1,Title:"Mr",FirstName:"Aaron",LastName:"Finch"},
      {SeqNo:2,Title:"Mr",FirstName:"Sanjiv",LastName:"Verma"},
      {SeqNo:3,Title:"Mr",FirstName:"Robert",LastName:"Rosch"},
      {SeqNo:4,Title:"Ms",FirstName:"Aakriti",LastName:"Singh"},
      {SeqNo:5,Title:"Ms",FirstName:"Reshma",LastName:"Sharma"},
      {SeqNo:6,Title:"Mr",FirstName:"Kundan",LastName:"Gupta"},
      {SeqNo:7,Title:"Ms",FirstName:"Rajni",LastName:"Chugh"},
      {SeqNo:8,Title:"Ms",FirstName:"Ketty",LastName:"Woods"},
      {SeqNo:9,Title:"Mr",FirstName:"Bob",LastName:"Hamilton"},
      {SeqNo:10,Title:"Ms",FirstName:"Mary",LastName:"Christina"}
      );
  3. Now click on elipses (...) of App and select Run OnStart. It will initialize the collection.
  4. Now click on Insert and search for blank flexible height gallery.
  5. Select this control to add on screen. It will ask you the Data source. Select the collection collSampleData we have created above.
  6. Now resize the app to cover the entire screen or resize as per your application requirements. (optional).
  7. Rename the ggallery as Gallery_ReOrder.
  8. Choose the TemplateSize property and set it's value as 50. Then set the TemplatePadding to 0.
  9. It's time to add controls in this template. Click on Edit Template icon.
  10. Start adding control and set properties-
  11. Icon - Arrow up
    1. Name: IconUp
    2. X: 0
    3. Y: 0
    4. Width: 20
    5. Height: Parent.TemplateHeight
    6. Padding: Top / Bottom / Left / Right: 0
    7. Visible: ThisItem.SeqNo <> First(Sort(collSampleData,SeqNo,Ascending)).SeqNo
    8. OnSelect: 
      1. Select(Parent);
        Set(vrCurrentSeq,Value(ThisItem.SeqNo));
        Patch(collSampleData,ThisItem,{SeqNo:0});
        Patch(collSampleData,First(Filter(collSampleData,SeqNo=vrCurrentSeq-1)),{SeqNo:vrCurrentSeq});
        Patch(collSampleData,First(Filter(collSampleData,SeqNo=0)),{SeqNo:vrCurrentSeq-1});
    9. The concept used here is capture the current item sequence. The get the previous item and set it's seq equal to current one. Then set the current selected item sequence to 1 less than that of current sequence.
  12. Icon - Arrow down
    1. Name: IconDown
    2. X: IconUp.X+IconUp.Width
    3. Y: IconUp.Y
    4. Width: IconUp.Width
    5. Height: IconUp.Height
    6. Padding: Top / Bottom / Left / Right: 0
    7. Visible: ThisItem.SeqNo <> First(Sort(collSampleData,SeqNo,Descending)).SeqNo
    8. OnSelect: 
      1. Select(Parent);
        Set(vrCurrentSeq,Value(ThisItem.SeqNo));
        Patch(collSampleData,ThisItem,{SeqNo:0});
        Patch(collSampleData,First(Filter(collSampleData,SeqNo=vrCurrentSeq+1)),{SeqNo:vrCurrentSeq});
        Patch(collSampleData,First(Filter(collSampleData,SeqNo=0)),{SeqNo:vrCurrentSeq+1});
    9. The concept used here is capture the current item sequence. The get the next item and set it's seq equal to current one. Then set the current selected item sequence to 1 plus than that of current sequence.
  13. Text label
    1. Name: LblSeqNo
    2. X: IconDown.X+IconDown.Width
    3. Y: IconDown.Y
    4. Width: 50
    5. Height: IconDown.Height
    6. Text: ThisItem.SeqNo
    7. Align: Align.Right
  14. Text label
    1. Name: LblTitle
    2. X: LblSeqNo.X+LblSeqNo.Width
    3. Y: LblSeqNo.Y
    4. Width: 70
    5. Height: LblSeqNo.Height
    6. Text: ThisItem.Title
  15. Text label
    1. Name: LblFirstName
    2. X: LblTitle.X+LblTitle.Width
    3. Y: LblTitle.Y
    4. Width: 150
    5. Height: LblTitle.Height
    6. Text: ThisItem.FirstName
  16. Text label
    1. Name: LblLastName
    2. X: LblFirstName.X+LblFirstName.Width
    3. Y: LblFirstName.Y
    4. Width: 150
    5. Height: ILblFirstName.Height
    6. Text: ThisItem.LastName
  17. Drop down
    1. Name: ddSortOrder
    2. X: LblLastName.X+LblLastName.Width
    3. Y: LblLastName.Y
    4. Width: 150
    5. Height: LblLastName.Height
    6. Default: ThisItem.SeqNo
  18. For Items property, first we will add below code in App >> OnStart (after the collSampleData collection creation)
    1. Set(vrCurrentSeq,First(Sort(collSampleData,SeqNo,Ascending)).SeqNo);
      ClearCollect(collSequence,Sequence(CountRows(collSampleData),vrCurrentSeq));
    2. Now click on elipses (...) of App and select Run OnStart. It will initialize the collection collSequence. 
  19. Now set the Items property of ddSortOrder as collSequence.
  20. Save the app.
  21. The remaining part is to write the login for OnChange property of ddSortOrder. It iis little bit complex. Here we need to check if the new position of item is greater than the current position or less. Based upon that, we need to reposition the items. For example, see the below screenshot.
  22. If we are changing order from 4 to 8 then Yellow highlighted items order also need to be updated. Similarly, if we are changing order from 4 to 2, then Yellow highlighted items order also need to be updated.
  23. Let's write the logic for ddSortOrder >> OnChange property.
    1. Select(Parent);
      Set(vrCurrentSeq,Value(ThisItem.SeqNo));
      Set(vrDesiredSeq,Value(ddSortOrder.Selected.Value));
      Patch(collSampleData,ThisItem,{SeqNo:0});
      If(vrDesiredSeq>vrCurrentSeq,
          ClearCollect(collNewSequence,Sequence(vrDesiredSeq-vrCurrentSeq,vrCurrentSeq+1));
          ForAll(collNewSequence,Patch(collSampleData,First(Filter(collSampleData,SeqNo=Value)),{SeqNo:Value-1}));,
          ClearCollect(collNewSequence,Sequence(vrCurrentSeq-vrDesiredSeq,vrDesiredSeq));
          ForAll(collNewSequence,Patch(collSampleData,First(Filter(collSampleData,SeqNo=Value)),{SeqNo:Value+1})););
      Patch(collSampleData,First(Filter(collSampleData,SeqNo=0)),{SeqNo:vrDesiredSeq});
  24. Last, to give a beautiful appearance to the grid, we will fill a background color to the alternate rows. For this, update the TemplateFill property of GalleryReOrder as below.
    1. If(Mod(ThisItem.SeqNo,2)=0,RGBA(222,222,222,1),RGBA(0,0,0,0))
  25. Save the app, publish and Run/Play.
  26. If you play this app and try to change the order, you will find that the order gets changed but the grid is not showing data properly. The reason is that, we had not applied the Sorting on grid datasource.. Edit the app and choose the gallery GalleryReOrder >> Items property. Here you will find that the datasource collSampleData is directly associated. Change it to -
    1. Sort(collSampleData,Value(SeqNo),Ascending)
  27. Now, save/publish and Run/Play.
  28. I swapped order 3 to 2 by clicking Up icon and the results are-
    1. Before
    2. After
  29. Now, I am swapping order 9 to 10 by clicking Down icon and the results are-
    1. Before
    2. After
  30. Now, I will reorder the item of position 7 to position 3 using drop down and the results are-
    1. Before
    2. After

With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.