Ways to choose all checkboxes using a Master checkbox in AngularJS

In my setup, I have a Top Master select box that can activate or deactivate checkboxes for Master 1 and Master 2. Additionally, each of the Masters has 4 Slave checkboxes that can be activated or deactivated accordingly. The implementation uses ng-model and ng-checked directives.

For example:

<input type="checkbox" ng-checked="top" ng-model="master1"/> Master 1  

Now, my query is:

  1. When the TOP Master is selected, why aren't the slaves of Master 1 and Master 2 automatically selected, even though their ng-models (Master 1 and Master 2) are already selected? However, selecting either Master 1 or Master 2 results in their respective slaves getting selected.
  2. If all 4 slaves under a Master are selected, the Master itself should also be selected. Furthermore, when both Masters are selected, then the TOP Master should automatically get selected as well. How can this functionality be achieved? And which controller should be utilized for this purpose?

Please review the code on Fiddle http://jsfiddle.net/aF2aL/32/

Answer №1

To make the necessary change, modify ng-checked="master1" to ng-checked="(master1||top)&&master1"

<div ng-controller="myCtrl">
    <input type="checkbox" ng-model="top"/> Top Master
   <ul> <input type="checkbox" ng-checked="top" ng-model="master1"/> Master 1  
        <ul ng-repeat="slave in slaveOne"> 
              <input id="checkSlave" type="checkbox" ng-checked="(master1||top)&&master1"/>{{slave.name}} 
        </ul>
    </ul>

  <ul>  <input type="checkbox" ng-checked="top" ng-model="master2"/> Master 2  
        <ul ng-repeat="slave in slaveTwo"> 
            <input id="checkSlave" type="checkbox" ng-checked="(master2||top)&&master2"/> {{slave.name}}
      </ul> 
  </ul>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Could not load ngx-restangular due to an error: (SystemJS) Module not yet loaded while trying to load "@angular/core"

Recently, I made the switch from using the AngularJS 'restangular' library to the Angular 'ngx-restangular' library during an upgrade from AngularJS to Angular. However, after the transition, I encountered an unexpected error along wit ...

Transform jQuery timer functionality to support both minutes and seconds for redirecting, rather than just seconds

Recently, I stumbled upon a fantastic timer and redirect script based on jQuery from the talented folks over at the 'jQuery by Example' website. This script automatically redirects users after a specified number of seconds has elapsed: Here&apos ...

Is there a Javascript database that functions seamlessly on Android, IOS, and Windows Phone 8 (Internet Explorer 10)?

I am currently maintaining an app for Android, iPhone, and Windows that relies on an ASP.NET Server with SQL Server Database. I would like to make the app functional offline as well, which requires utilizing a database on the client side. Despite my best ...

Looping through Angular injection

I am facing an issue with my AngularJS setup and I am unable to find a straightforward solution. My authentication process involves two tokens: Access token and Refresh token, managed by two separate services - Auth and Api. When a user accesses data usin ...

HtmlUnit: clicking on an anchor element does not result in the event being triggered

When I attempt to click on this specific link: <a id="table_statementlist_get_ib_table:0:ordernumber" class="OraLink" href="#" onclick="return _chain('disable_load_window();','submitForm(\'ib_trn_base_pas_statementlist_get&bsol ...

Exploring ways to display dynamic content within AngularJs Tabs

I need help figuring out how to display unique data dynamically for each tab within a table with tabs. Can anyone assist me with this? https://i.stack.imgur.com/63H3L.png Below is the code snippet for the tabs: <md-tab ng-repe ...

Combining shared table rows with embedded ruby code in Javascript - is it possible?

I'm a new Javascript learner and I'm attempting to create a function that will merge rows with the same value (year in this case) and add their numbers together. Although my code isn't functioning as expected, it also doesn't seem to be ...

AngularJS Formly's ui-mask feature: Stripping input mask characters from the model

I recently implemented an input mask on a 10-digit phone number field within my Formly form using AngularJS. The input is masked with dashes (xxx-xxx-xxxx) and displays correctly on the page. However, the issue arises when attempting to ignore these mask c ...

AngularJS - Issue encountered while attempting to inject a service from an external module

As a newcomer to AngularJs, I am facing a challenge with the issue described below. In the scenario presented, there are two services named MongoRESTService and MongoRESTService2 defined within the common-services module. The main module, olive, has a de ...

Learn how to move a div inside another div using drag and drop, ensuring that the entire element moves along with it

I am trying to implement jQueryUI for my project. I have a list of components that I want to drag and drop into a grid system, similar to a designer tool. I am using the following code to make the div draggable: $( "#Component1" ).draggable(); Then I dro ...

Leveraging underscore.js for null verification

let name = "someName"; if(name !== null) { // perform some action } Currently, I am utilizing http://underscorejs.org/#isNull. How can I achieve the same functionality using underscore.js? Is there any noticeable performance enhance ...

Create a toggle effect similar to jQuery using JavaScript

Looking for pure JavaScript code to implement show/hide functionality similar to jQuery. Currently, I am using the following code: y=document.getElementById('regform').style.display; if (y == 'block'){ document.getElementById(&apo ...

Working with string interpolation in SQLite3 and Nodejs

Just starting out with this and I'm running into an issue with trying to insert a variable into my sqlite3 query. I keep getting the error { [Error: SQLITE_ERROR: no such column: shmee] errno: 1, code: 'SQLITE_ERROR' } where "shmee" is actua ...

Display exclusively the chosen option from the dropdown menu

I am facing an issue with the select input on my webpage. Whenever I try to print the page, it prints all the options of the select instead of just the one that is selected. Can someone please guide me on how to modify it so that only the selected option ...

Learning how to utilize localStorage in conjunction with a color picker to modify the --var of :root

After spending an entire afternoon on my JavaScript issue as a beginner, I have a specific goal in mind: allowing the user to select and change the main color of the page. To implement this, I inserted a color picker in my HTML: <input type="color ...

Utilizing React for Redirecting to an External Site

I came across a solution for redirecting to an external link, which worked for one of my pages. However, the same solution does not seem to be working for a different page, and I'm unsure of the reason why. redirect.jsx: import React from "react"; ...

Could you please ensure that the animation activates upon hovering over the "a" element?

Utilizing bootstrap, I have created the following code. I am looking to add functionality that triggers an animation upon mouseover of an img or a element, and stops the animation upon mouseleave. .progress-bar { animation: pr 2s infinite; } @keyfr ...

Check to see if a variable has been assigned a value in the form of a jquery

Scenario: <div> <div class="application-section active"></div> <div class="application-section"></div> </div> I need to define the following variables var $activeSection = $("div.application-section.active") ...

How come my AngularJS view isn't loading properly on Cordova?

Looking at my index.html, it appears as follows: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="format-detection" content="telephone=no" /> <meta name="viewport" content="user-scalable=no, ...

What is the best way to fill multiple select options using a universal function in AngularJS?

Currently, I am developing the following code: <select class="form-control" ng-init="agreementTypes = initializeTypes('ManageMission/GetDescriptions/?type=AgreementTypes')" ng-model="mission.AgreementType" ng-options= ...