Unplanned
Last Updated: 21 Apr 2023 19:54 by Frank
cvuas
Created on: 31 Jan 2022 13:30
Category: Grid
Type: Feature Request
19
Allow changing the Sorting Direction order via an attribute
When you have Sortable set to true and you click on the header, the sorting direction goes from ascending to descending to neutral. I would like to configure my Grid so that when clicking on the header the sorting direction will start from descending, go to neutral, and after that to ascending or any other order. 
1 comment
Frank
Posted on: 21 Apr 2023 19:54

I might have a work around for this using the example provided in the documentation as a template. Hopefully this will help someone. My logic is that I count each sort click with a max of 3 options; ascending, descending, and none. On first sort, I manually change the sort from ascending to descending. On the second click, I set it to ascending. And on the third click I clear the sort. If at any time sorting is initiated on a new column, the count is reset. There is also a check against a list so that only field names in that list will get adjusted to a reverse sort order.

First to you code behind partial class (or in the code section on the razor page) add the following variables:

private string reverseSortColFieldName = null;
private int reverseSortColCount = 0;
private List<string> reverseSortColFieldNames = new List<string>()
{
    new string("col1 field name"),
    new string("col2 field name"),
    new string("col3 field name"),
};

In the list, use the field name for that column. So for example, if the first column I wanted to have reverse order had a name of Valuation, I would add new string("Valuation") to the list. You can have as many columns as needed which need a reverse sort order in this list.

Next you can copy the following logic from the documentation.

// Note: This can cause a performance delay if you do long operations here
// Note 2: The grid does not await this event, its purpose is to notify you of changes
// so you must not perform async operations and data loading here, or issues with the grid state may occur
// or other things you change on the page won't actually change. The .SetStateAsync() call redraws only the grid, but not the rest of the page
        private async Task OnStateChangedHandler(GridStateEventArgs<YourClassNameHere> args)
        {
            if (args.PropertyName == "SortDescriptors") // sorting changed
            {
                // ensure certain state based on some condition
                // in this example - ensure that the ID field is always filtered with a certain setting unless the user filters it explicitly
                var sortDescriptors = args.GridState.SortDescriptors;
                reverseSortColCount++;
                if (sortDescriptors.Count > 0)
                {
                    var descriptor = sortDescriptors.First();
                    if (reverseSortColFieldName != descriptor.Member)
                    {

                        // This means that a new column is being sorted, so reset the count

                        reverseSortColCount = 1;
                    }
                    reverseSortColFieldName = descriptor.Member;

                    if (reverseSortColFieldNames.Contains(descriptor.Member))
                    {
                        if (descriptor.SortDirection == Telerik.DataSource.ListSortDirection.Ascending)
                        {
                            descriptor.SortDirection = Telerik.DataSource.ListSortDirection.Descending;
                        }
                        else if (reverseSortColCount == 3)
                        {
                            // clear the sort and reset count back to zero and clear field name
                            sortDescriptors.Clear();
                            reverseSortColCount = 0;
                            reverseSortColFieldName = null;
                        }
                    }
                    else
                    {
                        // Clear out reverse sorting data
                        reverseSortColFieldName = null;
                        reverseSortColCount = 0;
                    }
                }
                else
                {
                    if (reverseSortColCount == 2)
                    {
                        // If it's the second click, there won't be any sort descriptors because
                        // the grid goes from decending to off. So we need to make sort ascending
                        Telerik.DataSource.SortDescriptor newDescriptor = new Telerik.DataSource.SortDescriptor();
                        newDescriptor.SortDirection = Telerik.DataSource.ListSortDirection.Ascending;
                        newDescriptor.Member = reverseSortColFieldName;
                        sortDescriptors.Add(newDescriptor);
                    }
                }

                // needed only if you will be overriding user actions or amending them
                // if you only need to be notified of changes, you should not call this method
                await GridRef.SetStateAsync(args.GridState);
            }
        }

 

And finally on your razor page on the TelerikGrid element add the following attribute in order to call the code above.
OnStateChanged="@((GridStateEventArgs<YourClassName> args) => OnStateChangedHandler(args))"