If I deselect an item, the "check all" option does not become deselected

I recently designed a page that displays all the weekdays and dates from Monday to Sunday. I added a feature where users can check all the weekdays with a single click, and uncheck them with another click. However, I encountered an issue where unchecking a specific weekday, such as Monday or Tuesday, doesn't automatically uncheck the "check all" box. Below is a snippet of the HTML code:

<div class=" row ">    
    <label class="custom-control custom-checkbox mb-2 mr-sm-2 mb-sm-0 warning">
    <input type="checkbox" id="rem"  class="custom-control-input" [checked]="checkAll" (change)="onChangeCheckAll($event.target.checked)">
    <span class="custom-control-indicator"></span>
    <span class="custom-control-description"></span>
    Select All
    </label>
</div>
... (more HTML code omitted for brevity)

After identifying the issue, I delved into the JavaScript code to find a solution:

// Function to handle checking or unchecking a specific weekday
onChangeCheckWeek(week: any, isChecked: boolean) {
    if (isChecked) {
      this.checkAll = false; // Uncheck the "Select All" box
    } else {
      this.checkAll = false;
    }
  }

// Function to handle checking or unchecking all weekdays
onChangeCheckAll(isChecked: boolean) {
    if (isChecked) {
      this.checkAllWeek = true; // Check all weekdays

    } else {
      this.checkAll = false;
      this.checkAllWeek = false; // Uncheck all weekdays

    }
  }

Answer №1

According to your reasoning, there is no need to validate any conditions in the onChangeCheckWeek method.

onChangeCheckWeek(week: any, isChecked: boolean) {
         this.checkAll = false; //           }

The Issue

It seems that you have neglected to include a single quote within the parameter value of the onChangeCheckWeek function. Due to this, the event fails to trigger.

Resolution:

Ensure that the onChangeCheckWeek event is structured as follows:

(change)="onChangeCheckWeek('Wednesday', $event.target.checked)"
instead of
(change)="onChangeCheckWeek(Wednesday, $event.target.checked)"
. You must include the single quote within 'Wednesday'.

Answer №2

Make your life simpler

<input #checkAll type="checkbox" class="custom-control-input" (change)="getStatus()" (click)="handleCheckAll()">

Within your TypeScript file

@ViewChild('checkAll') checkAll: ElementRef;
checkedDays: string[] = [];
days = ['Monday', 'Tuesday', 'You get the idea'];

getStatus() {
  let el = this.checkAll.nativeElement;
  if (this.checkedDays.length === this.days.length) { el.checked = true; }
  else if (!this.checkedDays.length) { el.checked = false; }
  else { el.indeterminate = true; }
}

handleCheckAll() {
  if (this.checkedDays.length === this.days.length) { this.checkedDays = []; }
  else if (!this.checkedDays.length) { this.checkedDays = this.days; }
  else { /* Decide what to do when 1 to 6 days are checked */}
}

All you have to do now is add the clicked day to the checkedDays array, and you're all set.

Answer №3

Hey there! A big thank you to all who offered help, but I managed to figure it out on my own. I implemented [(ngModel)]=selectAllWeekDays to select all the weekdays, and in the TypeScript file, I set this.selectAllWeekDays=false in the else statement. Take a look at the HTML snippet below:

 <div class=" row ">    
 <label class="custom-control custom-checkbox mb-2 mr-sm-2 mb-sm-0 warning">
 <input type="checkbox" id="rem"  class="custom-control-input" 
 [checked]="checkAll" (change)="onChangeCheckAll($event.target.checked)" 
 [(ngModel)]="selectAllWeekDays">
 <span class="custom-control-indicator"></span>
 <span class="custom-control-description"></span>
 Select All 
 </label>
 </div>

Below is the TypeScript snippet for reference:

onChangeCheckWeek(week:any ,  isChecked: boolean) {

if (isChecked) {
  this.checkAll = false;
} else {
  this.checkAll = false;
  this.selectAllWeekDays= false;
}

}

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

Error when attempting to open and close a div through a click event

Currently, I am in the process of developing a web service that generates arrays. I am facing an issue where all the div elements on my page load open by default, but I want them to remain closed and only open upon clicking. Specifically, I want a nested d ...

Angular module cannot be located

While working on implementing a chat feature for my project using sockJS and STOMP, I encountered several challenges with installing these two libraries. Despite attempting various methods such as installation from index.html, npm install, and manual downl ...

The parameter type SetStateAction<MemberEntityVM[]> cannot be assigned the argument type Promise<MemberEntityVM[]> in this context

I am looking to display a filtered list of GitHub members based on their organization (e.g., Microsoft employees). Implementing React + TS for this purpose, I have defined an API Model which represents the structure of the JSON data from the GitHub API: ex ...

Strategies for Mitigating Duplicate Requests in AngularJs with just one click

I am facing an issue with my AngularJS application where clicking on a link triggers the API call twice. I'm not sure why this happens and how to fix it. My service: SomethingService function getData() { return apiSettings.getApiInformation().t ...

Steps to update XmlHttpRequest URL during image upload

I am currently facing an issue with updating images on my website. When I try to update an image, it redirects to the wrong URL instead of the intended one. The form is set to post data to this URL: POST http://127.0.0.1/mgt/upload/processImage/foodImage ...

Switch the functionality of a Google Chrome extension back and forth numerous times

My Google Chrome extension works by attaching event listeners to all elements on a page and inserting CSS (lol.js and style.css) when clicked. However, I am facing an issue where I cannot remove the JS and CSS upon clicking the icon again. Right now, I am ...

Cross-Origin Request Blocked in Browser (Angular Front-end, Quarkus Back-end)

application.properties #WEB quarkus.tls.trust-all=true quarkus.http.cors=true Controller @POST @Path("/result") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Operation(summary = "Display Ev ...

Using ngFor to connect input with the Algolia Places feature

I have implemented an Algolia Places input within an ngFor loop using Angular8. The issue I am facing is that the (change) event only works properly after typing in the input for the second time. While this is functional, it's not exactly the behavior ...

JavaScript regular expression for detecting valid currency values

I am encountering an issue with removing decimal values from a monetary amount retrieved from a JSON feed: For example, if I have: 150,000, I want to display it as just 150 Here is the code snippet I am using: $.getJSON('/static/js/datatest.json&ap ...

Is there a way for me to extract an image from Open Flash Chart 2?

After attempting to follow this tutorial, I discovered that it was not functioning properly. Although my server side is operating correctly, I encountered an issue in Firefox when trying to access the instance of ofc and call post_image. The error messag ...

PHP is unable to render Google Maps on the webpage

My PHP code retrieves location information from a database (test) and table named manu, created using phpmyadmin in Wamp. It then displays those locations on a map with markers, showing latitude and longitude values. UPDATED <? $dbname =&ap ...

The type 'Promise<any>' cannot be assigned to the type 'Contact[]'

After exhausting all my resources on the internet and StackOverflow, I still couldn't find the right answer. This is my first project in Angular with MongoDB, but I keep encountering this error: "Type 'Promise' is not assignable to type &apo ...

Troubleshooting a 400 Bad Request Error in jQuery Ajax for WordPress Widgets

I am attempting to send information to admin-ajax.php in order to save it as a $_POST variable for handling on the cart page. However, my .ajax function keeps failing. var sendJsonA = {'value':'Data coming from JSON'} var ajaxurl = $ ...

How can I rename attribute values in a dropdown menu and ensure they work properly?

I'm facing an issue with my code. How can I store the option values in a database when they have numbering like value="1" or value="2"? Can I change it to something like value="1000" and have the other select box change to value="250" when selected? ...

Redux - a method of updating state through actions

Hello, I am currently working on developing a lottery system and I have a question regarding the best approach to modify state using action payloads. Let's consider the initial state: type initialCartState = { productsFromPreviousSession: Product[ ...

Using AngularJS, passing a value from outside a directive to the directive and detecting changes in the

As a newcomer to AngularJs, I am facing a challenge in retrieving data from outside a directive. The scenario involves multiple input fields being updated and the necessity for the directive to process this information. For instance, consider the code sni ...

AngularJS: Updating data in controller but not reflecting in the HTML view

Within my AngularJS app, I've come across the following code: example.html <div> <p>{{name}}</p> </div> <div> <button ng-click="someFunction()"></button> </div> exa ...

Is there a way to determine if the current path in React Router Dom v6 matches a specific pattern?

I have the following paths: export const ACCOUNT_PORTAL_PATHS = [ 'home/*', 'my-care/*', 'chats/*', 'profile/*', 'programs/*', 'completion/*', ] If the cur ...

Is there a way to determine if a table cell contains overflowing text compared to another cell?

https://i.sstatic.net/IYafA.png Is there a way to detect if the content of the fourth td overflows from the second td? I am looking for a method to determine which td has overflowed text and identify which td's text is overflowing. What approach ca ...

How can AngularJS achieve ng-repeat functionality with multiple variables similar to ng-init?

When using ng-init, you have the ability to utilize multiple variables: ng-init="var1=value1; var2=value2" I attempted something similar with ng-repeat but unfortunately it did not work as expected ng-repeat= "var1 in var1s; var2 in var2s" Is there a p ...