For  toggling sort on selected column (or sort field)

Chapter - 06

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

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), ...): This If statement checks if the ColumnName property of the selected item in the dropdown control drpColumns 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 variable locSortOrder. If locSortOrder is currently true, it becomes false, 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 the locReferralList collection by the column selected in the dropdown (drpColumns.Selected.ColumnName). The sort order is determined by the locSortOrder variable: ascending if locSortOrder is true, descending if false.
    • ClearCollect(...): This function first clears the locReferralList 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

Visit: www.amitpuri.com

Id: Chapter-06-CS00006

Category: Chapter 06

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

Code Snippet # 9
Chapter-08-CS00009 - Code Snippet # 9
Code Snippet # 4
Chapter-06-CS00004 - Code Snippet # 4
Code Snippet # 2
Chapter-06-CS00002 - Code Snippet # 2