0

I have table with input=text fields, and when some of fields has id 'four' I need to disable this whole row. For this I try to use this function:

  $scope.disableIfRowHasFour = function(row){
    var result = row.filter(function(field){
      return field.id === 'four';
    })

    return result.length !== 0;
  };

and in html:

<tr ng-disabled='disableIfRowHasFour(row)' ng-repeat='row in tableFields'>
  <td ng-repeat='field in row'>
    <input type="text" value='{{field.value}}'>
  </td>
</tr>

and in devtools I see that row is disabled, but inputs in this row - not.

<tr ng-disabled="disableIfRowHasFour(row)" ng-repeat="row in tableFields" class="ng-scope" disabled="disabled">

where I'm wrong? I expect that all items in a row will be disabled..Plnkr example

5
  • 1
    You should add the ng-disabled tag on the input instead of the tr Commented Dec 6, 2016 at 17:29
  • yes, but how can I disabled all fields in a row, if id=four has only 1 input? Commented Dec 6, 2016 at 17:30
  • 1
    add ng-disabled='disableIfRowHasFour(row)' on the input. It should work Commented Dec 6, 2016 at 17:31
  • oh!)) you right) thx a lot Commented Dec 6, 2016 at 17:32
  • Look my answer, you can use ng-init to prevent multiple calls. Commented Dec 6, 2016 at 17:33

1 Answer 1

1

In the tr tag use ng-init to initialize a variable with disabled value:

<tr ng-init='rowDisabled=disableIfRowHasFour(row)' ng-repeat='row in tableFields'>

And in the inputs you add ng-disabled:

<input type="text" value='{{field.value}}' ng-disabled="rowDisabled">
Sign up to request clarification or add additional context in comments.

4 Comments

I think this wouldn't work because rowDisabled would be a variable shared among all the rows and therefore it would be true if the last one has four and false if the last one has something different than four. But, I'm not sure about the scope of the variable.
each element in ng-repeat has its own scope, hence it will work
yes, it works too, thx! is this faster then "ng-disabled='disableIfRowHasFour(row)' on the input"? or it does no matter?
Storing the value in a variable prevent you to invoke the disableIfRowHasFour function on each input, saving processing.

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.