Apply a sort to a dataset in a PowerApps component (PCF)

1.3k Views Asked by At

I’m trying to create a new dataset type Powerapps Component (PCF). For the moment I am using it to display a view of the records that are available in an entity in Microsoft Dynamics CRM.

I wish to make the view sort itself when I click on the grid column headers (in a similar way that the default CRM grid view does). I'm trying to figure out how to apply a sort to the dataset so that I can refresh it as indicated by the documentation for the dataset.refresh() function:

Refreshes the dataset based on filters, sorting, linking, new column. New data will be pushed to control in another 'updateView' cycle.

The dataset object does have a “sorting” property, but changing its value and then refreshing the dataset doesn’t seem to have any effect. After the refresh, the sorting property reverts to the value it had before I changed it.

In short, the click handler for the grid header does something like the following bit of code. The refresh gets done and my updateView() function gets called as expected but the sorting was not applied.

dataset.sorting = [{name: 'createdon', sortDirection: 1}];
dataset.refresh();

Any help on getting the dataset sorting to work would be appreciated.

2

There are 2 best solutions below

0
On BEST ANSWER

I've been experimenting with PowerApps Component Framework a little bit recently and I can confirm that the following code won't be working:

dataSet.sorting = [ { name: "columnName", sortDirection: 0 } ];

However, I managed to get this one working for me:

dataSet.sorting.pop(); // you may want to clean up the whole collection
dataSet.sorting.push({ name: "columnName", sortDirection: 0 });

I haven't really figured out the reason of this behavior. The sorting array may be implemented as some form of observable collection in the background.

I hope this will guide you to a functioning solution.

3
On

The documentation is pretty abysmal here, but here is my best guess from putting a few different pieces of information together.

TLDR: I think there is some kind of extra method that needs to be called on the .sorting property, but I can't find out what it is called. Maybe something like:

dataset.sorting.setSorting({name: 'createdon', sortDirection: 1});

I think you're going to have to try a bunch of likely method names and see what works.

Background and links:

The only reference I could find to dataset.sorting was from here:

In this preview for canvas apps, only a limited set of filtering and sortStatus methods are supported. Filter and sort can be applied to dataset on primary type columns except for the GUID. Filter and sorting can be applied in the same way as in model-driven apps.To retrieve the dataset with filtering and sorting information, call the methods in context.parameters.[dataset_property_name].filtering and context.parameters.[dataset_property_name].sorting, then invoke the context.parameters.[dataset_property_name].refresh().

So it seems that the .filtering and .sorting properties are handled similarly, and that there are some methods attached to them, and only some are supported. That is about as vague as they could make it...

I did find an example of how .filtering is used:

    _context.parameters.sampleDataset.filtering.setFilter({
      conditions: conditionArray,
      filterOperator: 1, // Or
    });

There is a brief reference to .setFilter() in the docs, as well as FilterExpression

There is a SortStatus reference, but it doesn't have any corresponding methods explicitly called out. It is possible that this is not yet a supported feature in the public preview, or the documentation is lacking and the name and syntax of the method you need to call on .sorting is not yet documented.