See SAIL Recipes for information about how to work through recipes and adapt them to your application.
Display and sort on an additional column for total price that is not a field in the CDT, but calculated based on the unit price and quantity fields.
To do this, we'll store the calculated data in a dictionary that contains the relevant CDT fields as well as the additional calculate field.
The main expression uses a supporting rule, so let's create it first.
ucGridData: Calculates additional data to be displayed in the grid
Create expression rule ucGridData with the following rule inputs:
data (Any Type)
Enter the following definition for the rule:
Now that we've created the supporting rule, let's move on to the main expression.
Test it out
Sort the grid by the "Total Price" column. Notice that the data sorts appropriately even across all pages of data.
This expression shows how to modify the above expression for offline use. The only difference is that all rows are displayed initially since grid paging is not available when offline.
Notable implementation details
In order to correctly sort on the calculated column, we queried the entire data set, calculated the new value for each row, then paged and sorted on the result. If you were to only calculate the column for the current page, sorting on the calculated column would not work correctly across pages.
Since you must query all data and then loop over each item to calculate the additional data, this technique should not be used for a large amount of data, as the query and calculation may become slow. To further optimize this grid, only perform the calculation on every row when the user sorts by the calculated column, otherwise, simply calculate the data for the current page.