Code Snippet #6
For toggling sort on selected column (or sort field)
This code snippet performs a conditional operation that involves sorting a collection based on a selected column from a dropdown control and toggling the sort order.
If(
!IsBlank(drpColumns.Selected.ColumnName),
UpdateContext({locSortOrder: !locSortOrder});
ClearCollect(
locReferralList,
SortByColumns(
locReferralList,
drpColumns.Selected.ColumnName,
If(
locSortOrder,
Ascending,
Descending
)
)
);
)
Explanation
-
Conditional Check Using
IsBlank
:-If(!IsBlank(drpColumns.Selected.ColumnName), ...)
: ThisIf
statement checks if theColumnName
property of the selected item in the dropdown controldrpColumns
is not blank. It ensures that a column has been selected for sorting. -
Toggling Sorting Order:-
UpdateContext({locSortOrder: !locSortOrder});
: If a column is selected, this line toggles the value of the context variablelocSortOrder
. IflocSortOrder
is currentlytrue
, it becomesfalse
, and vice versa. This toggle changes the sort order between ascending and descending. -
Sorting the Collection:-
ClearCollect(locReferralList, SortByColumns(locReferralList, drpColumns.Selected.ColumnName, If(locSortOrder, Ascending, Descending)))
:SortByColumns(locReferralList, drpColumns.Selected.ColumnName, If(locSortOrder, Ascending, Descending))
: This function sorts thelocReferralList
collection by the column selected in the dropdown (drpColumns.Selected.ColumnName
). The sort order is determined by thelocSortOrder
variable: ascending iflocSortOrder
istrue
, descending iffalse
.ClearCollect(...)
: This function first clears thelocReferralList
collection and then repopulates it with the sorted data. This ensures that the collection always reflects the current sort order and the selected column for sorting.
In summary, this code snippet is used for dynamically sorting a collection (locReferralList
) based on a user’s selection from a dropdown control (drpColumns
). It provides functionality to toggle the sorting order each time the sorting operation is triggered, allowing users to easily switch between ascending and descending sort orders. This kind of dynamic data manipulation is useful in scenarios where users need to interact with and organize large sets of data.
Happy #low-code learning
Amit Puri, Advisor and Consultant, Strengthening Digital Experiences, Modernize Cloud Journey with AI-Driven Transformation!