标签:
<!DOCTYPE html>
<html lang="en" ng-app="plunker">
<head>
<title>AngularJS Plunker</title>
<script>document.write(‘<base href="‘ + document.location + ‘" />‘);</script>
<script src="http://libs.useso.com/js/jquery/1.11.1/jquery.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js"></script>
<script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.10.0.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
.inactive {border: none; background-color: #fff;}
.active{ background-color: #fff; }
</style>
</head>
<body ng-controller="MainCtrl">
<h2>Inline Edit</h2>
<table>
<tr> <th>name</th> <th>phone</th> <th></th> </tr>
<tr ng-repeat="employee in employees">
<td>
<input type="text" id=‘txt_name_{{employee.id}}‘ ng-model="employee.name" class="inactive" readonly />
</td>
<td> <input type="text" ng-model="employee.phone" class="inactive" readonly /></td>
<td><edit ng-Model="employee" ng-show="showEdit"><a>Edit</a></edit>
<update ng-Model="employee" ng-show="!showEdit"><a>Update</a></update>
<cancel ng-Model="employee" ng-show="!showEdit"> | <a>Cencel</a></cancel> |
<delete ng-Model="employee"><a>Delete</a></delete>
</td>
</tr>
</table>
<script>
var app = angular.module(‘plunker‘, [‘ui.bootstrap‘]);
app.controller(‘MainCtrl‘, function($scope) {
$scope.name = ‘World‘;
$scope.employees =[{id:101, name:‘John‘, phone:‘555-1276‘},{id:102, name:‘Mary‘, phone:‘800-1233‘},{id:103,name:‘Mike‘, phone:‘555-4321‘},{id:104,name:‘Adam‘, phone:‘555-5678‘},{id:105,name:‘Julie‘, phone:‘555-8765‘}, {id:106,name:‘Juliette‘, phone:‘555-5678‘}];
$scope.showEdit = true; //当前状态
$scope.master = {}; //临时变量,存储中间值
});
app.directive("edit",function($document){
return{
restrict: ‘AE‘,
require: ‘ngModel‘,
link: function(scope,element,attrs,ngModel){
element.bind("click",function(){
alert("I am clicked for editing");
var id = "txt_name_" +ngModel.$modelValue.id;
scope.$apply(function(){
angular.copy(ngModel.$modelValue,scope.master);
})
var obj = $("#"+id);
obj.removeClass("inactive");
obj.addClass("active");
obj.removeAttr("readOnly");
scope.$apply(function(){
scope.showEdit = false;
})
});
});
}
}
});
app.directive("update",function($document){
return{
restrict: ‘AE‘,
require: ‘ngModel‘,
link: function(scope,element,attrs,ngModel){
element.bind("click",function(){
alert(ngModel.$modelValue + " is updated, Update your value here.");
var id = "txt_name_" +ngModel.$modelValue.id;
var obj = $("#"+id);
obj.removeClass("active");
obj.addClass("inactive");
obj.attr("readOnly",true);
scope.$apply(function(){
scope.showEdit = true;
})
})
}
}
});
app.directive("cancel",function($document){
return{
restrict: ‘AE‘,
require: ‘ngModel‘,
link: function(scope,element,attrs,ngModel){
element.bind("click",function(){
scope.$apply(function(){
angular.copy(scope.master,ngModel.$modelValue);
//console.log(ngModel.$modelValue);
})
var id = "txt_name_" +ngModel.$modelValue.id;
var obj = $("#"+id);
obj.removeClass("active");
obj.addClass("inactive");
obj.prop("readOnly",true);
scope.$apply(function(){
scope.showEdit = true;
})
})
}
}
});
app.directive("delete",function($document){
return{
restrict:‘AE‘,
require: ‘ngModel‘,
link:function(scope, element, attrs,ngModel){
element.bind("click",function(){
var id = ngModel.$modelValue.id;
alert("delete item where employee id:=" + id);
scope.$apply(function(){
for(var i=0; i<scope.employees.length; i++){
if(scope.employees[i].id==id){
console.log(scope.employees[i])
scope.employees.splice(i,1);
}
}
console.log(scope.employees);
})
})
}
}
});
</script>
</body>
</html>
标签:
原文地址:http://www.cnblogs.com/dengzy/p/5372244.html