BindTo(...) and aggregates

2 posts, 0 answers
  1. Heiko
    Heiko avatar
    61 posts
    Member since:
    Nov 2018

    Posted 30 Apr 2020 Link to this post

    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

  2. Anton Mironov
    Admin
    Anton Mironov avatar
    168 posts

    Posted 04 May 2020 Link to this post

    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.
Back to Top