2

I need help in making the rows enable only when the checkbox is check. The default rows should be disabled but when the checkbox is only check, that row will be enabled. Here's the link to my code

LINK CODES

  patchValues() {
    let rows = this.myForm.get('rows') as FormArray;
    this.orders.forEach(material => {
      material.materials.forEach(x => {
      rows.push(this.fb.group({
        checkbox_value: [null],
        material_id:  new FormControl({value: x.id, disabled: true}, Validators.required),
        material_name: x.name, 
        quantity: [null, Validators.required]
      }))
    })
      })

  }

2 Answers 2

5

Have a look at the Demo & Src

Stack Blitz, Fork

Explanation:

  1. For enabling or disabling a input field, you need to use the following syntax [attr.disabled]="myForm.get('rows').value[i].checkbox_value ? null: ''"
  2. attr.disabled, here attr is for binding with properties which don't exist directly on an element
  3. myForm.get('rows').value[i].checkbox_value, is fairly straight forward access to a form control's value.
Sign up to request clarification or add additional context in comments.

10 Comments

Hi Abhijit Kar. I have putted a submit button and I need to save only those who have checkbox. So if a td has a checkbox, qty field should be inputted. Here's the forked link stackblitz.com/edit/…
@Joseph, have a look at the fork: stackblitz.com/edit/…. You just handle the submission in onCreateProduct() method.
I'm sorry. The button "save" should be enabled too. That's my problem.
@Joseph have a look now
Sorry I've to bother again. I saw one problem. When i click the checkbox and i input something in the quantity field and then uncheck the checkbox, i can still click the save button. It doesn't disabled.
|
2

Another aproach is create a directive

@Directive({
  selector: '[enabledControl]'
})
export class EnabledControlDirective {

    @Input() set enabledControl(condition: boolean) {
        if (this.ngControl) {
            if (this.ngControl.control) {
                if (condition)
                    this.ngControl.control.enable();
                else
                    this.ngControl.control.disable();
            }
        }
  }
  constructor(private ngControl : NgControl ) {
  }
}

Then you can use like

<input class="col-md-6" formControlName="quantity"
     [enabledControl]="row.get('checkbox_value').value" >

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.