Powerapps - Variables in gallery

Viewed 2985

I'm new to powerapps and have encountered a problem. I'm creating a "Shopping cart" and want to the user to be able to add or substract 1, by pressing the two icons.

I have tried using a variable, but when i press the green "+", it adds 1 to the entire gallery, and not just the selected item. How can I isolate the variables to only operate within the item, in which i press the button? I hope it makes sens.enter image description here

3 Answers

You should not use variables, use a collection instead. When the user presses the + button, Collect() the item with 1 unit if it does not exist or Update() the current units to +1. Similarly with the x button. Something like this for the OnSelect property of the + button:

If(
    IsEmpty(Filter(MyCollection, Id = ThisItem.Id)),
    Collect(MyCollection, {Id: ThisItem.Id, Quantity: 1}),
    UpdateIf(MyCollection, Id = ThisItem.Id, {Quantity: Quantity+1})
    )

The text to show in the gallery will be something like:

LookUp(MyCollection, Id=ThisItem.Id, Quantity)

Its more like this; If(IsEmpty(Filter(MyCollection,FoodID = Gallery.Selected.FoodID)),Collect(MyCollection,{FoodID: Gallery.Selected.FoodID, Quantity: 1}),UpdateIf(MyCollection,FoodID=Gallery.Selected.FoodID,{Quantity:Quantity+1}))

I personally would use a collection

Every Item the user wanted to add they could click on a + icon and it would add that item to the collection

Collect(ShoppingList,{Items here})

I would then link the collection to the gallery, inside the gallery then add the delete icons and edit icons. You may want a delete all using Clear(ShoppingList) this will clear the collection.

Once the user is happy they can click checkout This would save the collection to a sharepoint list or your DB of choice.

To add the collection to the DB set the button to OnSelect = Collect(ShoppingList,DB name)

Related