0

I have 'n' number of rows which has textbox and select box. I am trying to disable a textbox when i change the value of a select box in the same row using angular 2.

My code is below:

<table>
  <tr>
    <td><input type="text" (disabled)="isDisabled(data.rowIndex)"</td>
    <td> <select (onValueChanged)="test(data.rowIndex)">
           <option>Yes</option>
           <option>No</option>
    </td>
   </tr>
   <tr>
    <td><input type="text" (disabled)="isDisabled(data.rowIndex)"</td>
    <td> <select (onValueChanged)="test(data.rowIndex)">
           <option>Yes</option>
           <option>No</option>
    </td>
   </tr>
   .
   .
   .
</table>

component.ts

tabNumber: number = -1;

test(valueID){
        this.tabNumber = valueID;
}

isDisabled(num){
        return this.tabNumber === num;
    }

This above line disables only one textbox which i selected recently and enables other textboxes. Thus, my code could enable/disable only one textbox at a time. But i want to disable multiple boxes based on my selection. can anybody help?

2 Answers 2

1

see it in action on : plunker

html :

<table>
   <tr *ngFor="let item of items; let i = index">
    <td><input type="text" [disabled]="isDisabled(i)"/></td>
    <td> <select (change)="toggle($event,i)">
           <option value="yes">Yes</option>
           <option value="no">No</option>
          </select>
    </td>
   </tr>

</table>

component:

export class AppComponent {
   items = [{value : "hi"},{value : "hi"},{value : "hi"},{value : "hi"}]
   rows: number[] = [];

  isDisabled(index) {
    return this.rows[index] === -1;
  }

  enable(index){
    this.rows[index] = 1;
  }

  toggle($event,index){
    let selectElement = $event.target; 
    let value = selectElement.options[selectElement.selectedIndex].value;
    if(value === "yes"){
       this.enable(index);
    }else{
       this.disable(index);
    }
  }

  disable(index){
    this.rows[index] = -1;
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I think, there is a pbm in [disabled]="isDisabled()". This function is keep getting called. So it checks for tabNumber===Num. That is why other fields are getting enabled except the current selected one. Any idea about this issue.
the problem is that there one variable : tabNumber ang it's getting updated by all select tags you need an array here to keep track of each select tag
Can you please provide syntax for that.
@GreenComputers see the update there is a plunker link for you.
you are welome :)
0

For disabling multiple controls inside a parent element, you might consider this solution (custom directive): angular material disable controls using fieldset

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.