Skip to main content

Notifications

Power Apps - Building Power Apps
Unanswered

Decrementing count in collection when collection is edited

(0) ShareShare
ReportReport
Posted on by 205

Hi all,

 

I have created a gallery which uses a collection as its data source, this collection is generated when the screen becomes visible as such - ClearCollect(Findings, {no:1}). I also have a button that creates additional rows in the collection - Patch(auditFindings, Defaults(Findings), {no:CountRows(Findings)+1). In the gallery, I have a field called reportnumber-00X whereby X is thisitem.no

 

The problem comes in when I try to delete the entry in the gallery (done through a button in the gallery with onSelect property of RemoveIf(Findings, no=thisitem.no) . I would like the field reportnumber-00X to update automatically. E.g. if i have reportnumber-001, reportnumber-002, reportnumber-003, reportnumber-004 and I delete reportnumber-002, the gallery should show reportnumber-001, reportnumber-002, reportnumber-003 instead of reportnumber-001, reportnumber-003, reportnumber-004. How can this be achieved? TIA!

  • MarkRahn Profile Picture
    MarkRahn 918 on at
    Re: Decrementing count in collection when collection is edited

    Hi @ccqn ,

     

    I have had to do something similar. This is how I achieved what you are trying to do:

    1) In the OnVisible of a Screen, use:

     

    ClearCollect(Findings, {no:1,Title:"Item1"})

     

    2) Add a Gallery to the Screen and a "+" icon above it:

    MarkBandR_0-1706112199515.png

    3) Set the Gallery Data Source to "Findings". Add two Labels to the Template for the Gallery and link them to the "no" value and the "Title" value.

    4) For the "+" icon, set the OnSelect to:

     

    Collect(Findings, {no:CountRows(Findings)+1, Title:"Item" & Text(CountRows(Findings)+1)})

     

    5) In the Template for the Gallery, add a "Trash"can icon. Set the OnSelect to:

     

    RemoveIf(
     Findings,
     no = ThisItem.no
    );
    ClearCollect(
     Findings,
     With(
     {records: Findings},
     ForAll(
     Sequence(CountRows(records)),
     Patch(
     Last(
     FirstN(
     records,
     Value
     )
     ),
     {no: Value}
     )
     )
     )
    );

     

     

    With the example, you should now be able to add and delete items. The "no" field will update. See below:

    MarkBandR_1-1706112608578.png

    The item with the Title "Item3" was deleted and the other items renumbered.

     

    I pulled this example together using this article:

    https://powerapps.co.nz/auckland/index.php/our-blog/41-add-alternating-row-colours-to-power-apps-gallery

     

    If I've answered your question or solved your problem, please mark this question as answered. This helps others who have the same question find a solution quickly via the forum search. If you liked my response, please consider giving it a thumbs up.

    Thanks.

    -Mark

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,343

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,703

Leaderboard