【问题标题】:How to get SUM of the table field? - Angularjs & Sqlite如何获取表字段的 SUM? - Angularjs 和 Sqlite
【发布时间】:2020-01-01 15:20:22
【问题描述】:

我正在尝试使用连接到我的 sqlite 数据库的 Angularjs 获取我的表行(数量)的总和

我已经成功地构建了 CRUD 操作,但现在我一直在获取我的金额字段的总和

这是我的 html 代码的一部分:

<table id="tableSales" ng-init="listSales()">

    <thead class="bz-tablecell">
        <tr>
            <th id="table-checkbox"><input type="checkbox" onclick="toggle(this);" id="chk-all"></th>
            <th><b>Name</b></th>
            <th><b>Amount</b></th>
            <th><b>Quantity</b></th>
            <th><b>Customer</b></th>
            <th><b>Date</b></th>
            <th class="export-ignore"><b>Status</b></th>
            <th class="export-ignore"><b>Actions</b></th>
        </tr>
    </thead>
    <tbody>
        <tr class="bz-tablecell" dir-paginate="sale in sales|filter:search|itemsPerPage:20">
            <td id="table-checkbox"><input type="checkbox"></td>
            <td style="font-weight: 600">{{sale.name}}</td>
            <td>{{sale.amount | currency: "TZS "}}</td>
            <td>{{sale.quantity}}</td>
            <td><a href="#">{{sale.customer}}</a></td>
            <td>{{sale.date}}</td>
            <td class="export-ignore"><span class="approved" style="border-radius: 0 !important;">{{sale.status}}</span></td>
            <td class="export-ignore"><a ng-click="delete(sale)">Manage</a></td>
        </tr>
        <tr ng-if="sales.length <= 0">
            <td colspan="8" rowspan="4" class="center empty-state">
              <b class="top">There are no sales yet!</b>
              <br>Start by adding a new one.
            </td>
        </tr>
    </tbody>
</table>

这是我执行 CRUD 操作的 angularjs 代码:

"USE STRICT";
app.controller("salesController", function ($scope, $location, dbService) {
    $scope.sub = {
        'title': 'Sales Orders'
    }

    $scope.listSales = function () {
       //FETCH
        dbService.runAsync("SELECT * FROM sales WHERE active = 1", function (data) {
            $scope.sales = data;
        });
    }

    $scope.save = function () {
        if ($scope.sale.id) {
            //EDIT
            var id = $scope.sale.id;
            delete $scope.sale.id;
            delete $scope.sale.$$hashKey;
            dbService.update('sales', $scope.sale, {
                id: id
            });
        } else {
            //SAVE
            dbService.insert('sales', $scope.sale);
        }
        $scope.sale = {};
        $scope.listSales();
    }


    $scope.delete = function (data) {
        //DELETE
        if (confirm("Are you sure you want to delete this sale?")) {
            dbService.update('sales', {
                active: 0
            }, {
                id: data.id
            });
            $scope.listSales();

        }
    }


});

我需要做的是获取所有金额字段的总和,请帮助。谢谢

【问题讨论】:

    标签: javascript angularjs sqlite


    【解决方案1】:

    计算总和:

    $scope.listSales = function () {
       //FETCH
        dbService.runAsync("SELECT * FROM sales WHERE active = 1", function (data) {
            $scope.sales = data;
            //COMPUTE Sum
            $scope.sum = data.reduce(  ( (_,sum) => sum + _.amount) , 0);
        });
    }
    

    有关详细信息,请参阅

    【讨论】:

    • 你好@georgeawg。我已经在我的控制器上附加了上面的代码,但是当我添加新项目而不是得到总和时,它显示对象未定义
    • 我需要在我的 html 中显示我的项目总和
    猜你喜欢
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多