I'm new to Angular and Angular Material, now I'm working as a support in some project. There's a grid with filters and one checkbox, which checks if user in grid is active, inactive, or not chosen. It would be simpler with only two options (active, inactive) but well, I have to make 3 states for it:
- 1st click - Checked for active
- 2nd click - Unchecked for inactive
- 3rd click - Indeterminate for not chosen
Here is checkbox example from official Angular Material documentation: https://stackblitz.com/angular/rxdmnbxmkgk?file=app%2Fcheckbox-configurable-example.html
How to make it in the most simply way?
One way to do this is to set MAT_CHECKBOX_CLICK_ACTION to 'noop' and then you'll have to set the checked values with (click). Don't forget to bind both [ngModel] and [indeterminate].
Have a look at this: https://github.com/angular/material2/blob/master/src/lib/checkbox/checkbox.md
I tried to reply this Answer but I don't have enough reputation. First thank you for the answer and deep explanation. I was having trouble with the edition of the checkboxes and I figured out that if the value is false this line
doesn't work and the color is not updated either. I changed it for this one
I hope this comment help others.
If you need a working example you can also clone the material2 project here, and then:
Open the demo app and navigate to the checkbox component.
@angular/material >= 9
Here is a ready-to-use component:
and template:
Usage:
You can also override default tape to e.g. enum values:
where
customTape
replaces default values[null, true, false]
@angular/material <= 8
Just change
to the deprecated since version 9
MAT_CHECKBOX_CLICK_ACTION