I am attempting to dynamically enable or disable a checkbox in Angular 8 based on a specific value

My code snippet goes here

I have a List Array called "ktsessions" which contains the following data: ktsessions = [ {"presenter":"Bharath","topic":"Angular","status":"scheduled","emailId":"[email protected]"}, {"presenter":"Sayyad","topic":"Angular","status":"organized","emailId":"[email protected]"},{"presenter":"Kanchan","topic":"APS","status":"scheduled","emailId":"[email protected]"} ];

<tr *ngFor = "let ktsession of ktsessions >

  <td ><input type="checkbox" [disabled]='disabled'></td>
</tr>

Here is the TypeScript code:

getktsession(){   
    this.service.getKtsession().subscribe(data =>{
      console.log('get', data);
      this.ktsessions = data;
      this.ktsessions.find(user => {
     
       if(user.presenter === "Kanchan"){
       this.disabled = true
} else {
this.disabled = false;
}
        
      });
    });
  }

Answer №1

There seem to be a few issues that need addressing.

  1. <tr *ngFor = "let ktsession of ktsessions >
    - The closing quote after ktsessions is missing.
  2. <td ><input type="checkbox" [disabled]='disabled'></td>
    - Instead of [disabled]='disabled', it should be
    [disabled]="ktsession.disabled"
    . Each checkbox instance needs its own disabled property. Using [disabled]='disabled' sets all checkboxes with the disabled property of the component class instance, not individually.
  3. this.ktsessions.find(user => {
    - While Array#find technically works here, Array#forEach is more appropriate for iterating over an array and setting properties on each element.
  4. this.disabled = true and this.disabled = false - Setting the disabled property directly on the component class instance is incorrect. It should be set on each user instance: user.disabled = true or user.disabled = false.

To correct these issues, your template should resemble:

<tr *ngFor="let ktsession of ktsessions">
  <td>
    <input type="checkbox" [disabled]="ktsession.disabled" /> <!-- use "ktsession.disabled" instead of "disabled" -->
    {{ ktsession.presenter }}
  </td>
</tr>

Your subscribe function should be adjusted as follows:

this.getKtsession().subscribe((data) => {
  this.ktsessions = data;
  this.ktsessions.forEach((user) => { // Use forEach instead of find
    if (user.presenter === 'Kanchan') {
      user.disabled = true; // Set user.disabled, not this.disabled
    } else {
      user.disabled = false; // Set user.disabled, not this.disabled
    }
  });
});

See a working example with these changes in this StackBlitz.

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

Exploring THREE.js with VIVE Headset (JavaScript/Browser/WebGL in the Steam Universe)

Can you use THREE.js JavaScript to create experiences for the VIVE headset? Is it feasible to display a Chrome web browser in fullscreen mode on the VIVE? One challenge I foresee is tapping into the data from the controller paddles. How can this be achie ...

What is the best way to dynamically add a stylesheet using JavaScript/jQuery?

I've been scouring the web for a solution to a particular issue, but so far I'm coming up empty-handed. We're working with Umbraco CMS for a client's website, and it seems we can't insert conditional comments in the <head> se ...

Refreshing one Partial View Using Another Partial View in TypeScript and angular.js

Looking to update a partial view from another using Angular.js, TypeScript, and ASP.Net MVC3. One partial view contains a button and label, while the other has just a label. Clicking the button should display a value in the label of the same partial view, ...

What could be the reason for the Chrome Javascript console not displaying the function console.log output?

Just starting out with learning javascript and trying to complete a basic exercise from an online course. However, I'm noticing that my console is not displaying the expected output when running the fizzBuzz function. Any insights on why this might be ...

What is causing the unexpected behavior of deferred.resolve in the q manual?

I can't seem to grasp this concept and it might be a silly question. Let's analyze the code snippet below: function throwError() { throw Error("can't touch this."); } var def = q.defer(); def.promise.then( function() { co ...

"Error in Node.js and Express: The variable 'next' has not been defined

I encountered the Error: next is not defined message, but I am unsure of where in my codebase this error is originating from. https://i.sstatic.net/29ZPT.png Here is a snippet of my code: server.js // Server configuration and setup details are here // P ...

Is your Angular Bootstrap Modal component failing to function properly?

I've been attempting to implement Modals from Bootstrap for Angular, however, I'm encountering some issues. Here is the link to the documentation: I followed the instructions and copied the code for the 'Global configuration of modals' ...

What should be done if client.sendRequest(req, cursor).then((answer) does not return a response?

I am working with a VS Code client that sends requests to a server using the following code: client.sendRequest(req, cursor).then((answer)=> { processing... }) If for some valid reason the server does not send any response, what will happen? Is there ...

Having trouble deciphering the request header value within the REST web API

Utilizing Ajax, I am storing the token in the request header and sending it to a Rest API. Here is the request sent to the web API: var xhr = new XMLHttpRequest(); $.ajax({ url: 'http://localhost:32253/api/UserDetail/Authenticate', head ...

Encountering difficulties connecting to a local PostgreSQL database hosted on Docker while using TypeORM alongside pgAdmin4

I am encountering issues with connecting to a postgres database hosted in docker on localhost using the default port 5432. Both pgAdmin4 and TypeORM are giving me an "invalid password" error when attempting to connect. Here is the content of my docker-comp ...

Merge the JSON data with the Node.js/Express.js response

Whenever I input somedomain.com/some_api_url?_var1=1 in a browser, the response that I receive is {"1":"descriptive string"}. In this JSON response, the index 1 can vary from 1 to n, and the "descriptive string" summarizes what the index represents. I am ...

Determine the size of a file in either megabytes or kiloby

I need to determine the size of a file in either megabytes if the value is greater than 1024 or kilobytes if less than 1024. $(document).ready(function() { $('input[type="file"]').change(function(event) { var _size = this.files[0].si ...

Instructions on utilizing interpolation to transfer a value as an argument to a function

What is the correct way to insert the {{c.id}} argument into the function instead of hardcoding it as 32? <tr *ngFor="let c of myService.companyList"> <td>{{c.name}}</td> <td>{{c.email}}</td> <td>{{c.pass ...

Issue with Prettier AutoFormatting in a project that combines TypeScript and JavaScript codebases

Recently, I've started incorporating TypeScript into an existing JavaScript project. The project is quite large, so I've decided to transition it to TypeScript gradually. Below is a snippet from my eslintrc.js file: module.exports = { parser: ...

How to showcase numerous PDF documents within a ReactJS application

As a newcomer to the world of React, I am facing an issue with displaying multiple PDFs based on the selected link. Although I have been successful in displaying a PDF from a list of links pointing to my stored PDFs within the src directory, I encounter th ...

The AJAX POST form consistently submits data from the initial entry in the list

Upon investigation, I have discovered that the issue is linked to my AJAX posting method. When I submit the form using the standard method (action=""), it successfully populates my database based on the selected record from the list. However, when I util ...

The animation came to a halt after a brief period

Here is the code I wrote. When the button is clicked, the 3 containers should start flashing indefinitely, but they stop after a while. I can't figure out why. Any ideas? <!DOCTYPE html> <html> <head> <title></title> & ...

Efficient method of delivering cohesive information from database to user without the need for continuous querying

Within the database, there is stored data about each user that typically remains constant. However, occasionally a user may update their information, such as changing their name. This data includes the user's name, username, and company details. The ...

Encountering a blank or 404 error page in React after running npm build - let's troubleshoot where the issue might be

Upon running npm build(react), my index.html displays a blank page. To address this issue, I initially attempted the following steps: Adding the following line in package.json homepage : "./" or "." or "absolute file PATH" ...

Error occurs in Azure Function Linux Nodejs when trying to use @azure/storage-blob as it cannot read the property 'startsWith' of undefined

While testing my Azure Nodejs Linux function locally, I encountered this issue with the code snippet below: import { BlobServiceClient } from "@azure/storage-blob"; const connectionString = process.env[ "AZURE_STORAGE_CONNECTION_STRING&qu ...