Tuesday, October 5, 2021

PowerApps: CheckBox (Extract Selected Items)

 Hello Friends,

Welcome back with another post on PowerApps. Today, we will discuss about CheckBox control. Sometimes we have a situation where, we have to extract the selected checkbox values to show somewhere else. Means, the moment, we select a check box, it should get reflect in another gallery (where only selected values are getting shown). Upon Check/Uncheck of each item, this gallery should be updated. Let's see, how can we achieve this-

  1. Create a blank PowerApps. I am creating a tablet mode PowerApp.
  2. Now add a button and a vertical blank gallery.
  3. Choose the OnSelect property of button and define a collection which will be used as data source for the gallery. Name this collection as "collCheckBoxData". Now define another collection which will be used to hold the selected checkboxes values. Name it "collCheckBoxDataSelected". Also define two variables
    1. "varResetCheckBox" - This variable will be used to reset the check boxes checked status when we click on Load Data button.
    2. "varCheckedItems" - This variable will be used to hold the concatenated value of selected check boxes. (This is additional feature, we will discuss, where we will concatenate the values of selected check boxes  (delimiter based) so that we can save it in data source.
  4. The complete code is (you can map with color codes 😊)-
  5. ClearCollect(
        collCheckBoxData,
        {Title: "India"},
        {Title: "Russia"},
        {Title: "America"},
        {Title: "Japan"},
        {Title: "Brazil"},
        {Title: "China"},
        {Title: "South Korea"}
    );
    ClearCollect(
        collCheckBoxDataSelected,
        []
    );
    UpdateContext({varResetCheckBox: true});
    UpdateContext({varCheckedItems: ""});
    

  6. Now click on button using ALT key so that the function gets executed and the collection and variables could get initialized.
  7. Now edit the gallery we have added by clicking on edit icon as shown in below screenshot and add a checkbox.
  8. Set the template size to 60.
  9. Now we have to bind the data to this check box. For that, first we will bind collection with gallery.
  10. For this, click on gallery and set the Items property of gallery to "collCheckBoxData".
  11. Now again edit the template and click on Checkbox and set the Text property to ThisItem.Title.

  12. Now we have to apply some functions on OnCheck & OnUncheck property of checkbox.
  13. First, we will write function for OnCheck property. The objective is upon Checkbox checked event, we will check if this checkbox text is available in Selected Checkbox Data collection "collCheckBoxDataSelected". If not, then we will add it.
  14. Additionally, we will concatenate all selected text and will show in a label.
  15. Then we will set ResetCheckBox variable value to false. The complete code is-
  16. ForAll(
        Gallery2.AllItems,
        If(
            Checkbox2.Value = true,
            If(
                CountA(
                    Filter(
                        collCheckBoxDataSelected,
                        Title = Checkbox2.Text
                    )
                ) = 0,
                Collect(
                    collCheckBoxDataSelected,
                    {Title: Checkbox2.Text}
                );
            )
        )
    );
    UpdateContext({varCheckedItems: ""});
    UpdateContext(
        {
            varCheckedItems: Concat(
                collCheckBoxDataSelected.Title,
                Concatenate(
                    Title,
                    ";"
                )
            )
        }
    );
    UpdateContext({varResetCheckBox: false});
    
  17. Similarly, we will write function for OnUncheck property. The objective is upon Checkbox checked event, we will remove unchecked box text from the Selected Checkbox Data collection "collCheckBoxDataSelected". 
  18. Additionally, we will concatenate all selected text and will show in a label.
  19. Then we will set ResetCheckBox variable value to false. The complete code is-
  20. ForAll(
        Gallery2.AllItems,
        If(
            Checkbox2.Value = false,
            RemoveIf(
                collCheckBoxDataSelected,
                Title = Checkbox2.Text
            )
        )
    );
    UpdateContext({varCheckedItems: ""});
    UpdateContext(
        {
            varCheckedItems: Concat(
                collCheckBoxDataSelected.Title,
                Concatenate(
                    Title,
                    ";"
                )
            )
        }
    );
    UpdateContext({varResetCheckBox: false});
    
  21. Next we will update the Reset property of check box. Set this property with variable "varResetCheckBox".
  22. Major part has been done. Now we have to show the data of checkboxes, we have selected. For this we will add another vertical blank gallery.

  23. Set the data source as "collCheckBoxDataSelected"; Layout as "Title"; Fields as "Title". In case, if you don't find the fields to bind, just save and play the app and click on Load Data and then select any checkbox. It will load data in "collCheckBoxDataSelected". Now stop the app and assign the field.
  24. This way we have achieved the functionality.
  25. Now play the app

  26. Lastly, we have to display the concatenated value. For this, we will add a label and set it's Text property to the variable "varCheckedItems".

  27. The final screenshot 😊-
  28. Checkboxes selection-
  29. Checkboxes un-selection-
  30. Load Data (making all gallery and checkboxes in reset mode)-
  31. This way, you can implement such type of Checkbox functionality.
With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !

1 comment:

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