Ng-Checked-2

 

In this example of Ng-Checked, We will have a grid with checkbox and we will delete the records for which the checkboxes are checked and also provided a checkbox that will check all the checkboxes and clicking on delete will delete the records. See the working example

 
 
 
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Welcome to LearnKode - A code learning platform</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> </head> <body ng-app="checkedExample"> <div ng-controller="ExampleController"> <div class="container"> <label>Delete All: <input type="checkbox" ng-model="master"></label><input type="button" class="btn btn-danger btn-xs" ng-click="delete(master)" value="Delete" /><br /> <table class="table table-hover"> <thead><tr><th>Id</th><th>First Name</th><th>Last Name</th><th>Action</th></tr></thead> <tbody><tr ng-repeat="p in names | orderBy:'fName'"><td>{{p.id}}</td><td>{{p.fName}}</td><td>{{p.lName}}</td><td><input type="checkbox" ng-checked="master" ng-model="isTrue" ng-change="getIndex(p.id,isTrue )" /></td></tr></tbody> </table> </div> </div> <script> var app = angular.module("checkedExample", []); app.controller('ExampleController', ['$scope', function ($scope) { var arr = []; $scope.names = [{ id: '1', fName: 'John', lName: 'NA' }, { id: '2', fName: 'Smith', lName: 'NA' }, { id: '3', fName: 'Allen', lName: 'NA' }, { id: '4', fName: 'Marry', lName: 'NA' }, { id: '5', fName: 'Jennifer', lName: 'NA' }]; $scope.getIndex = function (id, isTrue) { if (isTrue) arr.push(id); else { var index = arr.indexOf(id); arr.splice(index, 1); } }; $scope.delete = function (isMaster) { if (isMaster) { $scope.names = []; } else { for (var i = 0; i < arr.length; i++) { var rec = $scope.names.filter(function (p) { return p.id == arr[i] }); var idx = $scope.names.indexOf(rec[0]); $scope.names.splice(idx, 1); } }; arr = []; }; }]); </script> </body> </html>
Output