AngularJs 复选框 select 全部
AngularJs checkbox select all
我正在尝试向 select/deselect 所有项目添加一个复选框,然后对所有项目求和。我已经为它做了一些代码。但是,这两个功能现在是分开的,我想将它们合并在一起以实现目标。
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()" 但没有成功。
如果您只考虑用一个函数来处理两个复选框,那么您可以用一个带有 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);
}
}
});
}
}
您可以将两个复选框合并为一个,同时执行这两个操作,即 select 全部并根据一个复选框的更改计算余额。在 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();
};
希望对您有所帮助。
我正在尝试向 select/deselect 所有项目添加一个复选框,然后对所有项目求和。我已经为它做了一些代码。但是,这两个功能现在是分开的,我想将它们合并在一起以实现目标。
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()" 但没有成功。
如果您只考虑用一个函数来处理两个复选框,那么您可以用一个带有 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);
}
}
});
}
}
您可以将两个复选框合并为一个,同时执行这两个操作,即 select 全部并根据一个复选框的更改计算余额。在 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();
};
希望对您有所帮助。