AngularJS Need to bind parent checkbox with child without using ng-model - angularjs

I got an User list using ng-repeat via an API call.
For every User their is 5 categories and each category got 2-3 authorizations on true/false.
For every user/category/authorization I want a checkbox that will change the value of authorization.
When I check the checkbox of a User I want all his 5 categories + all the authorizations been check true and if I uncheck one authorization or categories of a User his checkbox will go false.
So if I recapitulate: there is a parent(User), child(Category) and grand child(Authorization).
I saw that we can use ng-model with ng-checked but in my checkbox, the ng-model is used for the boolean authorization (ng-model="authorization.Enabled") and for more than 1 child this no more work.
How can I manage this case ? Need a $watcher ?
Thanks for your help !

You can use ng-click on each child/grand child.
Pass parent object (user or cathegory) object in it and modify its boolean value on specific condition (when all childs are true or not)
You shouldn't use ng-click and ng-model together, you can avoid doing this by use ng-checked and ng-click on parent(user or cathegory) checkbox.
vm.authorizationCheckboxClick = function (user,category, authorization) {
authorization.booleanValue = !authorization.booleanValue;
if (authorization.booleanValue){
category.booleanValue = true;
category.authorizations.forEach(function(element) {
if(!element.booleanValue){
category.booleanValue = false;
break;
}
}, this);
}
else {
category.booleanValue = false;
}
if (category.booleanValue){
user.booleanValue = true;
user.categories.forEach(function(element) {
if(!element.booleanValue){
user.booleanValue = false;
break;
}
}, this);
}
else {
user.booleanValue = false;
}
}
This example function changing value of authorizations and then changing bolean values of parent category and user. You can bind this values to ng-checked directive

Related

How to target an element when navigating to another page (partial)

I have a checkbox that I'd like to set the indeterminate state to based on the states of other checkboxes. When I'm on the page that the checkboxes are all in, it updates as expected (i.e. the checkbox is found). But when I navigate to that from another page, my method does not find the checkbox (i.e. returns null).
When I debug in Chrome devtools, I notice
let checkBoxWithIndeterminateState;
let checkbox = false;
fireWhenCheckBoxChanged() {
// returns null when navigating from another page but not when on its own page
checkBoxWithIndeterminateState = document.getElementById('checkBoxWithIndeterminateState')
checkBoxWithIndeterminateState.indeterminate = true
}
Template:
<input type="checkbox" id="checkBoxWithIndeterminateState" data-ng-model="checkbox">
How do I wait until the new template has loaded before my method tries to find the checkbox? I've read some suggestions to use this._$scope.$on('$viewContentLoaded'... but this doesn't work.
Thanks!
What about adding an ng-init directive to your target checkbox and do your logic in it, this way you are sure the element is there, here is a suggestion:
<input type="checkbox" ng-init="initTragetCheckbox()">
In your controller
$scope.initTragetCheckbox = function () {
// your code to execute for other checkboxes
var checkbox1 = document.getElementById("checkbox1");
var checkbox2 = document.getElementById("checkbox2");
....
}

How to know if all checkbox of jstree is checked or not

I am using jstree with checkbox.
I have another checkbox with Id chkSelectAll (Select All). When user select it, All jstree checkboxes are checked and if we unselect it, all jstree checkboex are unchecked using below code:
$('#chkSelectAll').change(function() {
if ($('#chkSelectAll').is(":checked"))
{
$("#drpDownSource .source-list").jstree().check_all(true);
}
else
{
$("#drpDownSource .source-list").jstree().uncheck_all(true);
}
});
Now if all jstree checkboxes are selected manually then I want to check chkSelectAll checkbox and if any one jstree checkbox is unchecked then I want chkSelectAll to be unchecked. I am using below code:
So
How can I know whether all jstree checkbox are checked or not?
.on("check_node.jstree uncheck_node.jstree", function (e, data) {
debugger;
if (e.type == "uncheck_node") {
$("#chkSelectAll").prop( "checked", false );
}
else if (e.type == "check_node") {
// here I get only one checkbox's status.
// How to check all checkboxe's status
}
});
Thanks
If you have single parent node with specific id then you can use is_checked (obj) for parent to decide whether all other nodes are checked or not.
Please have a look at https://www.jstree.com/api/#/?q=checkbox&f=check_node.jstree
Please note below
triggered when an node is checked (only if tie_selection in checkbox settings is false)
I hope your tree checkbox plugin configuration is having tie_selection setting to be set as false.
If you are looking for whether all nodes are checked manually to check "Select All" checkbox above then you can use below c
else if (e.type == "check_node") {
if ($(this).jstree().get_json('#', {flat:true}).length === $(this).jstree().get_checked(true).length)
$("#chkSelectAll").prop( "checked", true );
}
For complete and working example, you can have a look at https://everyething.com/Example-of-jsTree-with-select-all-checkbox

Can AngularJs $scope variables be conditionally bounded to each other?

So here my problem
I am trying to make form toggles with a global toggle to basically check and uncheck all of them using ng-model.
$scope.globalToggle =
{
"toggle": true
}
$scope.Toggles =
{
"toggle1": true,
"toggle2": true,
"toggle3": true,
}
This is what Im trying to achieve in an efficient manner:
If global toggle is checked set all to true or false.
If any toggle is un-checked set global to false.
If any toggle is checked and the other two toggles are also checked
then set global to true.
Currently I have managed to use a long winded out process of scope functions and adding ng-click to each checkbox element to achieve this, but I was hoping there is some better way to achieve my goal where I don't have to use ng-click on my elements. Is it possible to have some binding between these two variables that can change their value based on the conditions when they are changed from the DOM using ng-model?
Here you have a snipped which does exactly what you want :)
you just have to put ng-click directive on your toggles and write your algorithm
basicaly that:
globalToggleClicked = function() {
for (var toggle in $scope.Toggles) {
if ($scope.globalToggle.toggle == true)
$scope.Toggles[toggle] = true;
else
$scope.Toggles[toggle] = false;
}
};
toggleClicked = function(toggle){
if (toggle == false)
$scope.globalToggle.toggle = false;
else if (toggle == true) {
var nbChecked = 0;
for (var t in $scope.Toggles) {
if ($scope.Toggles[t] == true)
nbChecked++;
}
if (Object.keys($scope.Toggles).length == nbChecked)
$scope.globalToggle.toggle = true;
}
};
multi toggle management
I know it's with ng-click, but you don't really have other ways to do that

Toggling between $pristine and $dirty in AngularJS

I want to detect when a user has entered values into any form field by using the $dirty property and setting a flag accordingly. Not surprisingly, this works:
$scope.$watch('formDetails.$dirty', function() {
USR.userInputRecorded = true;
});
But I'd also like to detect when/if the user has emptied all fields and effectively restored the form to its original empty state. The snippet below does not work and I'm not sure why. Is there a way to watch for when the form changes back to "not dirty"?
$scope.$watch('formDetails.$pristine', function() {
USR.userInputRecorded = false;
});
Thanks.
Try this:
$scope.$watch('formDetails.$dirty', function(value) {
if (value === '') {
// field has been emptied;
your.form.$setPristine(true);
} else {
USR.userInputRecorded = true;
}
});

filtering in angular by select and checkbox

want to filter out values both the select box and in checkbox i achieved in select box but can't able to get from checkbox
MY PLUNKER DEMO
Example Here. Use a single object for the checkboxes, and then you can use a comparator function in the filter, like this:
$scope.checkMake = function(value) {
//Assume no filter is applied so the value will be shown.
var result = true;
for (var i in $scope.selected) {
if ($scope.selected[i] === true) {
//if we get here, we know a filter is applied, so
//check to see if the passed value is one of the selected
//checkboxes
result = $scope.selected[value];
break;
}
}
return result;
}

Resources