lostinwpf
lostinwpf

Reputation: 663

Summing a calculated unbound column in KendoUI Grid MVC

I have a KendoUI Grid where I need to display a sum of several of the columns in the footer. As the Items column is bound this seems to be working perfectly.

I also have a Total Net £s for Items column which is calculated but I cannot get the aggregate function to work. Is it possible to sum a calculated column?

 columns.Bound(m => m.Items).Format("{0:n2}")
.HtmlAttributes(new { @class = "items" }).Title("Units")
.ClientFooterTemplate("#= kendo.format('{0:n2}', sum) #");
 columns.Template(p => { })
.ClientTemplate("#= kendo.toString((Items * 12) * NetPoundsPerItem, 'n2') #")
.Title("Total Net £s for Items")
.FooterTemplate("#= kendo.tostring(sum, \"n2\") #");

Upvotes: 1

Views: 1924

Answers (1)

Micah Armantrout
Micah Armantrout

Reputation: 6971

because this is a unbound column you will need to keep track of your numbers manually and then put your sum in your aggregate column. Use a footer template

  columns: [
        { field: "Cost", width: 120 }, footertemplate: "<div id='total'> Total : $0.00 </div> }, 
    { field: "Quanity", title: "Quanity", width: 40}]

Use javascript to calculate the sum and then use jquery to set it.

 //get the correct grid Kendo Grid
 var grid = $("#grid").data("kendoGrid");

 //get the data
var data = grid.dataSource.view();

var FinalTotal = 0;

// now iterate through each cell 
  data.forEach(function (entry) {
       FinalTotal += entry["Quanitiy"] * entry["Price"];
  }
// set the aggregate template div
    $('#total').text("Total: " + kendo.toString(FinalTotal, 'C'));

Upvotes: 1

Related Questions