[英]How to get sum of checkboxes checked column values dynamically using jQuery
Angular Datatable: How to get sum of checked values amount dynamically in Datatable. Angular 数据表:如何在数据表中动态获取检查值的总和。
Have one datatable and checkbox, once we check the checkboxes selected and need to sum of all selected checkbox amount.有一个数据表和复选框,一旦我们检查了选中的复选框,并且需要对所有选中的复选框数量求和。 I tried the following code, getting values but unable to sum.
我尝试了以下代码,获取值但无法求和。 If I uncheck checkbox need to reduce the unchecked box amount.
如果我取消选中复选框需要减少未选中框的数量。 Please suggest the issue.
请提出问题。
checkboxCheckAmountCount(info: any): void {
var creditAmount = 0;
var checkedCount = $("#firstTable input:checked").length;
var amountAdd = false;
for (var i = 0; i < checkedCount; i++) {
if (info.index != "") {
alert(info.index);
creditAmount += parseFloat(info.index);
} else {
creditAmount = 0;
}
}
$("#idCheckBoxLabel").text(creditAmount);
}
The following is the function which works fine as per your requirement:以下是 function 可以根据您的要求正常工作:
checkboxCheckCount(info: any): void {
var creditAmount = 0;
var amountAdd = false;
$("#firstTable input:checked").each(function() {
if (info.index != "") {
creditAmount += parseInt($(this).prop("value"));
} else {
creditAmount = 0;
}
});
$("#idCheckBoxLabel").text(creditAmount);
}
You can use on reduce
on all checked items to get the sum as -您可以在所有检查项目上使用 on
reduce
来获得总和 -
checkboxCheckCount(info: any): void {
var creditAmount = $("#firstTable input:checked").toArray().reduce(function(sum: number, element: any) {
return sum + Number(element.value);
}, 0);
$("#idCheckBoxLabel").text(creditAmount);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.