angularJs 复选框获取对象数组问题

ww179564174 2016-11-27 04:00:08
已经实现 单选多选获取 对应值的ID 存入对应数组现在 想同时取到 id,code,name三个值 成为三个数组,没有思路求帮助解答,具体代码如下

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="../angular-1.3.0.14/angular.js" ></script>
</head>
<body ng-app="myApp">
<div ng-controller="MyCtrl">
<table class="table">
<thead>
<tr>
<th>
<input type="checkbox" ng-click="selectAll($event)" ng-checked="isSelectedAll()"/>
</th>
<th>全选</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="e in users" ng-class="getSelectedClass(e.id)">
<td>
<input type="checkbox" name="selected" ng-checked="isSelected(e.id)" ng-click="updateSelection($event, e.id)"/>
</td>
<td>{{e.name}}</td>
</tr>
<p>id{{selected}}</p>
<p>姓名{{selected1}}</p>
<p>帐号{{selected2}}</p>
<button ng-click="ck()">点我</button>
</tbody>
</table>
</div>
<script>
var myApp = angular.module('myApp', []);
MyCtrl = function ($scope,$http) {
$scope.users=[{
"id": "1",
"code": "123",
"name": "qweww",
"email": "70ee6533@qq.com"
},
{
"id": "2",
"code": "130",
"name": "xueeen",
"email": "70ee533@qq.com"
},{
"id": "3",
"code": "1233",
"name": "qwe1",
"email": "707116533@qq.com"
},
{
"id": "4",
"code": "13230",
"name": "xueq3uan",
"email": "707116533@qq.com"
}];
$scope.ck = function(){
if($scope.selected.length == 0){
alert("空")
}else{
alert("非空")
}
}
$scope.selected = [];
$scope.selected1 = [];
$scope.selected2 = [];
var updateSelected = function (action, id ,name) {
if (action == 'add' & $scope.selected.indexOf(id) == -1) $scope.selected.push(id);
if (action == 'remove' && $scope.selected.indexOf(id) != -1) $scope.selected.splice($scope.selected.indexOf(id), 1);
}


$scope.updateSelection = function ($event, id) {
var checkbox = $event.target;
var action = (checkbox.checked ? 'add' : 'remove');
updateSelected(action, id);
};


$scope.selectAll = function ($event) {
var checkbox = $event.target;
var action = (checkbox.checked ? 'add' : 'remove');
for (var i = 0; i < $scope.users.length; i++) {
var entity = $scope.users[i];
updateSelected(action, entity.id);
}
};


$scope.getSelectedClass = function (entity) {
return $scope.isSelected(entity.id) ? 'selected' : '';
};


$scope.isSelected = function (id) {
return $scope.selected.indexOf(id) >= 0;
};

$scope.isSelectedAll = function () {
return $scope.selected.length === $scope.users.length;
};
}

</script>
</body>
</html>

...全文
239 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
scscms太阳光 2016-11-28
  • 打赏
  • 举报
回复
        var updateSelected = function (action, id) {
            var index = $scope.selected.indexOf(id);
            if (action == 'add' && index == -1){
                $scope.selected.push(id);
                for(var i = $scope.users.length;i--;){
                    if($scope.users[i].id == id){
                        $scope.selected1.push($scope.users[i].name);
                        $scope.selected2.push($scope.users[i].code);
                        break;
                    }
                }
            }else if (action == 'remove' && index != -1){
                $scope.selected.splice(index, 1);
                $scope.selected1.splice(index, 1);
                $scope.selected2.splice(index, 1);
            }
        }

87,901

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧