optimized update() to only check the least number of checkboxes

This commit is contained in:
Richard Schreiber
2021-02-19 11:02:42 +01:00
parent 78f2581bb8
commit d5950821e2

View File

@@ -738,22 +738,20 @@ $(function () {
}); });
var update = function () { var update = function () {
var all_true = true; var all_same;
var all_false = true; var checkboxes = $checkboxes.toArray();
$toggle.closest("table").find("td:first-child input[type=checkbox]").each(function () { var i = checkboxes.length;
if ($(this).prop("checked")) { while (i--) {
all_false = false; if (all_same === undefined) {
} else { all_same = checkboxes[i].checked;
all_true = false; continue;
}
if (all_same != checkboxes[i].checked) {
$toggle.prop("checked", false).prop("indeterminate", true);
return;
} }
});
if (all_true) {
$toggle.prop("checked", true).prop("indeterminate", false);
} else if (all_false) {
$toggle.prop("checked", false).prop("indeterminate", false);
} else {
$toggle.prop("checked", false).prop("indeterminate", true);
} }
$toggle.prop("checked", all_same).prop("indeterminate", false);
}; };
$(this).closest("table").find("td:first-child input[type=checkbox]").change(update); $(this).closest("table").find("td:first-child input[type=checkbox]").change(update);