AngularJs复选框全选

问题描述 投票:0回答:2

我正在尝试添加一个复选框来选择/取消选择所有项目,然后对所有项目求和。我为它做了一些代码。但是,这两个函数现在是分开的,我想将它们合并在一起以实现目标。

HTML

//Select all checkbox
<input type="checkbox" ng-change="checkAll()" ng-model="params.checkbox[0]" 
class="ng-valid ng-dirty ng-valid-parse ng-touched ng-empty" style="">

//Select a single checkout and do cal balance
<input type="checkbox" name="marked_reconciled" ng- 
model="trx.marked_reconciled" ng-change="calBalance()"> 

这是AngularJs

$scope.checkAll = function () { angular.forEach($scope.records, function (v) 
{ $scope.params.checkbox[v.id] = $scope.params.checkbox[0]; }); };

$scope.calBalance = function () {
                if (!$scope.trans) {
                    return;
                }
                var current = 0;
                var statement_mth = new Date($scope.item.reconcile_date).getMonth();
                angular.forEach($scope.trans.trans, function (trx) {
                    var reconcile_mth = new Date(trx.reconcile_on).getMonth();
                    if (trx.marked_reconciled && ((statement_mth == reconcile_mth) || !trx.reconcile_on)) {
                        if (trx.entry == 'debit') {
                            current += parseFloat(trx.amount);
                        } else if (trx.entry == 'credit') {
                            current -= parseFloat(trx.amount);
                        }
                    }
                });
            };

我试图将ng-change合并为“checkAll(); calBalance()”但不起作用。

javascript angularjs checkbox angularjs-ng-change
2个回答
0
投票

您可以将两个复选框合并为一个执行两个操作,即选择全部并在一个复选框上计算更改的余额。在html中:

<input type="checkbox" ng-change="checkAllAndCalBalance()" ng-model="params.checkbox[0]" 
class="ng-valid ng-dirty ng-valid-parse ng-touched ng-empty" style="">

并在逻辑中创建一个新的函数checkAllAndCalBalance来完成这两项任务。由于另一个复选框的ng-model不再存在,您必须在逻辑中手动设置其值,以便依赖于trx.marked_reconciled的计算不会受到影响:

 $scope.checkAll = function () { angular.forEach($scope.records, function (v) 
{ $scope.params.checkbox[v.id] = $scope.params.checkbox[0]; }); };

$scope.calBalance = function () {
                if (!$scope.trans) {
                    return;
                }
                var current = 0;
                var statement_mth = new Date($scope.item.reconcile_date).getMonth();
                angular.forEach($scope.trans.trans, function (trx) {
                    var reconcile_mth = new Date(trx.reconcile_on).getMonth();
                    if (trx.marked_reconciled && ((statement_mth == reconcile_mth) || !trx.reconcile_on)) {
                        if (trx.entry == 'debit') {
                            current += parseFloat(trx.amount);
                        } else if (trx.entry == 'credit') {
                            current -= parseFloat(trx.amount);
                        }
                    }
                });
    };

$scope.checkAllAndCalBalance = function(){
    //as not specified in ng-model, setting the value of trx.marked_reconciled manually in logic
    $scope.trx.marked_reconciled=$scope.params.checkbox[0];
    //select all  
    $scope.checkAll();
    //calculate balance
    $scope.calBalance();
};

希望这可以帮助。


0
投票

如果您只考虑使用单个函数来处理复选框,那么您可以使用if条件来区分两个不同的复选框控件。

//Select all checkbox
<input type="checkbox" ng-change="checkBoxSelection('All')" ng-model="params.checkbox[0]" 
class="ng-valid ng-dirty ng-valid-parse ng-touched ng-empty" style="">

//Select a single checkout and do cal balance
<input type="checkbox" name="marked_reconciled" ng- 
model="trx.marked_reconciled" ng-change="checkBoxSelection('')"> 


$scope.checkBoxSelection = function(mode){
    if(mode==='All'){
        angular.forEach($scope.records, function (v) { 
            $scope.params.checkbox[v.id] = $scope.params.checkbox[0]; 
        });
    }else {
       if (!$scope.trans) {
           return;
       }
       var current = 0;
       var statement_mth = new Date($scope.item.reconcile_date).getMonth();
       angular.forEach($scope.trans.trans, function (trx) {
           var reconcile_mth = new Date(trx.reconcile_on).getMonth();
           if (trx.marked_reconciled && ((statement_mth == reconcile_mth) || !trx.reconcile_on)) {
               if (trx.entry == 'debit') {
                   current += parseFloat(trx.amount);
               } else if (trx.entry == 'credit') {
                   current -= parseFloat(trx.amount);
               }
           }
       });
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.