Declined
Last Updated: 04 Sep 2020 10:54 by ADMIN
Gudmar
Created on: 27 Aug 2019 14:10
Category: UI for Blazor
Type: Feature Request
4
Adding SortField to Grid

For the Grid, is is possible to have a a SortField along with Field (that displays the data) in columns.

The SortField will be used instead of Field when sorting (if Grid is Sortable), like in WebForms: https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview.sortexpression?view=netframework-4.8.

Maybe you want to sort data based on other values instead of what is displayed.

This could be especially useful for dates that have been formatted for display in Field

but you don't want to use for sorting because date and string sorting is not the same.

7 comments
ADMIN
Marin Bratanov
Posted on: 04 Sep 2020 10:54

Hello John,

If you want a field to be used for data operations (filtering, sorting, grouping), you should set that the the Field parameter of the column. If you want the column to show the data for a different field, you can use its Template to choose what to render in it.

If we start adding parameters that control the operations in a different manner, configuring the grid will become complicated and counter intuitive.

Then, there are also other ways to influence the grid - you can capture its StateChanged event and alter its State (example here).

---

On the particular situation - defining sort order is something you can do through the grid state as well - see here on setting programmatic sorting. When the Field of the column is set correctly, it will work directly - the grid can sort strings, numbers, dates, enums. When you say "custom status" I am assuming it is a string, and so the grid can handle that.

If that custom status is an entire class, however, you must implement all data operations through the OnRead event because the grid cannot do this - there are no defined comparisons for entire classes in the framework, and so the grid can do this for you only when value types are used (read more here). An alternative is to use the nested field (such as ID or text) for the grid column, see how to do that here.

If this does not help you move forward, I advise that you open a support ticket where you can attach files and show us a mockup of the setup and problem you have so we can provide a more accurate answer.

Regards,
Marin Bratanov
Progress Telerik

Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive , special prizes and more, for FREE?! Register now for DevReach 2.0(20).

John Campion
Posted on: 03 Sep 2020 13:49

Not sure I see why that is a problem.  Not adding feature A because it might lead to requests for other features B and C seems to me like an odd way to consider requests.  

Let me put out there my use case to see if it resonates.

I have Task objects that are the base of the list of grid items.

The user can set up custom statuses for these tasks and define the sort order for these statuses (ie alphabetical is never used in this case).

The status class consists of an ID (database ID), the actual status, "In Progress", and the sort order, 10.

The status is stored with the task as the database ID.   So we can do the drop down, using the text and ID correctly and that piece works fine in the grid.  But when users sort the grid by status, there is no way to respect the sort order.

Any suggestions?

ADMIN
Marin Bratanov
Posted on: 03 Sep 2020 08:07

Hi John,

If we start going that way, we would have to add GroupField, FilterField, then I wouldn't wonder if an EditField starts getting requests and all of that will turn into a "property hell" situation, will become a rather convoluted code that's hard to maintain (i.e., it is more likely to be buggy), and it will be rather difficult and confusing to use.

 

Regards,
Marin Bratanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

John Campion
Posted on: 03 Sep 2020 01:49

Wondering if you would reconsider this?  Would be very helpful to have a separate sort field for some columns.

 

ADMIN
Marin Bratanov
Posted on: 29 Aug 2019 10:58

Hi Gudmar,

Thank you for the clarification. I am going to keep this request open (I just added your latest explanation to the top post for visibility). Considering that templates in Blazor are more powerful than in WebForms, I am not sure if such a feature will make sense for Blazor yet, but let's keep an eye on the public demand and comments.

In the meantime, you can easily do the same with templates (EDIT: a video of the expected behavior is attached below):

 

@using Telerik.Blazor.Components.Grid

<TelerikGrid Data="@MyData" Height="300px"
             Pageable="true" Sortable="true"
             FilterMode="Telerik.Blazor.FilterMode.FilterRow">
    <TelerikGridColumns>
        <TelerikGridColumn Field="@(nameof(SampleData.Id))" />
        <TelerikGridColumn Field="@(nameof(SampleData.Name))" Title="Employee Name" />
        <TelerikGridColumn Field="@(nameof(SampleData.Name))" Title="Hire Date, sorts by name">
            <Template>
                @( (context as SampleData).HireDate.ToShortDateString() )
            </Template>
        </TelerikGridColumn>
    </TelerikGridColumns>
</TelerikGrid>

@code {
    public IEnumerable<SampleData> MyData = Enumerable.Range(1, 50).Select(x => new SampleData
    {
        Id = x,
        Name = "name " + x,
        HireDate = DateTime.Now.AddDays(-x)
    });

    public class SampleData
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime HireDate { get; set; }
    }

    //in a real case, consider fetching the data in an appropriate event like OnInitializedAsync
    //also, consider keeping the models in dedicated locations like a shared library
    //this is just an example that is easy to copy and run
}

 

Of course, if your grid is editable, you should also use the editor template to edit the appropriate field instead of having two fields for the Name.

Regards,
Marin Bratanov
Progress Telerik

 UI for Blazor
Attached Files:
Gudmar
Posted on: 28 Aug 2019 19:14

Hi Marin,

I was thinking that the SortField would not be displayed in the grid, it would only be used as a column for sorting the column instead of Field if sortable is true.

If you have worked with asp.net web forms gridview then this is a feature there that has helped us much especially with sorting dates and alpha numeric values. We show a different value in the gridview Field than we use for sorting in SortField in some cases.

https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview.sortexpression?view=netframework-4.8

Gudmar 

ADMIN
Marin Bratanov
Posted on: 28 Aug 2019 16:28

Hello Gudmar,

Does using a column template to show that second (display) field work for you? If the column's Field is set to the field you want it to sort by, the template should not affect that. Could you give that a try and let me know how that works out and whether you encounter problems?

 

Regards,
Marin Bratanov
Progress Telerik

 UI for Blazor