使用角度js获取ng-repeat内的总和值

时间:2020-12-07 19:45:06

I used ng-repeat to repeat json array. I calculated Night(s) by using dayDiff() function. Now I want to get total night all invoices. I am using angularjs.

我用ng-repeat重复json数组。我使用dayDiff()函数计算了Night(s)。现在我想得到所有发票的总计。我正在使用angularjs。

How can I get total nights for all invoices?

我怎样才能获得所有发票的总夜数?

<table class="table" ng-show="filteredItems > 0">
    <tr>
        <td>No</td>
        <td>Invoice No</td>
        <td>Name</td>
        <td>Eamil</td>
        <td>Room Name</td>
        <td>Check In Date</td>
        <td>Check Out Date</td>
        <td>No. Room</td>
        <td>Night(s)</td>
        <td>Booking Date</td>
        <td>Amount</td>
    </tr>
    <tr ng-repeat="data in filtered = (list | filter:search ) | startFrom:(currentPage-1)*entryLimit | limitTo:entryLimit">
        <td>{{$index+1}}</td>
        <td>{{data.invoicenumber}}</td>
        <td>{{data.firtname}}{{data.lastname}}</td>
        <td>{{data.email}}</td>
        <td>{{data.roomname}}</td>
        <td ng-model='fromDate'>{{data.cidt}}</td>
        <td ng-model='toDate'>{{data.codt}}</td>
        <td>{{data.qty}}</td>
        <td ng-model='night'>{{dayDiff(data.cidt,data.codt)}}</td>
        <td>{{data.bdt}}</td>
        <td>{{data.btotal}}</td>
    </tr>
</table>

2 个解决方案

#1


0  

You need to add an extra row to begin with. This extra row will look like this:

您需要添加一个额外的行开头。这个额外的行将如下所示:

<tr>
    <td colspan="11">Total nights: {{calcTotal(filtered)}}</td>
</tr>

Then in your controller you need to add a function to calculate the nights like

然后在你的控制器中你需要添加一个函数来计算夜晚

$scope.calcTotal = function(filtered){
     var sum = 0;
     for(var i = 0 ; i<filtered.length ; i++){
        sum = sum + filtered[i].nights;
     }
     return sum;
};

#2


0  

You could first, use a factory for your JSON model, to store the computed nights:

您可以先使用JSON模型的工厂来存储计算的夜晚:

// We inject the dayDiff function via the dayDiffService service
angular.factory('invoice', ['dayDiffService', function(dayDiffService) {
    var Invoice = function(data) {
        // merge json properties to self
        angular.merge(this, data);
        // we compute the night(s)
        this.nights = dayDiffService.dayDiff(data.cidt, data.codt);
    }
    return Invoice;
}]);

Then, in your controller, you add a function to sum up the nights from a filtered list:

然后,在您的控制器中,添加一个函数以从过滤后的列表中总结夜晚:

angular.controller('invoicesCtrl', ['$scope', 'invoice', function($scope, Invoice) {
    $scope.list = [];

    // let's say that JSON holds your json model from http's response
    $scope.list = JSON.map(function() { 
        return new Invoice(i) 
    });

    $scope.sumNights = function(filtered) {
        filtered.reduce(function(sum, invoice) {
            sum += invoice.nights;
            sum
        }, 0);
    }
}]);

Then, in your html you add a new row to display the computed result:

然后,在你的html中添加一个新行来显示计算结果:

<div ng-controller="invoicesCtrl as vm">
    <table>
        ...
        <tbody>
            <tr ng-repeat="data in filtered = (vm.list | filter:search ) | startFrom:(currentPage-1)*entryLimit | limitTo:entryLimit">
                <td>{{$index+1}}</td>
                ...
                <tr>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="8"></td
                <td>{{vm.sumNights(filtered)}}</td
                <td colspan="2"></td
            </tr>
        </tfoot>
    </table>
</div>

#1


0  

You need to add an extra row to begin with. This extra row will look like this:

您需要添加一个额外的行开头。这个额外的行将如下所示:

<tr>
    <td colspan="11">Total nights: {{calcTotal(filtered)}}</td>
</tr>

Then in your controller you need to add a function to calculate the nights like

然后在你的控制器中你需要添加一个函数来计算夜晚

$scope.calcTotal = function(filtered){
     var sum = 0;
     for(var i = 0 ; i<filtered.length ; i++){
        sum = sum + filtered[i].nights;
     }
     return sum;
};

#2


0  

You could first, use a factory for your JSON model, to store the computed nights:

您可以先使用JSON模型的工厂来存储计算的夜晚:

// We inject the dayDiff function via the dayDiffService service
angular.factory('invoice', ['dayDiffService', function(dayDiffService) {
    var Invoice = function(data) {
        // merge json properties to self
        angular.merge(this, data);
        // we compute the night(s)
        this.nights = dayDiffService.dayDiff(data.cidt, data.codt);
    }
    return Invoice;
}]);

Then, in your controller, you add a function to sum up the nights from a filtered list:

然后,在您的控制器中,添加一个函数以从过滤后的列表中总结夜晚:

angular.controller('invoicesCtrl', ['$scope', 'invoice', function($scope, Invoice) {
    $scope.list = [];

    // let's say that JSON holds your json model from http's response
    $scope.list = JSON.map(function() { 
        return new Invoice(i) 
    });

    $scope.sumNights = function(filtered) {
        filtered.reduce(function(sum, invoice) {
            sum += invoice.nights;
            sum
        }, 0);
    }
}]);

Then, in your html you add a new row to display the computed result:

然后,在你的html中添加一个新行来显示计算结果:

<div ng-controller="invoicesCtrl as vm">
    <table>
        ...
        <tbody>
            <tr ng-repeat="data in filtered = (vm.list | filter:search ) | startFrom:(currentPage-1)*entryLimit | limitTo:entryLimit">
                <td>{{$index+1}}</td>
                ...
                <tr>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="8"></td
                <td>{{vm.sumNights(filtered)}}</td
                <td colspan="2"></td
            </tr>
        </tfoot>
    </table>
</div>