Default sort order in PivotGrid

1 Answer 137 Views
PivotGrid
Taras
Top achievements
Rank 3
Iron
Iron
Veteran
Taras asked on 24 Nov 2022, 10:44 PM

How do you specify the default sort order of rows/columns in a PivotGrid?

My controller has this method

        public IActionResult PivotGrid()
        {
            IEnumerable<ResponseReportPivot> objResponseList = _db.ResponseReportPivot;
            var sortedResponseList = objResponseList.OrderBy(s => s.EventYear).ThenBy(s => s.Venue);
            return View(sortedResponseList);
        }

When I run the pivot I get the display in Pivot Display.jpg attached.

If I change the OrderBy clauses, I get the display in Pivot display 2.jpg.

I know the user can change the sort but I want the column display to default to be Event Year ascending and the row display to default to Venue ascending.

I haven't seen anywhere how that can be accomplished.

Here's the pivot grid code


@*For local binding*@

@using Application.Models;
@using Kendo.Mvc.Extensions;
@using Kendo.Mvc.UI;

@model IEnumerable<ResponseReportPivot>

@{
    ViewBag.Title = "Response Report Pivot Grid";
}
@Html.AntiForgeryToken()

<style>
    .k-pivot-table .k-grid-content td {
        text-align: left;
    }
</style>

<div class="k-pivotgrid-wrapper">
    @(Html.Kendo().PivotConfigurator()
        .Name("configurator")
        .HtmlAttributes(new { @class = "hidden-on-narrow" })
        .Filterable(true)
        .Sortable(true)
        .Height(570)
    )

    @(Html.Kendo().PivotGrid<ResponseReportPivot>()
        .Name("pivotgrid")
        .Configurator("#configurator")
        .ColumnWidth(120)
        .Filterable(true)
        .Height(570)
        .Sortable(true)
        .BindTo(Model)
        .DataSource(dataSource => dataSource
        .Ajax()
        .Schema(schema => schema
        .Cube(cube => cube
        .Dimensions(dimensions =>
        {
            dimensions.Add(model => model.Venue).Caption("All Venues");
            dimensions.Add(model => model.EventYear).Caption("All Years");
        })
        .Measures(measures =>
        {
            measures.Add("Count").Field(model => model.Venue).AggregateName("count");
        })
        ))
        .Columns(columns =>
        {
            columns.Add("EventYear").Expand(true);
        })
        .Rows(rows => rows.Add("Venue").Expand(true))
        .Measures(measures => measures.Values("Count"))
        .Events(e => e.Error("onError"))
        )
    )
</div>
<div class="responsive-message"></div>

<script>
    function onError(e) {
        alert("error: " + kendo.stringify(e.errors[0]));
    }
</script>

 

 

1 Answer, 1 is accepted

Sort by
0
Accepted
Alexander
Telerik team
answered on 29 Nov 2022, 04:14 PM

Hi Taras,

Thank you for reaching out and for taking the time to share the relevant PivotGrid configuration on your end, it is greatly appreciated.

Generally, the PivotGridDataSource exposes a built-in .Sort() API configuration that can be utilized for explicitly adding default sorting for any given arbitrary fields. Here is an example:

.DataSource(dataSource => dataSource
    .Ajax()
    .Transport(transport => transport.Read("Customers_Read", "PivotGrid"))
    .Sort(s => 
    {
        s.Add("Country").Ascending();         
        s.Add("ContactTitle").Descending();
    })
    ...
    .Columns(columns =>
    {
        columns.Add("Country").Expand(true);
    })
    .Rows(rows => rows.Add("ContactTitle").Expand(true))
)

For your convenience, here is a Telerik REPL example that illustrates the mentioned above. Notice that the default sorting is applied for both the "Country" column and the "ContactTitle" row.

I hope this helps.

Kind Regards,
Alexander
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Taras
Top achievements
Rank 3
Iron
Iron
Veteran
commented on 30 Nov 2022, 05:22 PM

That worked.  Thanks
Tags
PivotGrid
Asked by
Taras
Top achievements
Rank 3
Iron
Iron
Veteran
Answers by
Alexander
Telerik team
Share this question
or