0

Im new to angular and Im trying to push the certain selected checkbox from table and update into new array

<tr ng-repeat="val in values ">
      <td ng-bind="$index"></td>
      <td ng-bind="val.rec">ED1500322</td> 
      <td>working</td>
      <td ng-bind="val.result">I am going to School</td>
      <td>
       <div class="radio">
        <input ng-model="val.iscorrect" value="yes" type="radio">
        <label for="opt1">yes</label>
        <input ng-model="val.iscorrect" value="no" type="radio">
        <label for="opt10">no</label>
       </div>
      </td>
    </tr>

As we can observe that are checkbox present, when ever the checkbox is selected, I wanted to push it into new array

I wanted new array to have only selected checkboxes

I know this can be achieved by index by not sure how to do it

Plunker:http://plnkr.co/edit/NGnUwudclnOsnO6Y9Vww?p=preview

Any help in getting only selected checkbox is appreciated

4 Answers 4

1

You can piggyback on ng-change. It would look something like this

javascript

$scope.DoChange = function(val) { array.push(val)};

html

<input ng-model="val.iscorrect" value="yes" type="radio" ng-change="DoChange(val)">

I updated the plunkr

<!DOCTYPE html>
<html>

<head>
  <script data-require="[email protected]" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body ng-app="app" ng-controller="ctrl">
  <table class="table table-bordered dashboard_table">
    <thead>
      <tr>
        <th>Sl No</th>
        <th>Recording ID</th>
        <th>Recording
          <br> Audio File</th>
        <th>Speech Recognizer
          <br> Output text</th>
        <th>100% Correct
          <br>- Y(1) / N(0)?</th>
      </tr>
    </thead>
    <tbody>
        <tr ng-repeat="val in values ">
          <td ng-bind="$index"></td>
          <td ng-bind="val.rec">ED1500322</td> 
          <td>working</td>
          <td ng-bind="val.result">I am going to School</td>
          <td>
           <div class="radio">
            <input ng-model="val.iscorrect" value="yes" type="radio">
            <label for="opt1">yes</label>
            <input ng-model="val.iscorrect" value="no" type="radio" ng-change="log(val)">
            <label for="opt10">no</label>
           </div>
          </td>
        </tr>

    </tbody>
  </table>
  <button ng-click=getResult()>getResult</button>
  <script>
    var ngApp = angular.module('app', []);
    ngApp.controller('ctrl', function($scope) {
       $scope.log = function(v){console.log(v);}
      $scope.values = [{
        name: "John",
        rec:234,
        iscorrect: ''
      }, {
        name: "Paul",
        rec:44,
        iscorrect: ''
      }, {
        name: "George",
        rec:2664,
        iscorrect: 'no'
      }, {
        name: "Ringo",
        rec:124,
        iscorrect: 'yes'
      }];

      $scope.result=[]
       $scope.getResult=function(){
        scope.result=[]
       }

    });



  </script>
</body>

</html>
Sign up to request clarification or add additional context in comments.

Comments

1

If you're looking to push all the selected items into $scope.results. The modified code below taken from your plunker will work:

 var ngApp = angular.module('app', []);
 ngApp.controller('ctrl', function($scope) {

     $scope.values = [{
         name: "John",
         rec: 234,
         iscorrect: ''
     }, {
         name: "Paul",
         rec: 44,
         iscorrect: ''
     }, {
         name: "George",
         rec: 2664,
         iscorrect: 'no'
     }, {
         name: "Ringo",
         rec: 124,
         iscorrect: 'yes'
     }];

     $scope.result = []
     $scope.getResult = function() {
         $scope.result = $scope.values.filter(function(res) {
             return res.iscorrect.length > 0;
         })
     }

 });

Comments

1

Here you go this should do the trick Only change this.

<tr ng-repeat="(ind,val) in values track by $index">
    <td ng-bind="$index"></td>
    <td ng-bind="val.rec">ED1500322</td>
    <td>working</td>
    <td ng-bind="val.result">I am going to School</td>
    <td>
      <div class="radio">
        <input ng-model="values[ind].iscorrect" value="yes" type="radio">
        <label for="opt1">yes</label>
        <input ng-model="values[ind].iscorrect" value="no" type="radio">
        <label for="opt10">no</label>
      </div>
    </td>   
</tr>

Plunk Here!

Comments

1

By default checkbox / radio buttons return true or false .

So this means you can just add a ng-click directive on the radio button and pass through your ng-model with $index and any other data you wish to store .

Example

 <input ng-model="val.iscorrect" data-value="no" type="radio" ng-click="pushValue($index,val.iscorrect)">

FYI i would assume data-value="no" is connected to false so no need to pass this..

Hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.