AngularJS: Getting multiple values for same variable -
i doing project using mean stack , still learning parts of angularjs. have form filled user. contains series of checkboxes selects students , stores them in array. each students, intend take 2 values(correct answers, incorrect answers) student. how model these 2 inputs 2 variables , values in controller?
the problem if 1 time, select 3 students, there 3 pairs of correct , incorrect answers. if other time have different students selected, how map answers students?
view.html:
<label ng-repeat="studenta1 in studentsa1"> <input type="checkbox" ng-model="selectedstudentsa1" ng-checked="exista1(studenta1)" value="studenta1" ng-click="toggleselectiona1(studenta1)"> {{studenta1}} </label> {{selectedstudentsa1}} <div class="col-md-3"> <h6>correct answers</h6> <input type="number" min="0" name="correctanswers" ng-model="correctanswers" required> </div> <div class="col-md-3"> <h6>incorrect answers</h6> <input type="number" name="incorrectanswers" ng-model="incorrectanswers" min="0" required> </div> controller.js:
$scope.studentsa1 = ['akhilesh', 'prathamesh', 'mandar', 'sunmay']; $scope.selectedstudentsa1 = []; $scope.exista1 = function(item) { return $scope.selectedstudentsa1.indexof(item) > -1; }; $scope.toggleselectiona1 = function(item) { var idx = $scope.selectedstudentsa1.indexof(item); if (idx > -1) { $scope.selectedstudentsa1.splice(idx, 1); } else { $scope.selectedstudentsa1.push(item); } }; $scope.checkalla1 = function() { if ($scope.selectalla1) { angular.foreach($scope.studentsa1, function(item) { var idx = $scope.selectedstudentsa1.indexof(item); if(idx >= 0) { return true; } else { $scope.selectedstudentsa1.push(item); } }); } else { $scope.selectedstudentsa1 = []; } };
check if helps have made fiddle if looking more please comment them
in view side
<div ng-app ng-controller="maincontroller"> <table> <tr> <th> <input type="checkbox" ng-model="selectall" ng-click="selectallcheckbox(selectall)"> </th> <th>name</th> <th>correct</th> <th>wrong</th> </tr> <tr ng-repeat="studenta1 in studentsa1"> <td> <input type="checkbox" ng-model="studenta1.selected"> </td> <td> {{studenta1.name}}</td> <td> <input type="number" ng-model="studenta1.correct" /> </td> <td> <input type="number" ng-model="studenta1.wrong" /> </td>\ </tr> </table> <br> {{selectedstudentsa1}} <a ng-click="selectedstudents()">submit</a> </div> in controller side
function maincontroller($scope) { $scope.studentsa1 = [{ selected: false, name: 'akhilesh', correct: '', wrong: '' }, { selected: false, name: 'prathamesh', correct: '', wrong: '' }, { selected: false, name: 'mandar', correct: '', wrong: '' }, { selected: false, name: 'sunmay', correct: '', wrong: '' }]; $scope.selectedstudents = function() { $scope.selectedstudentsa1 = $scope.studentsa1.filter(i => i.selected == true); $scope.correctanswers = $scope.selectedstudentsa1.length; $scope.incorrectanswers = $scope.studentsa1.length - $scope.selectedstudentsa1.length; } $scope.selectallcheckbox = function(value) { $scope.studentsa1.foreach(function(item) { item.selected = value; }); } }
Comments
Post a Comment