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 variablelocSortOrder
. IflocSortOrder
is currentlytrue
, it becomesfalse
, 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 thetabReferrals
context variable. It performs several operations on theReferrals
data:Search(Referrals, txtSearch.Value, "nledu_name", "nledu_referralemail", "nledu_referralnotes")
: It searches theReferrals
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 columnnledu_referralstatus
. The sorting order is determined bylocSortOrder
– ascending iflocSortOrder
istrue
, and descending iffalse
.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
Dr. Amit Puri, Advisor and Consultant, Strengthening Digital Experiences, Modernize Cloud Journey with AI-Driven Transformation!