分享

AngularJS进阶(十一)AngularJS实现表格数据的编辑,更新和删除

 WindySky 2018-02-08

AngularJS实现表格数据的编辑,更新和删除

效果

 

实现

首先,我们先建立一些数据,当然你可以从你任何地方读出你的数据

  1. var app = angular.module('plunker', ['ui.bootstrap']);  
  2. app.controller('MainCtrl', function($scope) {  
  3.   $scope.name = 'World';  
  4.   $scope.employees =[{id:101, name:'John', phone:'555-1276'},  
  5.                    {id:102, name:'Mary', phone:'800-1233'},  
  6.                    {id:103,name:'Mike', phone:'555-4321'},  
  7.                    {id:104,name:'Adam', phone:'555-5678'},  
  8.                    {id:105,name:'Julie', phone:'555-8765'},  
  9.                    {id:106,name:'Juliette', phone:'555-5678'}];  
  10. $scope.showEdit = true;  
  11.  $scope.master = {};  
  12. });  

因为我们没有用到angularbootstrap.这里,我们可以直接

  1. var app = angular.module('plunker',[]);  
  2. <!DOCTYPE html>  
  3. <html ng-app="plunker">  
  4.    
  5.   <head>  
  6.     <meta charset="utf-8" />  
  7.     <title>AngularJS Plunker</title>  
  8.     <script>document.write('<base href="' + document.location + '" />');</script>  
  9.        
  10.     <script data-require="angular.js@1.2.x" src="http://code./1.2.13/angular.js" data-semver="1.2.13"></script>  
  11.        
  12.     <script src="jquery-1.11.0.min.js"></script>  
  13.     <script src="ui-bootstrap-tpls-0.10.0.min.js"></script>  
  14.     <script src="bootstrap.js"></script>  
  15.     <script src="app.js"></script>  
  16.     <link rel="stylesheet" href="bootstrap.css" />  
  17.     <link rel="stylesheet" href="mycss.css" />  
  18.   </head>  
  19.    
  20.   <body ng-controller="MainCtrl">  
  21.     <h2>Inline Edit</h2>  
  22.     <!--<input id="test" value="ddd"/>-->  
  23.     <table>  
  24.       <tr>  
  25.         <th>name</th>  
  26.         <th>phone</th>  
  27.         <th></th>  
  28.       </tr>  
  29.       <tr ng-repeat="employee in employees">  
  30.         <td>  
  31.           <input type="text" id='txt_name_{{employee.id}}' ng-model="employee.name" class="inactive" readonly />  
  32.         </td>  
  33.         <td> <input type="text" ng-model="employee.phone" class="inactive" readonly /></td>  
  34.         <td><edit ng-Model="employee" ng-show="showEdit"><a>Edit</a></edit>  
  35.             <update ng-Model="employee" ng-show="!showEdit"><a>Update</a></update>   
  36.             <cancel ng-Model="employee" ng-show="!showEdit"> | <a>Cencel</a></cancel>  
  37.           | <delete ng-Model="employee"><a>Delete</a></delete>  
  38.         </td>  
  39.       </tr>  
  40.     </table>  
  41.   </body>  
  42. </html>  

我们来看其中一个标签,<edit>,这里呢,我们用ng-Model来绑定employee这个对象。

这里,我们用angulardirective来对着三个标签进行事件的绑定

angulardirctive主要作用于DOM对象,而且他可以对Element Name (比如<edit></edit>)  对应于E:)、Attribute(比如<mytag edit=express></mytag> 对应于A、

Comment <!--   my comment >  对应于M、Class <link class=mycssclass></link> 对应于C)。

默认对Attribute A),

当我们有

app.directiv("edit", function(){

  return{

    restrict: "E"

    . . . .

}

});

意思是说,我们要找到叫Element=edit”的DOM对象,这里就是<edit>,

当然你也可以携程 restrict: AE”,那意思就是说要找到Element或者attribute = editDOM对象

这里你可以随便对AEMC进行组合。

当你找到之后呢,就要对这个DOM进行操作,对于我们来说,就是对他绑定一个click的事件

  1. app.directive("edit", function(){  
  2.   return{  
  3.     restrict: "E",  
  4.     link: function(scope,element){  
  5.       element.bind("click",function(e){  
  6.         alert("I am clicked for editing");  
  7.       });  
  8.     }  
  9.   }  
  10. })  

这个时候呢,当你点Edit的时候呢,click事件就会触发。

再往下呢就是对edit click事件的延伸,我们要得到employee nameinputbox,然后对他进行css的转换,比如当你click edit后,应该出现inputboxcssinactive或者active的调整,并且移除readOnly

这里要注意一件事,就是angular.copy,为什么使用angular.copy?这个是为后面的cancel做准备的,当你放弃修改的时候,你希望你的值恢复成原样,这个时候,对于angularJS来说,是要对model恢复原样。如何恢复修改之前的model?最简单的方法就是创建一个$scope.master = {}空的对象,然后在你click edit之后,马上把还没改变的model拷贝到这个空的master中去,把master作为一个临时的存储对象。

当我们click Edit之后,我们要隐藏Edit,而叫Update | Cancel出现。这个时候$scope.showEdit就用上了,在<edit><update>,<cancel>上面都有一个ng-show,这个flag用来指定这个元素是不是要显示。ng-show=showEdit”这个值是绑定$scope.showEdit的。

  1. app.directive("edit", function(){  
  2.   return{  
  3.     restrict: "E",  
  4.     link: function(scope,element){  
  5.       element.bind("click",function(e){  
  6.         alert("I am clicked for editing");  
  7.       });  
  8.     }  
  9.   }  
  10. })  

下面,我们要给Update做事件的绑定。这里就没用什么可说的了。

  1. app.directive("update",function($document){  
  2.   return{  
  3.     restrict: 'AE',  
  4.     require: 'ngModel',  
  5.     link: function(scope,element,attrs,ngModel){  
  6.       element.bind("click",function(){  
  7.          alert(ngModel.$modelValue + " is updated, Update your value here.");  
  8.          var id = "txt_name_" +ngModel.$modelValue.id;  
  9.          var obj = $("#"+id);  
  10.          obj.removeClass("active");  
  11.          obj.addClass("inactive");  
  12.          obj.attr("readOnly",true);  
  13.           scope.$apply(function(){  
  14.            scope.showEdit = true;  
  15.          })  
  16.       })  
  17.     }  
  18.   }  
  19. })  

在下面就是Cancel了,上面说过了,Cancel的时候要还原之前的值,这个时候呢,我们就用angular.copy把当时临时存储的$scope.master拷贝回model

  1. app.directive("cancel",function($document){  
  2.   return{  
  3.     restrict: 'AE',  
  4.     require: 'ngModel',  
  5.     link: function(scope,element,attrs,ngModel){  
  6.       element.bind("click",function(){  
  7.          scope.$apply(function(){  
  8.            angular.copy(scope.master,ngModel.$modelValue);  
  9.            //console.log(ngModel.$modelValue);  
  10.          })  
  11.             
  12.          var id = "txt_name_" +ngModel.$modelValue.id;  
  13.          var obj = $("#"+id);  
  14.          obj.removeClass("active");  
  15.          obj.addClass("inactive");  
  16.          obj.prop("readOnly",true);  
  17.           scope.$apply(function(){  
  18.            scope.showEdit = true;  
  19.          })  
  20.       })  
  21.     }  
  22.   }  
  23. });  

最后就是Delete了,其实永远都要记住的事AngularMVC,所以你这里你不用操心删除table行这样的事,只要删除modelemploee.id = @id就可以了

  1. app.directive("delete",function($document){  
  2.   return{  
  3.     restrict:'AE',  
  4.     require: 'ngModel',  
  5.     link:function(scope, element, attrs,ngModel){  
  6.       element.bind("click",function(){  
  7.         var id = ngModel.$modelValue.id;  
  8.         alert("delete item where employee id:=" + id);  
  9.         scope.$apply(function(){  
  10.           for(var i=0; i<scope.employees.length; i++){  
  11.             if(scope.employees[i].id==id){  
  12.                console.log(scope.employees[i])  
  13.                scope.employees.splice(i,1);  
  14.             }  
  15.           }  
  16.           console.log(scope.employees);  
  17.         })  
  18.       })  
  19.     }  
  20.   }  
  21. });  

基本上就完工了。这里我没有用任何现成的angular 插件,这只是对angular基本原理的阐述,如有误导或者有能简单的方法请指教。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多