1

I'm trying to get selected rows from table in AngularJS, but not able to figure out how to do that. Below is the plunker to see what I did.

http://plnkr.co/edit/APb0MeK5th7z79l32SEy?p=preview

Code Html

 <body ng-controller="MainCtrl">

<table>
<tr><td>Select Column</td><td>id</td> <td>name</td> <td>address</td> <td>classA</td> <td>classB</td></tr>  
<tr ng-repeat="employee in employees">
    <td><input type="checkbox"></td>
    <td>{{employee.id}}</td>
    <td>{{employee.name}}</td>
    <td>{{employee.address}}</td>
    <td><input type="checkbox" ng-model="employee.classA"></td>
    <td><input type="checkbox" ng-model="employee.classB"></td>
</tr>
</table>
<input type="button" name="save" value="submit">

Code JS

 var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {




    $scope.employees = [
        { id:"1", name: "A",              address: "A1",    classA: true,  classB: true  },
        { id:"2", name: "B",            address: "A2",    classA: false, classB: true  },
        { id:"3", name: "C",            address: "A3",    classA: true, classB: false  },
        { id:"4", name: "D",             address: "A4",   classA: false, classB: true  },
        { id:"5", name: "E",             address: "A5",   classA: false, classB: true  },
    ];  

    });

I want the rows which are selected by user from left column check box while they click on submit.

Help is appreciated.

2 Answers 2

1

Check plunker here.

<td><input type="checkbox"
       ng-model="employee.checked"
       ng-true-value="1" 
       ng-false-value="0"></td>

...

<button ng-click="getSelected()">getSelected</button>

...

$scope.getSelected = function () {
  var ar = $scope.employees.filter(
    function (value) {
      if (value.checked == 1) {
        return true;
      } else {
        return false;
      }
    }
    );

  console.log(ar);
};
Sign up to request clarification or add additional context in comments.

Comments

0

You should bind this checkbox to a model, like you did with classA and classB.

for example:

<td><input type="checkbox" ng-model=employee.selected></td>

1 Comment

After binding a check box what should I do ? Do I need to loop it ? I want only selected employees like if I select 1 and 3 then.

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.