This is a migrated thread and some comments may be shown as answers.

BindTo(...) and aggregates

1 Answer 1122 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Heiko
Top achievements
Rank 1
Iron
Veteran
Heiko asked on 30 Apr 2020, 09:20 AM

I am using Grid and via BindTo(...) it displays a list of objects from my PageModel; this works fine.

@(Html.Kendo().Grid<ProjectImportFormat>()
    .Name("grdProjectImports")
    .BindTo(Model.ProjectResults)
    .AutoBind(true)

...)

 

What I like to do is display the count of all records in the footer of the grid. Normally aggregates are declared inside the datasource, but there is only a list, no datasource. How can this be accomplished?

Regards
Heiko

1 Answer, 1 is accepted

Sort by
1
Anton Mironov
Telerik team
answered on 04 May 2020, 03:11 PM

Hello Heiko,

Thank you for the provided code snippet.

Below are presented a couple of approaches that could be undertaken in the current scenario. The recommended approach is to make use of the ClientFooterTemplate() option of the grid's columns.

1. The most straightforward one is to use the count of items in the model of the page. Choose the column in which footer will be positioned:

columns.Bound(p => p.FieldName).ClientFooterTemplate(Model.ProjectResults.Count().ToString());

2. The following approach demonstrates how to aggregate the DataSource while keeping the local data-binding:

.BindTo(data)
       .DataSource(dataSource => dataSource
       .Ajax()
       .ServerOperation(false)
       .Aggregates(ag => ag.Add(f => f.FieldName).Count())
       .PageSize(10)
    )

It is important to add the ServerOperation(false) functionality (added in the snippet above).

After that, reference the aggregate in the ClientFooterTemplate:

columns.Bound(p => p.FieldName).ClientFooterTemplate("#=data.Name.Count#");


3. The ClientFooterTemplate could use a JavaScript function to return the count based on the number of items in the data source of the grid. Here is an example:

columns.Bound(p => p.FieldName).ClientFooterTemplate("#= showTotal()#");

Below is the function of the data count:

function showTotal() {
            var grid = $("#Grid").data("kendoGrid");
            var dataSource = grid.dataSource;
            //total records
            var totalRecords = dataSource.total();

            return totalRecords;
        }

If you need any further assistance feel free to contact me back.

 

Regards,
Anton Mironov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
Grid
Asked by
Heiko
Top achievements
Rank 1
Iron
Veteran
Answers by
Anton Mironov
Telerik team
Share this question
or