select all checkboxes with angular JS

21,732

Solution 1

You are missed the container divs with ng-controller and ng-app and angular.module.

user.select = $scope.selectAll is the correct variant.

https://jsfiddle.net/0vb4gapj/1/

Solution 2

Try setting the checked boxes against each user to be checked when the top checkbox is checked:

<input type="checkbox" ng-model="selectAll"/>    

<table class="table">
  <tr>
      <th>User ID</th>
      <th>User Name</th>
      <th>Select</th>    
 </tr>
 <tr ng-repeat="user in users | filter:search">
    <td>{{user.id}}</td>
    <td>{{user.name}}</td>
    <td><input type="checkbox" ng-click="usersetting(user)" ng-model="user.select" ng-checked="selectAll"></td>
    <td><tt>{{user.select}}</tt><br/></td>
 </tr>
</table>

Solution 3

Here is a JSFiddle you can use ng-checked with a variable on it like user.checked (or use the user.select as you want)

<div ng-app="app" ng-controller="dummy">
  <table>
    <tr ng-repeat="user in users">
      <td>{{user.id}}</td>
      <td>{{user.name}}</td>
      <td>
        <input type="checkbox" ng-click="usersetting(user)" ng-checked="user.checked" ng-model="user.select">
      </td>
      <td><tt>{{user.select}}</tt>
        <br/>
      </td>
    </tr>
  </table>
  <button ng-click="checkAll()">Check all</button>
</div>

JS:

var app = angular.module("app", []);
app.controller('dummy', function($scope) {
  $scope.users = [{
    id: 1,
    name: "Hello",
    select: 1
  }, {
    id: 2,
    name: "World",
    select: 1
  }, ];
  $scope.checkAll = function() {
    angular.forEach($scope.users, function(user) {
      user.checked = 1;
    });
  }
});
Share:
21,732
Paili
Author by

Paili

Updated on December 06, 2020

Comments

  • Paili
    Paili over 3 years

    I am trying to select all checkboxes with one single checkbox. But how to do that?

    This is my HTML:

        <input type="checkbox" ng-model="selectAll" ng-click="checkAll()" />
    
    <!-- userlist --> 
        <!--<div id="scrollArea" ng-controller="ScrollController">-->
        <table class="table">
          <tr>
              <th>User ID</th>
              <th>User Name</th>
              <th>Select</th>    
         </tr>
         <tr ng-repeat="user in users | filter:search">
            <td>{{user.id}}</td>
            <td>{{user.name}}</td>
            <td><input type="checkbox" ng-click="usersetting(user)" ng-model="user.select"></td>
            <td><tt>{{user.select}}</tt><br/></td>
         </tr>
        </table>
    

    I create an extra checkbox to selecet and unselect all checkboxes.

    JS:

    .controller('UsersCtrl', function($scope, $http){
        $http.get('users.json').then(function(usersResponse) {
          $scope.users = usersResponse.data;
        });
    
          $scope.checkAll = function () {
                angular.forEach($scope.users, function (user) {
                    user.select = true;
                    });
                };  
     });
    

    I tried this too, but none of them works for me :(

      $scope.checkAll = function () {
            angular.forEach($scope.users, function (user) {
                user.select = $scope.selectAll;
            });
        };