【问题标题】:AngularJs checkbox select allAngularJs复选框全选
【发布时间】:2018-11-08 17:16:40
【问题描述】:

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

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()" 但不起作用。

【问题讨论】:

  • 你的html里只有两个复选框?还有其他复选框吗?
  • @Nabil Shahid 表格正文中有其他复选框,表格第一行有“全选”复选框。

标签: javascript angularjs checkbox angularjs-ng-change


【解决方案1】:

您可以将两个复选框合并为一个执行这两种操作,即全选并计算一个复选框上的更改的余额。在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();
};

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    如果您只考虑使用单个函数来处理两个复选框,那么您可以使用带有 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);
                   }
               }
           });
        }
    }
    

    【讨论】:

    • 感谢您的回复。我已经测试了该功能,但复选框仍然无法全选。关键是 ng-model,它是 params.checkbox[0] 和 trx.marked_reconciled。如果我使用 params.checkbox[0] 替换 trx.marked_reconciled,它可以选择所有复选框,但该项目不会计算。
    • 您是否希望针对这两种不同的行为将模板复选框控件合并为一个?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    相关资源
    最近更新 更多