json - angularjs checkbox append to scope variable -
i have table follows:
<tr ng-repeat="user in users"> <td>{{user.fields.first_name}}, {{ user.fields.last_name }}</td> <td> <input type="checkbox"> </td> </tr>
and json follows:
{ "users": [ { "fields": {"first_name": "sam", "last_name": "smith"}, "model": "auth.user", "pk": 3}, { "fields": {"first_name": "tom", "last_name": "moody"}, "model": "auth.user", "pk": 4} ] }
i want have functionality add pk's
of selected checkboxes scope variable. since don't have boolean field selected in json, how approach this?
or can try ng-change, bit more elegant imo:
var app = angular.module('app', []); app.controller('acontroller', function($scope) { $scope.users = [ { "fields": {"first_name": "wasif", "last_name": "abbas"}, "model": "auth.user", "pk": 3}, { "fields": {"first_name": "asad", "last_name": "zaman"}, "model": "auth.user", "pk": 4} ]; $scope.selectedusers = {}; $scope.adduser = function(pk, userselected) { if(userselected) { $scope.selectedusers[pk] = true; } else { delete $scope.selectedusers[pk]; } } $scope.getselectedusersarray = function() { return object.keys($scope.selectedusers); } });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app"> <div ng-controller="acontroller"> <table> <tr ng-repeat="user in users"> <td>{{user.fields.first_name}}, {{ user.fields.last_name }}</td> <td> <input type="checkbox" ng-model="userselected" ng-change="adduser(user.pk, userselected)"/> </td> </tr> </table> <div>selected: {{getselectedusersarray()}}</div> </div> </div>
Comments
Post a Comment