Several Checkbox Options with Switchable Values

My task is to create a set of three checkboxes with specific conditions when clicked.

For instance, consider checkboxes 1, 2, and 3.

If checkbox 1 is selected, then checkboxes 2 and 3 should be enabled.

If checkbox 1 is changed to 'false', then checkboxes 2 and 3 need to be disabled.

Additionally, if checkbox 1 is unchecked, the values of checkboxes 2 and 3 should be reset to 'false' and they should be disabled.

I have managed to achieve the above conditions. However, the issue lies in the fact that the value is not updating in ngmodel.

Sample code snippet:


<td>
    <div class="col-lg-12" id="radiobutton">
        <input #EDIsRead=ngModel [(ngModel)]="GetRoleIsRead.EDIsRead" name="GetRoleIsRead.EDIsRead" class="form-check-input col-sm-2"
        type="checkbox" id="EDIsRead" style="padding-left: 0px !important;" (change)="CheckeventforRead('ED')">
    </div>
</td>
<td>
    <div class="col-lg-12" id="radiobutton">
        <input #EDIsModifyLOB=ngModel [(ngModel)]="GetRoleIsModifyLOB.EDIsModifyLOB" name="GetRoleIsModifyLOB.EDIsModifyLOB" class="form-check-input col-sm-2"
        type="checkbox" id="EDIsModifyLOB" style="padding-left: 0px !important;" (change)="CheckeventforModifyLOB('ED','0')">
    </div>
</td>
<td>
    <div class="col-lg-12" id="radiobutton">
        <input #EDIsModifyAll=ngModel [(ngModel)]="GetRoleIsModifyAll.EDIsModifyAll" name="GetRoleIsModifyAll.EDIsModifyAll" class="form-check-input col-sm-2"
        type="checkbox" id="EDIsModifyAll" style="padding-left: 0px !important;" (change)="CheckeventforModifyAll('ED','0')">
    </div>
</td>

Input parameters given are 'true true false'.

So, I am passing 'true' for checkbox 1, 'true' for checkbox 2, and 'false' for checkbox 3 with two-way binding.

If I change checkbox 3 to 'true', then checkbox 2 becomes unchecked but the value does not change.

Here is an example of my JSON data.

Before:

{"GetRoleIsRead.EDIsRead": true, "GetRoleIsModifyLOB.EDIsModifyLOB": false, "GetRoleIsModifyAll.EDIsModifyAll": true }

After checking the second checkbox:

{"GetRoleIsRead.EDIsRead": true, "GetRoleIsModifyLOB.EDIsModifyLOB": true, "GetRoleIsModifyAll.EDIsModifyAll": true }

The expected output should be:

{"GetRoleIsRead.EDIsRead": true, "GetRoleIsModifyLOB.EDIsModifyLOB": true, "GetRoleIsModifyAll.EDIsModifyAll": false}

Answer №1

Hopefully, I've grasped the essence of your query. I have created a model using stackblitz. If this aligns with your requirements, please let me know and I can provide further clarification.

In brief - I utilized the [disabled] property to toggle the activation of inputs, followed by the implementation of the (change) event to reset the values stored in the variables.

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

Looking for the most effective approach to implement responsive styling in an Angular 7 project? I've already included CSS for every component, but I

Looking for guidance on implementing Responsive Style in an Angular 7 Project. I have included CSS for each component, but when I apply responsive styles globally, they do not inherit as expected. Should I add responsive styles individually to each compo ...

Typescript extra property specifications

I need some assistance with creating a custom input field using typescript and Formik. Can someone please help me figure out how to include additional props like label & name in the code snippet below? I've been stuck on this for a while and I have a ...

Observables do not provide any results when used in a pipe with an image src

I recently created a custom pipe for the image src in my application: It is applied to selectors like this: <img [src]="myobject?.URL | secure" /> Here's the code snippet for the pipe: import { Pipe, PipeTransform } from '@angular/c ...

Angular 12: Running ng test shows code coverage error - TypeError: Unable to access 'initialize' property as undefined

I encountered an error in the code coverage console: TypeError: Cannot read properties of undefined (reading 'initialize') I am trying to call a service method from the component.ts file The code in the component.ts file looks like: this.myAuth ...

The call does not match any overloads in Vue when using TypeScript

What is the reason behind the occurrence of the error in the ID part of the query? This call does not have a matching overload. <template> <swiper-slide slot="list" v-for="(list, index) in list.banner" :key=" ...

Mistakenly importing the incorrect version of Angular

While working on my Angular 1 app in typescript, I faced an issue when importing angular using the following syntax: import * as angular from 'angular'; Instead of importing angular from angular, it was being imported from angular-mocks. Thi ...

Launching Angular Application on GitHub Pages

I am encountering an issue with my GitHub Pages website that is connected to a custom domain. My domain is hosting a web application created with Angular, but I am having trouble loading the .js and .css files when I visit the site. In the DevTools/Networ ...

Angular2 ngFor, encountering undefined property

Having an issue where one of my properties is showing as "undefined" even though it is defined. Can't seem to find a solution: I have a parent component with the following data: @Component({ selector: "app-my-products", templateUrl: ...

Guide to implementing the collapsible start and stop button feature in Angular

Having an issue in my Angular application with the dashboard page. I've created a button for start or stop (toggle functionality) but it's not working as expected. .component.ts toggleCollapse(jammer) { this.jammer.isCollapsed ? 'START& ...

Implementing pagination for an Angular Material table using an HTTP request to set the page

How can I update the table page index when users click on next and previous buttons in an API that fetches data based on a specified Page number? It's important to note that I have already created a shared table. @Input() columns: Column[] = []; @In ...

Is there a way to drop a pin on the Google Maps app?

Is there a way to pinpoint the specific location on Google Maps? <google-map id="map-container" width="100%" height="100%" class="maps"></google-map> ...

How can I toggle the visibility of a div after the DOM has finished loading?

I was experimenting with a radio button on the interface linked to a property in the typescript file that controls the visibility of another div. However, I noticed that both *ngIf="isFooSelected" and [hidden]="!isFooSelected" only function upon initial pa ...

An error in npm occurred: "The name "@types/handlebars" is invalid."

While attempting to install typedoc using npm, I encountered the following error: npm ERR! Invalid name: "@types/handlebars" To resolve this issue, I proceeded to install @types/handlebars directly by running: npm install @types/handlebars However, I r ...

The information being sent from Angular is not being successfully transmitted to the XAM

Here is my Angular service post method: getUserDetails(username , password) { alert(password); return this.http.post<myData>("http://localhost/test/api/auth.php", { username, password }); } This is the structure of my PHP file: <?php ...

Transform the information sent from the server into a user-friendly interface using typescript on the frontend

Received data from the backend looks like this: id: number; user_id: number; car_brand: string; car_model: string; vin: string; equipment: string; reg_number: string; car_mileage: number; car_year: string; Meanwhile, the interface in the ...

Angular 4: Retrieving the selected element's object from a collection of elements

In my x.component.html file, I have a list of objects that are being displayed in Bootstrap cards like this: <div *ngFor="let item of items | async"> <div class="row"> <div class="col-lg-6"> <div class="card ...

Challenge with Routing in Angular 2

I'm dealing with a route setup like this: path: 'something/:id/somethingElse' In my header.component.html, I need to include a link to this page. I tried the following approach: <a routerLink="['something',myIdThatIsDynamicO ...

Troubleshooting type casting issue in Typescript when working with objects containing getters and setters

I'm facing an issue with the code snippet provided. I suspect it might be related to casting, but I'm unable to pinpoint the exact solution. interface Code { code: string; expiration: number; } interface IActivationCode { [userId: string] ...

React is inferring the type of the 'charts' property in the object literal as 'any[]'

ide: vscode typescript: 2.7.1 react: 16.3.0-alpha.1 interface IState { numbers: number[]; } class CustomCanvas1 extends React.Component<undefined, IState> { constructor(properties: undefined) { super(properties); this.state = { ...

Extending Error object disrupts `instanceof` validation in TypeScript

Could someone clarify why the error instanceof CustomError part of the code below returns false? class CustomError extends Error {} const error = new CustomError(); console.log(error instanceof Error); // true console.log(error instanceof CustomError); ...