1.需求
在添加页面实现一个checkbox的选择,然后在详情页面展示时,会自动选上之前被选中的。
2.添加页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl" >
选择
<div ng-repeat="item in list">
<input type="checkbox" name="tagName" value="item.id" ng-click="select(item.id,$event)"> {{item.shortName}}
</div>
结果:{{result}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
//创建checkbox用的
$scope.list=[{"id":1,"shortName":"张三"},{"id":2,"shortName":"李四"},{"id":3,"shortName":"王二"}];
//存储已选
$scope.result = [];
//触发事件
$scope.select = function(id,event){
console.log(event)//打印看看这是什么,有利于理解
console.log(action)
var action = event.target;
if(action.checked){//选中,就添加
if($scope.result.indexOf(id) == -1){//不存在就添加
$scope.result.push(id);
}
}else{//去除就删除result里
var idx = $scope.result.indexOf(id);
if( idx != -1){//不存在就添加
$scope.result.splice(idx,1);
}
}
$scope.equipmentFailureInfo.KDDT_STATE=$scope.result.toString(); //将选中的id保存到KDDT_STATE字段中
};
});
</script>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
3.详情展示
//假设添加页面的结果是:$scope.result = [3,2];
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl" >
回写时设置不可选,即设ng-disabled="true"
<div ng-repeat="item in list">
<input type="checkbox" name="tagName" ng-checked="isSelected(item.id)" value="item.id" ng-disabled="true" > {{item.shortName}}
</div>
结果:{{result}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
//创建checkbox用的
$scope.list=[{"id":1,"shortName":"张三"},{"id":2,"shortName":"李四"},{"id":3,"shortName":"王二"}];
//在添加页面得到的结果
//你会发现,顺序也不会影响结果
$scope.result = [3,2];
//被选中条件:ng-checked的结果为true
$scope.isSelected = function(id){
if($scope.equipmentFailureInfo.KDDT_STATE!=null){ //后台获取KDDT_STATE字段后转为数组存于$scope.result
$scope.result=$scope.equipmentFailureInfo.KDDT_STATE.split(",");
}
return $scope.result.indexOf(id)!=-1;
//只要返回的结果为true,则对应的checkbox就会被选中,
//所以我的思路是看存添加页面的结果里是否含有当前id即value值,
//有就返回的true,没有就返回false
};
});
</script>
</body>
</html>