Skip to content

Commit 7930ec0

Browse files
Merge pull request #3993 from syncfusion-content/945715-sort
documentation(945801):Sort foreign key column based on text
2 parents 11eba6e + e1d7ff5 commit 7930ec0

File tree

7 files changed

+168
-2
lines changed

7 files changed

+168
-2
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
public ActionResult Index()
2+
{
3+
return View();
4+
}
5+
6+
public ActionResult GetEmployeeRecords()
7+
{
8+
IEnumerable employeeData = EmployeeView.GetAllRecords();
9+
return Json(employeeData);
10+
}
11+
12+
public ActionResult GetOrderRecords(DataManagerRequest request)
13+
{
14+
IEnumerable orderData = OrdersDetails.GetAllRecords();
15+
DataOperations dataOperations = new DataOperations();
16+
if (request.Sorted != null && request.Sorted.Count > 0)
17+
{
18+
string sortColumn = request.Sorted[0].Name;
19+
string sortDirection = request.Sorted[0].Direction;
20+
if (sortColumn == "EmployeeID")
21+
{
22+
orderData = GetSortedOrdersByEmployee(sortDirection);
23+
}
24+
else
25+
{
26+
orderData = dataOperations.PerformSorting(orderData, request.Sorted);
27+
}
28+
}
29+
int totalRecords = orderData.Cast<OrdersDetails>().Count();
30+
if (request.Skip != 0)
31+
{
32+
orderData = dataOperations.PerformSkip(orderData, request.Skip);
33+
}
34+
if (request.Take != 0)
35+
{
36+
orderData = dataOperations.PerformTake(orderData, request.Take);
37+
}
38+
return request.RequiresCounts ? Json(new { result = orderData, count = totalRecords }) : Json(orderData);
39+
}
40+
41+
private List<OrdersDetails> GetSortedOrdersByEmployee(string sortDirection)
42+
{
43+
var employees = EmployeeView.GetAllRecords();
44+
List<EmployeeView> sortedEmployees = (sortDirection == "ascending")
45+
? employees.OrderBy(e => e.FirstName).ToList()
46+
: employees.OrderByDescending(e => e.FirstName).ToList();
47+
48+
List<OrdersDetails> sortedOrders = new List<OrdersDetails>();
49+
foreach (var employee in sortedEmployees)
50+
{
51+
var employeeOrders = OrdersDetails.GetAllRecords().Where(o => o.EmployeeID == employee.EmployeeID).ToList();
52+
sortedOrders.AddRange(employeeOrders);
53+
}
54+
return sortedOrders;
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
public IActionResult Index()
2+
{
3+
return View();
4+
}
5+
6+
public IActionResult GetEmployeeRecords()
7+
{
8+
IEnumerable employeeData = EmployeeView.GetAllRecords();
9+
return Json(employeeData);
10+
}
11+
12+
public IActionResult GetOrderRecords([FromBody] DataManagerRequest request)
13+
{
14+
IEnumerable orderData = OrdersDetails.GetAllRecords();
15+
DataOperations dataOperations = new DataOperations();
16+
if (request.Sorted != null && request.Sorted.Count > 0)
17+
{
18+
string sortColumn = request.Sorted[0].Name;
19+
string sortDirection = request.Sorted[0].Direction;
20+
if (sortColumn == "EmployeeID")
21+
{
22+
orderData = GetSortedOrdersByEmployee(sortDirection);
23+
}
24+
else
25+
{
26+
orderData = dataOperations.PerformSorting(orderData, request.Sorted);
27+
}
28+
}
29+
int totalRecords = orderData.Cast<OrdersDetails>().Count();
30+
if (request.Skip != 0)
31+
{
32+
orderData = dataOperations.PerformSkip(orderData, request.Skip);
33+
}
34+
if (request.Take != 0)
35+
{
36+
orderData = dataOperations.PerformTake(orderData, request.Take);
37+
}
38+
return request.RequiresCounts ? Json(new { result = orderData, count = totalRecords }) : Json(orderData);
39+
}
40+
41+
private List<OrdersDetails> GetSortedOrdersByEmployee(string sortDirection)
42+
{
43+
var employees = EmployeeView.GetAllRecords();
44+
List<EmployeeView> sortedEmployees = (sortDirection == "ascending")
45+
? employees.OrderBy(e => e.FirstName).ToList()
46+
: employees.OrderByDescending(e => e.FirstName).ToList();
47+
48+
List<OrdersDetails> sortedOrders = new List<OrdersDetails>();
49+
foreach (var employee in sortedEmployees)
50+
{
51+
var employeeOrders = OrdersDetails.GetAllRecords().Where(o => o.EmployeeID == employee.EmployeeID).ToList();
52+
sortedOrders.AddRange(employeeOrders);
53+
}
54+
return sortedOrders;
55+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@Html.EJS().Grid("grid").DataSource(ds => ds.Url(@Url.Action("GetOrderRecords", "Home")).Adaptor("UrlAdaptor")).Height("348px").Columns(col =>
2+
{
3+
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
4+
col.Field("EmployeeID").HeaderText("Employee Name").ForeignKeyField("EmployeeID").ForeignKeyValue("FirstName").DataSource(ds => ds.Url(@Url.Action("GetEmployeeRecords", "Home")).Adaptor("UrlAdaptor")).Width("140").Add();
5+
col.Field("CustomerID").HeaderText("Customer ID").Width("150").Add();
6+
col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
7+
col.Field("ShipName").HeaderText("Ship Name").Width("160").Add();
8+
}).AllowSorting(true).AllowPaging(true).Render()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@{
2+
var GetEmployeeRecords = new Syncfusion.EJ2.DataManager { Url = Url.Action("GetOrderRecords", "Home"), Adaptor = "UrlAdaptor"};
3+
}
4+
5+
<ejs-grid id="grid" allowSorting="true" allowPaging="true" >
6+
<e-data-manager url="/Home/GetEmployeeRecords" adaptor="UrlAdaptor"></e-data-manager>
7+
<e-grid-columns>
8+
<e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="125"></e-grid-column>
9+
<e-grid-column field="EmployeeID" headerText="Employee ID" foreignKeyField="EmployeeID" foreignKeyValue="FirstName" dataSource=GetEmployeeRecords ></e-grid-column>
10+
<e-grid-column field="CustomerID" headerText="CustomerID" width="125"></e-grid-column>
11+
<e-grid-column field="Freight" headerText="Freight" textAlign="Right" width="125"></e-grid-column>
12+
<e-grid-column field="ShipName" headerText="ShipName" width="125"></e-grid-column>
13+
</e-grid-columns>
14+
</ejs-grid>

ej2-asp-core-mvc/grid/EJ2_ASP.MVC/sorting.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
layout: post
3-
title: Sorting in ##Platform_Name## Grid Component
3+
title: Sorting in Syncfusion ##Platform_Name## Grid Component | Learn Sorting Features
44
description: Learn here all about Sorting in Syncfusion ##Platform_Name## Grid component of Syncfusion Essential JS 2 and more.
55
platform: ej2-asp-core-mvc
66
control: Sorting
@@ -156,6 +156,23 @@ The following example demonstrates how to perform sorting by enabling a foreign
156156

157157
![Sorting](images/sorting/sorting-local-forign.png)
158158

159+
**Sort foreign key column based on text for remote data**
160+
161+
In the case of remote data in the Syncfusion ASP.NET MVC Grid, the sorting operation will be performed based on the `ForeignKeyField` property of the column. The `ForeignKeyField` property should be defined in the column definition with the corresponding foreign key field name for each row. The Grid will send a request to the server-side with the `ForeignKeyField` name, and the server-side should handle the sorting operation and return the sorted data to the Grid.
162+
163+
The following example demonstrates sorting a foreign key column where the **EmployeeID** column is a foreign key, and the corresponding **FirstName** column is displayed from the employee data source:
164+
165+
{% tabs %}
166+
{% highlight razor tabtitle="CSHTML" %}
167+
{% include code-snippet/grid/sorting/foreign-sort-remote/razor %}
168+
{% endhighlight %}
169+
{% highlight c# tabtitle="foreign-sort" %}
170+
{% include code-snippet/grid/sorting/foreign-sort-remote/foreign-sort.cs %}
171+
{% endhighlight %}
172+
{% endtabs %}
173+
174+
![Sorting](images/sorting/sort-remote.gif)
175+
159176
## Perform sorting based on its culture
160177

161178
Perform sorting based on culture in the Grid can be achieved by utilizing the [Locale](https://help.syncfusion.com/cr/aspnetmvc-js2/Syncfusion.EJ2.Grids.Grid.html#Syncfusion_EJ2_Grids_Grid_Locale) property. By setting the `Locale` property to the desired culture code, you enable sorting based on that specific culture. This allows you to apply locale-specific sorting rules and ensure accurate ordering for different languages and regions.

ej2-asp-core-mvc/grid/EJ2_ASP.NETCORE/sorting.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
layout: post
3-
title: Sorting in ##Platform_Name## Grid Component
3+
title: Sorting in Syncfusion ##Platform_Name## Grid Component | Learn Sorting Features
44
description: Learn here all about Sorting in Syncfusion ##Platform_Name## Grid component of Syncfusion Essential JS 2 and more.
55
platform: ej2-asp-core-mvc
66
control: Sorting
@@ -156,6 +156,23 @@ The following example demonstrates how to perform sorting by enabling a foreign
156156

157157
![Sorting](images/sorting/sorting-local-forign.png)
158158

159+
**Sort foreign key column based on text for remote data**
160+
161+
In the case of remote data in the Syncfusion ASP.NET Core Grid, the sorting operation will be performed based on the `foreignKeyField` property of the column. The `foreignKeyField` property should be defined in the column definition with the corresponding foreign key field name for each row. The Grid will send a request to the server-side with the `foreignKeyField` name, and the server-side should handle the sorting operation and return the sorted data to the Grid.
162+
163+
The following example demonstrates sorting a foreign key column where the **EmployeeID** column is a foreign key, and the corresponding **FirstName** column is displayed from the employee data source:
164+
165+
{% tabs %}
166+
{% highlight cshtml tabtitle="CSHTML" %}
167+
{% include code-snippet/grid/sorting/foreign-sort-remote/tagHelper %}
168+
{% endhighlight %}
169+
{% highlight c# tabtitle="foreign-sort" %}
170+
{% include code-snippet/grid/sorting/foreign-sort-remote/foreign-sortcore.cs %}
171+
{% endhighlight %}
172+
{% endtabs %}
173+
174+
![Sorting](images/sorting/sort-remote.gif)
175+
159176
## Perform sorting based on its culture
160177

161178
Perform sorting based on culture in the Grid can be achieved by utilizing the [locale](https://help.syncfusion.com/cr/aspnetcore-js2/syncfusion.ej2.grids.grid.html#Syncfusion_EJ2_Grids_Grid_Locale) property. By setting the `locale` property to the desired culture code, you enable sorting based on that specific culture. This allows you to apply locale-specific sorting rules and ensure accurate ordering for different languages and regions.
Loading

0 commit comments

Comments
 (0)