I want to attach my status field with mat-checkbox and want to get the value in the form of a string as we use to get in Material-1.
I am looking for an alternative of ng-true-value="'ACTIVE'" ng-false-value="'INACTIVE'"
in Material-7.
I want to attach my status field with mat-checkbox and want to get the value in the form of a string as we use to get in Material-1.
I am looking for an alternative of ng-true-value="'ACTIVE'" ng-false-value="'INACTIVE'"
in Material-7.
You can implement this by using the value
property of MatCheckbox
and listening to changes. For example:
HTML
<mat-checkbox [value]="falseValue" (change)="checkboxChange($event.source, $event.checked)">
Check me!
</mat-checkbox>
TS
falseValue = 'No'
trueValue = 'Yes';
checkboxChange(checkbox: MatCheckbox, checked: boolean) {
checkbox.value = checked ? this.trueValue : this.falseValue;
}
This can be implemented as a directive for simple reusability:
TS
import {Directive, Input} from '@angular/core';
import {MatCheckbox} from '@angular/material/checkbox';
@Directive({
selector: 'mat-checkbox[checkboxValue]',
exportAs: 'checkboxValue'
})
export class CheckboxValueDirective {
@Input('checkboxValue') checkbox: MatCheckbox;
@Input() falseValue: string = '0';
@Input() trueValue: string = '1';
ngOnInit() {
this.checkbox.value = this.checkbox.checked ? this.trueValue : this.falseValue;
this.checkbox.registerOnChange((checked: boolean) => {
this.checkbox.value = checked ? this.trueValue : this.falseValue;
})
}
}
Usage:
<mat-checkbox #checkbox [checkboxValue]="checkbox" trueValue="Yes" falseValue="No" [checked]="true">
Check me!
</mat-checkbox>
Here is the directive example on StackBlitz: https://stackblitz.com/edit/angular-aapsr6?file=app/checkbox-value-directive.ts
I used an approach similar to the comment above. Saving the values โโof several checkboxes in an array called filtroStatus
filtroStatus: string[] = [];
checkboxChange(checkbox: MatCheckbox, checked: boolean, statusValue: string){
if (checked) {
this.filtroStatus.push(statusValue);
statusValue === 'em_analise' ? this.filtroStatus.push('pendente') : null;
statusValue === 'em_dia' ? this.filtroStatus.push('em_andamento') : null;
} else {
const index = this.filtroStatus.indexOf(statusValue);
const total_excluir = statusValue ==='em_analise' || 'em_dia' ? 2 : 1;
index >= 0 ? this.filtroStatus.splice(index, total_excluir) : null ;
}
}
For someone looking for a simple answer
TS:
import { Directive, Input } from '@angular/core';
import { ControlValueAccessor } from '@angular/forms';
import { MatCheckbox } from '@angular/material/checkbox';
@Directive({
selector: 'mat-checkbox[appCheckboxValue]'
})
export class CheckboxValueDirective implements ControlValueAccessor {
@Input() trueValue = true;
@Input() falseValue = false;
constructor(@Optional() @Self() private ngControl: NgControl, private checkbox: MatCheckbox) {
if (this.ngControl) {
this.ngControl.valueAccessor = this;
}
}
writeValue(value: any): void {
this.checkbox.writeValue(value === this.trueValue);
}
registerOnChange(fn: any): void {
this.checkbox.registerOnChange((checked: boolean) => {
fn(checked ? this.trueValue : this.falseValue);
});
}
registerOnTouched(fn: any): void {
this.checkbox.registerOnTouched(fn);
}
setDisabledState?(isDisabled: boolean): void {
this.checkbox.setDisabledState(isDisabled);
}
}
HTML:
<mat-checkbox appCheckboxValue trueValue="ACTIVE" falseValue="INACTIVE" [(ngModel)]="myCheck">
</mat-checkbox>