1

I am dynamically rendering the checkbox and combobox fields and performing following functionality:

  1. based on the API response, if the checkbox field appears as selected then combobox appears as enable else it would be disable.
  2. If user select checkbox field then combo box should be enable else disabled

The combo box fields works work for the first case but not working in the second case. The state (enable or disable) cannot changes later on.

Here is my template snippet:

 < tr *ngFor="let widgetAttribute of _preferencesWidget">

 < td *ngFor="let subattribute of widgetAttribute.methods">

 < select *ngIf="subattribute.name == 'Email' && subattribute.type == 'LIST'" id="{{subattribute.name}}Check" class="selectpicker" data-max-options="1" (change)="validateCombo($event)"
                                                            [disabled]="!subattribute.enabled">
                                                                <option *ngFor="let subValue of subattribute.values" value="{{subValue.code}}" [selected]="subValue.code == subattribute.selected">{{subValue.description}}< /option>
                                                            < /select>
 < /td>
< /tr>

The generated id for checkbox is : InformationCheck and for combobox is : pensionMCheck

My component logic is:

 public validateCombo() {

     if (this.pensionCheck) {
         $('#pensionMCheck').prop('disabled', false);
         if ($('#pensionMCheck').val() === 'Y') {
             this.pensionMCheck = true;
         } else {
             this.pensionMCheck = false;
         }
    } else {
         this.pensionMCheck = false;
         $('#pensionMCheck').val('N');
         $('#pensionMCheck').prop('disabled', true);
         $('#pensionMCheck').prop('selectedIndex', 0);
    }

1 Answer 1

1

Bind the checkbox status to a property (isChecked) and bind enabled of <select> to the same property:

<input type="checkbox" [(ngModel)]="isChecked">
<select [(ngModel)]="selectedItem" [enabled]="!isChecked"
Sign up to request clarification or add additional context in comments.

5 Comments

I am able to resolve it. Code is working fine the only problem is that there is duplicate element exists in the DOM tree.
What duplicate element? You mean multiple select elements? What problem does it cause?
No, the the ID that a element is getting dynamically is duplicated with another element.
Are you talking about id="{{subattribute.name}}Check"?
I don't know. If name is different for each `subattribute, then there shouldn't be duplicates.

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.