Asp net Core Grid - aggregates not work

1 Answer 404 Views
Grid
Miroslav
Top achievements
Rank 1
Miroslav asked on 19 Feb 2022, 02:05 PM

Hello I want to use aggregate function in my grid but it not work. I read examples here but no success.

Can someone tell me why?

Here  is my code:

 @(Html.Kendo().Grid(Model.RecipeLinesdetail)
                .Name("#grid")
                .Editable(editable => editable.Mode(GridEditMode.InCell))
                .Columns(c=>{
                    c.Bound(c=>c.RecipeLineId).Hidden(true);
                    c.Bound(c=>c.ChemistryRecipeViewModel.ChemistryName)
                        .EditorTemplateName("ChemistryEditor")
                        .Width(500)
                        .ClientTemplate("#= ChemistryRecipeViewModel.ChemistryName # <input type='hidden' name='RecipeLinesdetail[#=index(data)#]"+
                            ".ChemistryRecipeViewModel.ChemistryId' value='#= ChemistryRecipeViewModel.ChemistryId #' />"+
                                                        "<input type='hidden' name='RecipeLinesdetail[#=index(data)#]"+
                            ".ChemistryRecipeViewModel.ChemistryName' value='#= ChemistryRecipeViewModel.ChemistryName #' />");
                    c.Bound(c=>c.Dosage)
                          .EditorTemplateName("DosageEditor")                          
                          .ClientTemplate("#= kendo.toString(Dosage,\"n3\")# <input type='hidden' name='RecipeLinesdetail[#=index(data)#].Dosage' value='#= Dosage #' />")
                          .ClientFooterTemplate("Dávkování celkem: #=sum#");
                    c.Bound(c=>c.Units.UnitName)
                        .EditorTemplateName("UnitsEditor")
                        .Title("Jednotka výpočtu")
                        .ClientTemplate("#= Units.UnitName # <input type='hidden' name='RecipeLinesdetail[#=index(data)#]"+
                            ".Units.UnitId' value='#= Units.UnitId #' />"+
                            "<input type='hidden' name='RecipeLinesdetail[#=index(data)#]"+
                            ".Units.UnitName' value='#= Units.UnitName #' />");
                    c.Bound(c=>c.Rounded)
                        .EditorTemplateName("DosageEditor")
                        .ClientTemplate("#= kendo.toString(Rounded,\"n3\") # <input type='hidden' name='RecipeLinesdetail[#=index(data)#].Rounded' value='#= Rounded #' />");
                    c.Bound(c=>c.Units2.UnitName)
                        .EditorTemplateName("Units2Editor")
                        .Title("Jednotka vážení")
                        .ClientTemplate("#= Units2.UnitName # <input type='hidden' name='RecipeLinesdetail[#=index(data)#]"+
                            ".Units2.UnitId' value='#= Units2.UnitId #' />"+
                            "<input type='hidden' name='RecipeLinesdetail[#=index(data)#]"+
                            ".Units2.UnitName' value='#= Units2.UnitName #' />");
                    c.Bound(c=>c.Comment)
                        .ClientTemplate("#= Comment # <input type='hidden' name='RecipeLinesdetail[#=index(data)#].Comment' value='#= Comment #' />");
                    c.Command(command => command.Destroy()).Width(150);
                })
                .DataSource(dataSource => dataSource
                .Ajax()
                .Aggregates(agr=>{
                    agr.Add(c=>c.Dosage).Sum();
                })
                .Batch(true)
                .Create(create => create.Action("Create", "Recipes"))
                .Destroy(destroy => destroy.Action("Destroy", "Recipes"))
                .Model(m =>{
                    m.Id(id=>id.RecipeLineId);
                    m.Field(id=>id.RecipeLineId).Editable(false).DefaultValue(0);
                    m.Field(f=>f.ChemistryRecipeViewModel).DefaultValue(
                       new ChemistryRecipeViewModel{
                           ChemistryId="0",
                           ChemistryName=""
                       }); 
                   m.Field(f=>f.Units).DefaultValue(
                       new UnitRecipeViewModel{
                           UnitId=0,
                           UnitName=""
                       }); 
                    m.Field(f=>f.Units2).DefaultValue(
                       new UnitRecipeViewModel{
                            UnitId=0,
                           UnitName=""
                       });
                    m.Field(f=>f.Comment).DefaultValue("");
                }))

                .ToolBar(toolbar =>
                {
                    toolbar.Create();
                    toolbar.Excel();
                    toolbar.Search();
                })

                .Excel(excel=>excel.ProxyURL(Url.Action("Excel_Export_Save", "Grid")))
                .Editable())

And last point is I would like to have it works also when user add or change value 

Is there possibility to update Sum when cell change?

Many thx in advance

Lukas

 

1 Answer, 1 is accepted

Sort by
0
Stoyan
Telerik team
answered on 23 Feb 2022, 01:39 PM

Hello Miroslav,

Thank you for sharing your code.

It looks that you have configured the use of client-side Aggregates of the Grid properly. This means that the Component evaluates the aggregates on the client and they will be updated when a row's dataItem is updated.

That being said to display the aggregates you need to specify a Kendo Template to show the aggregate values. If you'd like to avoid the use of Grouping, I'd recommend using the ClientFooterTemplate configuration to display the aggregates in the Grid's Footer.

 c.Bound(c=>c.Dosage).ClientFooterTemplate("<div>Sum: #= sum #</div>");

Please review the following REPL that showcases the approach.

Alternatively, if you'd like to use Grid Grouping this approach is shown in the Grid Aggregates Demo.

Regards,
Stoyan
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Miroslav
Top achievements
Rank 1
commented on 22 Mar 2022, 08:33 AM

Hello thx for your reply. I try your solution but still not work.
Stoyan
Telerik team
commented on 23 Mar 2022, 01:39 PM

Hi Miroslav,

Upon closer look it seems that the issue is caused by the lack of DataSource transport.update configuration. Therefore when an item is updated its data is not saved and the changes aren't reflected by the Aggregates.

To configure the Update operation please refer to the DataSource Crud Operations article and the Custom editor Demo where in Source the configuration of the Controller Action method are available.

Regards,
Stoyan
Tags
Grid
Asked by
Miroslav
Top achievements
Rank 1
Answers by
Stoyan
Telerik team
Share this question
or