0
votes

Project is Powerapps with a SQL backend.

I have a list of (you can think of them as stages). The stages can be customized.You can add new stages and you can re-order them. On other screens (tasks) you can set the stage of that task.

I wanted to have a list where you could drag items up and down the list to change the order, be we can't figure that out. So instead, I came up with the idea to have an "order" column along with the name of the steps so you can add an item and via drop-down set where it sat in the order. Now we're not sure how to handle updating all the members of the list when we add or remove items.

Is there an existing best practice or design pattern in PowerApps to handle this scenario?

1

1 Answers

0
votes

I'm afraid you can't sort a collection directly. You can sort it when doing something with the fields using Sort() or SortByColumn() functions. If you really want a collection sorted, you will need to copy the collection to another one like:
ClearCollect(CollectionSorted; SortByColumns(CollectionNotSorted; "Order"))
To do what you intended (if I understand correctly), this should work. This is the layout I used:
enter image description here
Two text input to my collection columns, one for step name and one for the order you want it to be. One Dropdown with all the steps, so the user can choose what item to remove from the collection. To add the step, in the OnSelect property of the Add button used this code:

If(
    CountRows(
        Filter(
            StepCol;
            Order = Value(lbl_Order.Text)
        )
    ) > 0;
    UpdateIf(
        StepCol;
        Order >= Value(lbl_Order.Text);
        {Order: Order + 1}
    );;
    Collect(
        StepCol;
        {
            StepName: lbl_StepName.Text;
            Order: Value(lbl_Order.Text)
        }
    );
    Collect(
        StepCol;
        {
            StepName: lbl_StepName.Text;
            Order: Value(lbl_Order.Text)
        }
    )
)

And for the remove button:

UpdateContext({var: Dropdown1.Selected.Order});;
Remove(
    StepCol;
    LookUp(
        StepCol;
        Order = Dropdown1.Selected.Order
    )
);;
UpdateIf(
    StepCol;
    Order > var;
    {Order: Order - 1}
);;

If you don't understand the code or need some more help or explanaition, let me know. Best Regards