Rajasekhar
Rajasekhar

Reputation: 2455

Select all checkbox checks except disabled checkboxes using Angular

Trying to Select all checkbox checking and need to restrict disabled checkboxes and check only enabled checkboxes, but all checkboxes are selecting(all enabled and disabled checkboxes are checking). Please suggest me,

checkuncheckall() {
    this.isChecked = !this.isChecked;
    this.displaying_results.forEach(i => {
     this.persons[i].checked = this.isChecked;
    });
  }

Sample Example

Upvotes: 1

Views: 1171

Answers (3)

Mohamed Enab
Mohamed Enab

Reputation: 139

First you will have to wrap all checkboxes with formarray and when you retrieve your data push for every single data form control inside formarray then bind the formcontrol with it then in the function of select all check if the form control is enable here is working example of what i mean. Don't forget to upvote.

Upvotes: 0

Shariq
Shariq

Reputation: 175

You have to just check for the checkboxes which are not disabled and perform check/uncheck only those checkboxes, like:

checkuncheckall() {
this.isChecked = !this.isChecked;
this.displaying_results.forEach(i => {
  if (this.persons[i].firstName !== 'Superman') {
    this.persons[i].checked = this.isChecked;
  }
});
}

Upvotes: 1

user776686
user776686

Reputation: 8655

To answer your question shortly - all you need to do is to test for the opposite condition as when you set the control to disabled:

this.displaying_results.forEach(i => {
  if(this.persons[i].firstName !== 'Superman'){
    this.persons[i].checked = this.isChecked;
  }
});

Upvotes: 1

Related Questions