2016-03-17 17 views
5

Próbuję zaznaczyć wszystkie pola wyboru za pomocą jednego pola wyboru. Ale jak to zrobić?zaznacz wszystkie pola wyboru z kątowym JS

To jest mój 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> 

utworzyć dodatkowe pole do selecet i odznaczyć wszystkie pola wyboru.

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; 
       }); 
      }; 
}); 

Próbowałem to też, ale żaden z nich nie działa na mnie :(

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

może pomóc http://stackoverflow.com/questions/35914431/triggering-all-the-checkbox-event-while-selecting-checkall-in-angularjs/35914967#35914967 –

+0

Czy coś się dzieje, gdy funkcja 'usersetting (user)' jest wywoływana? być może kiedy sprawdzasz wszystkie pola wyboru coś w tej funkcji odrzuca żądanie i/lub odznacza pole? –

+1

To pytanie przypomina duplikat duplikatu. Link do @hadiJZ powinien służyć twojemu celowi. –

Odpowiedz

5

Jesteś brakowało div kontenerów z ng-controller i ng-app i angular.module.

user.select = $scope.selectAll jest prawidłowym wariantem.

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

+0

nie działa dla mnie:/Ale dzięki! :) Może jest jeszcze inny problem:/Cinkowałem mój kod w pasteBin ... app.js i HTML pastebin.com/7eJu14nd pastebin.com/tq10Gtjb Właśnie usunąłem skrypt dla apiomat – Paili

+1

@Paili 1. Odniesienia OnLoad nieistniejąca funkcja. 2. Twój kod js musi być * po * 'angular.js' w html (np. App.js, na przykład). Kod roboczy: http://pastebin.com/rMgmiRgW –

1

Oto JSFiddle można użyć ng-checked ze zmienną na nim jak user.checked (lub użyj user.select, jak chcesz)

<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; 
    }); 
    } 
}); 
0

Proste użyć następującego kodu

HTML

<input type="checkbox" ng-model="checkboxes.checked" data-ng-click="checkAll()" class="select-all" value="" /> 

JS

$scope.checkAll = function() { 
       angular.forEach($scope.users, function(item) { 
       $scope.checkboxes.items[item._id] =  $scope.checkboxes.checked; 
       $scope.selection.push(item._id); 
      }); 
       } 
1

Spróbuj ustawić zaznaczone pola przeciwko każdemu użytkownikowi należy sprawdzić, gdy górna wyboru jest sprawdzono:

<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> 
Powiązane problemy