1

I am trying to use a function to check all checkboxes in a tree of checkboxes. It is successfully checking and unchecking, BUT if I select a single checkbox then press check all, and uncheck, the single checkbox stays as it is as freeze

HTML

<fieldset id="field3">
  <table>
    <tr ng-repeat="e in empdepts | groupBy:'dep_LDesc'">
      <td>
        <label ng-click="showContent = !showContent"></label>
        <details ng-open="showContent">
          <summary>
            <input type="checkbox" ng-model="chk" /> {{e[0].dep_LDesc}}</summary>
          <div ng-repeat="employee in e">
            <input type="checkbox" ng-checked="chk"> {{employee.Sname}}
          </div>
        </details>
      </td>
    </tr>
  </table>
  <hr />
  <table>
    <tr>
      <td>
        <input type="checkbox" ng-model="all" ng-click="selectall(all)" /> All Departments</td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" ng-model="self" /> Self Services </td>
    </tr>
  </table>
</fieldset>

Controller

$scope.selectall = function (all) {
    if (all) {
        $scope.chk = true;
    }
    else
    {
        $scope.chk = false;
    }
}

LoadEmpDepts();

function LoadEmpDepts() {
    Assignments.getempdepts().then(function (response) {
        $scope.empdepts = (response.data);
        console.log($scope.empdepts);
    })
}

enter image description here

5
  • use $timeout helps your problem Commented Nov 8, 2017 at 19:33
  • Could you be missing the quotes around all? ng-click="selectall('all')" and don't forget to change if(all) to if('all'). Commented Nov 8, 2017 at 21:13
  • I tried to use the $timeout but nothing is changed can you show me how to do it in my example Commented Nov 9, 2017 at 7:51
  • I tried to put single quote and it successfully checked all but fails to uncheck all Commented Nov 9, 2017 at 7:52
  • is there is a way to give priority to check-all than single check, thats all what i need, is to overwrite Commented Nov 9, 2017 at 10:41

1 Answer 1

1

You need to have the checked property for individual elements, so the option would be to have a checked property inside each element of the array.

so whenever checkbox is checked update the property. and when checking all you make all elements checked property to be true.

  <label for="{{all.type}}"  ng-if="x.type === 'ALL'" >{{x.type}}</label>

example,

DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

is there is a way to give priority to check-all than single check, thats all what i need, is to overwrite

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.