简体   繁体   中英

breeze.js how to do sum on column values — sum(UnitsInTotal)

I am using ASP.NET MVC 4 with knockout and Breeze, and trying to sum a column value (sum(UnitsInTotal)) .

I can do it in Knockout foreach, but to avoid it in cshtml (I have my breeze based column filter's which works fine.), I would like to add it on my Breeze based javascript code itself. Any help ...

var serviceAddress = "breeze/Values",
    em = new breeze.EntityManager(serviceAddress),
    getProducts = function () {
        var query = new breeze.EntityQuery("Product");
        var promise = em.executeQuery(query);
        return promise;
    },
    getSumOfUnitsInTotal = function () {
        var query = new breeze,EntityQuery("Product").where...
        var promise=em.executeQuery(query);
        return promise;
}

I am not sure , how to do SUM(UnitsInTotal) which is a column with numeric Data. I would like to know , how to write the Breeze query to get the SUM value. I am new to breeze.

Finally got it.

Here is my BreezeQuery.

 getItemTotal=function() {var queryAggUnitsInTotal= new breeze.EntityQuery("Product").select("UnitPrice");
   return  em.executeQuery(queryAggUnitsInTotal)
   .then(calculateUnitsInTotal ).fail(handleFail);

calculateUnitsInTotal = function (data) {var unitsInTotal = 0;

       data.results.forEach(function (result)
       {
           unitsInTotal = unitsInTotal + result.UnitPrice;
       });
    return unitsInTotal; --ko.observable(unitsInTotal);--- Sum of UnitpriceValues

// Total value of my UnitPrice column.-- thru ko.observableArray, append and used in cshtml.I am trying to avoid too many knockout calculation at cshtml.// This approach my cshtml looks better and very clean. --Hope this help some dev's.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM