Enabling ForeignKey Column Sorting by Text
Environment
| Product | Progress Telerik UI for ASP.NET Core Grid |
| Progress Telerik UI for ASP.NET Core version | Created with the 2023.1.314 version |
Description
How can I enable ForeignKey column sorting by text in the Telerik UI for ASP.NET Core Grid?
Solution
The following example demonstrates how to enable the sort-by-text functionality in a ForeignKey column by adding the text field in the Grid.
To achieve the desired scenario:
- Add the complex model field that is corresponding for the ForeignKey column.
- Subscribe to the
document.ready()event after the Grid declaration, get a reference of the header for the ForeignKey column, and change thedata-kendo-fieldattribute to point to the text field.
If the Grid uses deferred initialization, place the handler after the deferred scripts are rendered. This example uses ServerOperation(false), so the Grid sorts the returned data on the client by the text field.
public class ProductViewModel
{
public int ProductID
{
get;
set;
}
public string ProductName
{
get;
set;
}
public decimal UnitPrice
{
get;
set;
}
public CategoryViewModel Category
{
get;
set;
}
public int? CategoryID { get; set; }
}Place the script in the same .cshtml view after the Grid markup, or in an @section Scripts that the layout renders after the view content. The Grid must be initialized before the handler runs.
For the complete implementation of the suggested approach, refer to the Telerik REPL example on enabling foreign key column sorting by text in the Grid.