Completed
Last Updated: 04 Feb 2021 03:01 by ADMIN
Release 2.22.0
chandra
Created on: 30 Dec 2020 14:25
Category: Grid
Type: Bug Report
3
ToODataString() does not apply multiple orderby expressions when using multi column sorting

The orderby clause only has one column when multicolumn sorting is enabled, you can test it with this sample.

---

ADMIN EDIT

A workaround is to replace the orderby generated by the grid with your own clause that uses the DataSourceRequest to extract all sort descriptors, here's an example:

@using System.Net.Http.Json
@using System.Text.RegularExpressions
@using Telerik.Blazor.Extensions
@using WasmApp.Shared
@inject HttpClient Http

<TelerikGrid Data=@GridData
             Height="460px"
             RowHeight="60"
             PageSize="10"
             Pageable="true"
             Sortable="true"
             FilterMode="@GridFilterMode.FilterRow"
             SortMode="@SortMode.Multiple"
             OnRead=@ReadItems
             TotalCount=@Total>
    <GridColumns>
        <GridColumn Field="ProductID" />
        <GridColumn Field="ProductName" />
        <GridColumn Field="Discontinued" />
    </GridColumns>
</TelerikGrid>

@code{
    public List<ODataProduct> GridData { get; set; } = new List<ODataProduct>();
    public int Total { get; set; } = 0;

    protected async Task ReadItems(GridReadEventArgs args)
    {
        var baseUrl = "https://demos.telerik.com/kendo-ui/service-v4/odata/Products?";

        string OdataUrl = args.Request.ToODataString();

        // replace the original orederby clause with one that contains all the order rules
        Regex x = new Regex("(orderby=)(.*?)(&)", RegexOptions.IgnoreCase);
        string actualOrderByClause = "orderby=";

        for (int i = 0; i < args.Request.Sorts.Count; i++)
        {
            if (i > 0)
            {
                actualOrderByClause += ",";
            }
            string order = args.Request.Sorts[i].SortDirection == Telerik.DataSource.ListSortDirection.Ascending ? "" : "%20desc";
            actualOrderByClause += $"{args.Request.Sorts[i].Member}{order}";
        }

        actualOrderByClause += "&";

        string OdataQueryWithMultipleOrder = x.Replace(OdataUrl, actualOrderByClause);

        //do the request as usual
        var requestUrl = $"{baseUrl}{OdataQueryWithMultipleOrder}";

        ODataResponseOrders response = await Http.GetFromJsonAsync<ODataResponseOrders>(requestUrl);

        GridData = response.Products;
        Total = response.Total;
    }
}

---

0 comments