Search results

Sorting in ASP.NET MVC Pivot Table control

Member Sorting

Allows to order field members in rows and columns either in ascending or descending order. By default, field members in rows and columns are in ascending order.

Member sorting can be enabled by setting the EnableSorting property in PivotViewDataSourceSettings class to true. After enabling this API, click the sort icon besides each field in row or column axis, available in field list or grouping bar UI for re-arranging members either in ascending or descending order.

By default the EnableSorting property in PivotViewDataSourceSettings class set as true. If we set it as false, then the field members arrange in pivot table as its data source order. And, the sort icons in grouping bar and field list buttons will be removed.

output
output
output

Member sorting can also be configured using the PivotViewSortSettings class through code behind, during initial rendering. The settings required to sort are:

  • Name: It allows to set the field name.
  • Order: It allows to set the sort direction either to ascending or descending of the respective field.

By default the Order property in the PivotViewSortSettings class set as Sorting.Ascending. Meanwhile, we can arrange the field members as its order in data source by setting it as Sorting.None where the sort icons in grouping bar and field list buttons for the corresponding field will be removed.

razor
Sorting.cs
@using Syncfusion.EJ2.PivotView

@Html.EJS().PivotView("PivotView").Height(300).DataSourceSettings(dataSource => dataSource.DataSource((IEnumerable<object>)ViewBag.DataSource).ExpandAll(false).EnableSorting(true)
.SortSettings(sortsettings =>
{
    sortsettings.Name("Country").Order(Sorting.Descending).Add();
}).Rows(rows =>
{
    rows.Name("Country").Add(); rows.Name("Products").Add();
}).Columns(columns =>
{
    columns.Name("Year").Caption("Year").Add(); columns.Name("Quarter").Add();
}).Values(values =>
{
    values.Name("Sold").Caption("Units Sold").Add(); values.Name("Amount").Caption("Sold Amount").Add();
})).Render()
public ActionResult Index()
{
    var data = GetPivotData();
    ViewBag.DataSource = data;
    return View();
}

Value sorting

This property is applicable only for relational data source.

Allows to sort individual value field and its aggregated values either in row or column axis in both ascending and descending order. It can been enabled by setting the EnableValueSorting property in PivotView class to true. On enabling, end user can sort the values by directly clicking the value field header positioned either in row or column axis of the pivot table component.

The value sorting can also be configured using the PivotViewValueSortSettings option through code behind. The settings required to sort value fields are:

  • HeaderText: It allows to set the header names with delimiters, that is used for value sorting. The header names are arranged from Level 1 to Level N, down the hierarchy with a delimiter for better specification.
  • HeaderDelimiter: It allows to set the delimiters string to separate the header text between levels.
  • SortOrder: It allows to set the sort direction of the value field.

Value fields are set to the column axis by default. In such cases, the value sorting applied will have an effect on the column alone. You need to place the value fields in the row axis to do so in row wise. For more information, please refer here.

razor
ValueSorting.cs
@using Syncfusion.EJ2.PivotView

@Html.EJS().PivotView("PivotView").Height(300).DataSourceSettings(dataSource => dataSource.DataSource((IEnumerable<object>)ViewBag.DataSource).ExpandAll(false)
.ValueSortSettings(new PivotViewValueSortSettings {
    HeaderText = "FY 2015##Sold Amount",
    HeaderDelimiter = "##",
    SortOrder = Sorting.Descending })
.Rows(rows =>
{
    rows.Name("Country").Add(); rows.Name("Products").Add();
}).Columns(columns =>
{
    columns.Name("Year").Caption("Year").Add(); columns.Name("Quarter").Add();
}).Values(values =>
{
    values.Name("Sold").Caption("Units Sold").Add(); values.Name("Amount").Caption("Sold Amount").Add();
})).EnableValueSorting(true).Render()
public ActionResult Index()
{
    var data = GetPivotData();
    ViewBag.DataSource = data;
    return View();
}

output