For  filter and sort functionality

Chapter - 08

Visit to buy the book on Citizen Development https://go.amitpuri.com/buynow

Code Snippet #7

For filter and sort functionality

This code snippet is designed to update a context variable (tabReferrals) with a filtered and sorted list of referral data from a data source (Referrals). It also toggles the sorting order between ascending and descending.

    UpdateContext({locSortOrder: !locSortOrder});
    UpdateContext(
        {
            tabReferrals: Filter(
                SortByColumns(
                    Search(
                        Referrals,
                        txtSearch.Value,
                        "nledu_name",
                        "nledu_referralemail",
                        "nledu_referralnotes"
                    ),
                    "nledu_referralstatus",
                    If(
                        locSortOrder,
                        SortOrder.Ascending,
                        SortOrder.Descending
                    )
                ),
                'Referrals (Views)'.'My Referrals'
            )
        }
    );

Explanation

  • Toggling Sorting Order:- UpdateContext({locSortOrder: !locSortOrder});: This line toggles the value of the context variable locSortOrder. If locSortOrder is currently true, it becomes false, and vice versa. This is used to switch between ascending and descending sorting orders.

  • Updating tabReferrals with Filtered and Sorted Data:- UpdateContext({ tabReferrals: Filter(SortByColumns(Search(...), "nledu_referralstatus", If(locSortOrder, SortOrder.Ascending, SortOrder.Descending)), 'Referrals (Views)'.'My Referrals') }): This complex line updates the tabReferrals context variable. It performs several operations on the Referrals data:
    • Search(Referrals, txtSearch.Value, "nledu_name", "nledu_referralemail", "nledu_referralnotes"): It searches the Referrals data source for records where the search query (txtSearch.Value) matches any of the specified fields (nledu_name, nledu_referralemail, nledu_referralnotes).
    • SortByColumns(..., "nledu_referralstatus", If(locSortOrder, SortOrder.Ascending, SortOrder.Descending)): The search results are then sorted by the column nledu_referralstatus. The sorting order is determined by locSortOrder – ascending if locSortOrder is true, and descending if false.
    • Filter(..., 'Referrals (Views)'.'My Referrals'): Finally, the sorted results are filtered based on a condition or view defined as 'Referrals (Views)'.'My Referrals'.
  • Usage of UpdateContext:- UpdateContext is used to create or update context variables within the current screen. Context variables are local to the screen they are set in and are used to store data temporarily.

In summary, this code snippet is used for dynamic data handling in a Power Apps application. It allows for searching within the Referrals data source, sorting the search results based on a toggleable order, and applying an additional filter. This kind of functionality is typically used in scenarios where users need to interact with and manipulate large sets of data, such as in data management interfaces or search features within an application.

Happy #low-code learning

Visit: www.amitpuri.com

Id: Chapter-08-CS00007

Category: Chapter 08

Amit Puri, Advisor and Consultant, Strengthening Digital Experiences, Modernize Cloud Journey with AI-Driven Transformation!

Code Snippet # 4
Chapter-06-CS00004 - Code Snippet # 4
Code Snippet # 5
Chapter-06-CS00005 - Code Snippet # 5
Code Snippet # 3
Chapter-08-CS00003 - Code Snippet # 3