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

How to sum per row plus previous row using row index

1 Answer 128 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ivica
Top achievements
Rank 1
Ivica asked on 12 Dec 2018, 11:35 AM

Hi, 

could you help me about following calculations in the grid for telerik asp.net core (C#)

IN - OUT = SALDO (for row index 0)
IN - OUT = SALDO (for row index 1) + SALDO from previous row

image example attached

1 Answer, 1 is accepted

Sort by
0
Accepted
Eyup
Telerik team
answered on 13 Dec 2018, 03:02 PM
Hello Ivica,

I've replied to your query in your support ticket. I am also posting the approach here which can prove helpful to other developers as well:

You can achieve this requirement using the following approach:
@(Html.Kendo().Grid<KendoCoreTrialPack1.Models.OrderViewModel>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.OrderID).Filterable(false);
        columns.Bound(p => p.Freight);
        columns.Bound(p => p.OrderDate).Format("{0:MM/dd/yyyy}");
        columns.Bound(p => p.ShipName);
        columns.Bound(c => c.ShipCity).ClientTemplate("<span class='calculatedValue'></span>");
    })
    .Events(e => e.DataBound("gridDataBound"))
    ...
JavaScript:
function gridDataBound(e) {
    var items = e.sender.items();
    for (var i = 0; i < items.length; i++) {
        var dataItem = e.sender.dataItem(items[i]);
        var value1 = dataItem.OrderID;
        var value2 = dataItem.Freight;
 
        var prevResult = 0;
        if (i > 0) {
            var prevCellValue = $(items[i - 1]).find(".calculatedValue").text();
            if (isFinite(prevCellValue)) {
                prevResult = parseFloat(prevCellValue);
            }
        }
 
        $(items[i]).find(".calculatedValue").text(value2 - value1 + prevResult);
    }
}

I hope this will prove helpful.


Regards,
Eyup
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Ivica
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Share this question
or