![](/img/trans.png)
[英]how to get row data of a table when input check box will be selected using angular.js
[英]How to get table row data using check box in Angular.js
我需要一个帮助,我需要使用Angular.js从表行中获取所有检查的数据。 我在下面解释我的代码。
<tr ng-repeat="gl in galleryDatas">
<td><input type="checkbox" name=""> {{$index+1}}</td>
<td><img ng-src="upload/{{gl.image}}" border="0" name="image" style="width:100px; height:100px;" /></td>
<td>{{gl.description}}</td>
</tr>
在这里我需要用户单击复选框,相应的行数据将检索到数组中。请帮助我。
在复选框上使用ng-change
<td><input type="checkbox" ng-change="clickedRow(gl.checked)" name="" ng-model="gl.checked"></td>
在控制器中
$scope.clickedRow = function(checked) {
if (checked) {
//add to the list
} else {
// remove from list
}
}
你可以通过这个过程来实现
在您的控制器中:
$scope.galleryDatas = [
{
name: 'something',
description: "name 1"
},
{
name: 'another',
description: "name 2"
}
];
$scope.checkedValue = [];
$scope.saveInArray = function(index) {
if($scope.galleryDatas[index].checked) {
$scope.checkedValue.push($scope.galleryDatas[index]);
} else {
var newIndex = $scope.checkedValue.map(function(e) { return e.name; }).indexOf($scope.galleryDatas[index].name);
// for compare instead of "name" you should use that field is unique. ie: e.uniqueField
// and $scope.galleryDatas[index].uniqueField
if(newIndex !== -1)
$scope.checkedValue.splice(newIndex,1);
}
};
注意:选中的行数据存储在$scope.checkedValue
数组中,未选中时则从$scope.checkedValue
数组中删除
和HTML:
<td><input type="checkbox" ng-model="gl.checked" ng-click="saveInArray($index)"> {{$index+1}}</td>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.