如何获取选定的行数据AngularJS

时间:2016-03-07 19:34:07

标签: angularjs checkbox angularjs-ng-repeat

我试图从AngularJS中的表中获取选定的行,但无法弄清楚如何执行此操作。以下是看到我做了什么的傻瓜。

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

代码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  },
    ];  

    });

我希望用户在点击提交时从左列选中的行复选框。

非常感谢帮助。

2 个答案:

答案 0 :(得分:1)

检查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);
};

答案 1 :(得分:0)

您应该将此复选框绑定到模型,就像使用classA和classB一样。

例如:

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