aj基本增删改查

来源:互联网 发布:ubuntu源无法使用 编辑:程序博客网 时间:2024/05/17 04:52
  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset="UTF-8">  
  5.         <title>综合练习</title>  
  6.         <style>  
  7.             .addUser{  
  8.                 width: 100px;height: 40px;font-size: 18px;background-color: #11C1F3;  
  9.             }  
  10.         </style>  
  11.         <script type="text/javascript" src="js/angular.js" ></script>  
  12.         <script type="text/javascript" src="js/angular-route.js" ></script>  
  13.         <script type="text/javascript">  
  14.             var app = angular.module("myApp",["ngRoute"]);  
  15.             //使用config配置路由规则  
  16.             app.config(["$routeProvider",function($routeProvider){  
  17.                 $routeProvider  
  18.                     .when("/addUser",{  
  19.                         controller:"addUserCtrl",  
  20.                         templateUrl:"addUser.html"  
  21.                         })  
  22.                     .when("/updatePwd/:name",{  
  23.                         controller:"updatePwdCtrl",  
  24.                         templateUrl:"updatePwd.html"  
  25.                         })  
  26.                     .otherwise({redirectTo:"/addUser"});  
  27.             }]);  
  28.             app.controller("myCtrl",function($scope,$location){  
  29.                 $scope.users = [  
  30.                     {"id":1,"name":"张三","pwd":"123","age":20,"sex":"男"},  
  31.                     {"id":2,"name":"李四","pwd":"456","age":33,"sex":"女"},  
  32.                     {"id":3,"name":"王五","pwd":"789","age":42,"sex":"男"}  
  33.                 ];  
  34.                 //定义页面跳转方法  
  35.                 $scope.goToUrl = function(path){  
  36.                     alert(path);  
  37.                     $location.path(path);  
  38.                 }  
  39.                 //全部删除  
  40.                 $scope.del=function(){  
  41.                     if(confirm("确实删除吗?")){  
  42.                         $scope.users=[];  
  43.                     }  
  44.                 };  
  45.                 $scope.ageTest=function(age,agesize){  
  46.                       
  47.                     if(agesize !="--请选择--"){  
  48.                         var ages=agesize.split("-");  
  49.                         var ageMin=ages[0];  
  50.                         var ageMax=ages[1];  
  51.                           
  52.                         if(age<ageMin || age>ageMax){  
  53.                               
  54.                             return false;  
  55.                         }else{  
  56.                             return true;  
  57.                         }  
  58.                     }else{  
  59.                         return true;  
  60.                     }  
  61.                 }  
  62.                 //批量删除  
  63.                 $scope.deleteSel=function(){  
  64.                     var userNames=[];  
  65.                     for(index in $scope.users){  
  66.                         if($scope.users[index].state == true){  
  67.                             userNames.push($scope.users[index].name);  
  68.                         }  
  69.                     }  
  70.                   
  71.                 if(userNames.length>0){  
  72.                     if(confirm("是否删除选中项?")){  
  73.                         for(i in userNames){  
  74.                             var name=userNames[i];  
  75.                             for(i2 in $scope.users){  
  76.                                 if($scope.users[i2].name==name){  
  77.                                     $scope.users.splice(i2,1);  
  78.                                 }  
  79.                             }  
  80.                         }  
  81.                     }  
  82.                 }else{  
  83.                     alert("请选择删除的项")  
  84.                 }  
  85.                 }  
  86.             });  
  87.             //定义添加用户控制器  
  88.             app.controller("addUserCtrl",function($scope){  
  89.                   
  90.                 $scope.name = "";  
  91.                 $scope.pwd = "";  
  92.                 $scope.pwd2 = "";  
  93.                 $scope.age = "";  
  94.                 $scope.sex = "";  
  95.                   
  96.                 $scope.sub = function(){  
  97.                     var newUser = {  
  98.                         id:$scope.users.length + 1,  
  99.                         name:$scope.name,  
  100.                         pwd:$scope.pwd,  
  101.                         age:$scope.age,  
  102.                         sex:$scope.sex  
  103.                     }  
  104.                     //添加新用户.  
  105.                     $scope.users.push(newUser);  
  106.                 }  
  107.             });  
  108.             //定义修改用户控制器  
  109.             app.controller("updatePwdCtrl",function($scope,$routeParams){  
  110.                 $scope.name = $routeParams.name;  
  111.                 $scope.oldPwd = "";  
  112.                 $scope.pwd1 = "";  
  113.                 $scope.pwd2 = "";  
  114.                 $scope.updatePwd = function(){  
  115.                     for(index in $scope.users){  
  116.                         if($scope.users[index].name==$scope.name){  
  117.                             $scope.users[index].pwd=$scope.pwd1;  
  118.                         }  
  119.                     }  
  120.                 }  
  121.                   
  122.             });  
  123.               
  124.         </script>  
  125.     </head>  
  126.     <body ng-app="myApp" ng-controller="myCtrl">  
  127.         <center>  
  128.             <h3>用户信息表</h3>  
  129.             <div>  
  130.                 <input ng-model="search" placeholder="用户名查询"  size="10" />    
  131.                 年龄:<select id="age" ng-model="agesize" ng-init="agesize='--请选择--'">  
  132.                         <option>--请选择--</option>  
  133.                         <option>19-30</option>  
  134.                         <option>31-40</option>  
  135.                         <option>41-50</option>  
  136.                     </select>    
  137.                 性别:<select>  
  138.                     <option></option>  
  139.                     <option></option>  
  140.                 </select>    
  141.                 <input type="button" value="全部删除"  ng-click="del()"/>  
  142.                 <input ng-click="deleteSel()" type="button" value="批量删除"/>  
  143.             </div><br />  
  144.             <div>  
  145.                 <table border="1" cellspacing="1" cellpadding="10">  
  146.                     <thead>  
  147.                         <tr>  
  148.                             <th><input type="checkbox"</th>  
  149.                             <th>id</th>  
  150.                             <th>用户名</th>  
  151.                             <th>密码</th>  
  152.                             <th>年龄</th>  
  153.                             <th>性别</th>  
  154.                             <th>操作</th>  
  155.                         </tr>  
  156.                     </thead>  
  157.                     <tbody>  
  158.                         <tr ng-repeat="user in users | filter:{'name':search}" ng-if="ageTest(user.age,agesize)">  
  159.                             <th><input ng-model="user.state" type="checkbox"</th>  
  160.                             <td>{{user.id}}</td>  
  161.                             <td>{{user.name}}</td>  
  162.                             <td>{{user.pwd}}</td>  
  163.                             <td>{{user.age}}</td>  
  164.                             <td>{{user.sex}}</td>  
  165.                             <td><button ng-click="goToUrl('/updatePwd/'+user.name)">修改密码</button> </td>  
  166.                         </tr>  
  167.                     </tbody>  
  168.                 </table>  
  169.             </div><br />  
  170.             <button class="addUser" ng-click="goToUrl('/addUser')">添加用户</button><br /><br />  
  171.             <!-- 1.添加用户页面 -->  
  172.             <script type="text/ng-template" id="addUser.html">  
  173.                 <form action="">  
  174.                     <table border="1" cellspacing="1" cellpadding="10">  
  175.                         <tr>  
  176.                             <th>用户名:</th>  
  177.                             <td><input ng-model="name" placeholder="请输入用户名" /></td>  
  178.                         </tr>  
  179.                         <tr>  
  180.                             <th>密 码:</th>  
  181.                             <td><input ng-model="pwd" placeholder="请输入密码" /></td>  
  182.                         </tr><tr>  
  183.                             <th>年 龄:</th>  
  184.                             <td><input ng-model="age" placeholder="请输入年龄" /></td>  
  185.                         </tr><tr>  
  186.                             <th>性 别:</th>  
  187.                             <td><input ng-model="sex" placeholder="请输入性别" /></td>  
  188.                         </tr>  
  189.                         <tr align="center">  
  190.                             <td colspan="2"><input type="button" ng-click="sub()" value="提交" /></td>  
  191.                         </tr>  
  192.                     </table>  
  193.                 </form>  
  194.             </script>  
  195.             <!-- 2.修改用户信息页面 -->  
  196.             <script type="text/ng-template" id="updatePwd.html">  
  197.                 <form>  
  198.                     <table border="1" cellspacing="1" cellpadding="10">  
  199.                         <tr>  
  200.                             <th>用户名:</th>  
  201.                             <td><input disabled="disabled" ng-model="name" placeholder="请输入用户名"  /></td>  
  202.                         </tr>  
  203.                         <tr>  
  204.                             <th>旧密码:</th>  
  205.                             <td><input ng-model="oldPwd" placeholder="请输入密码" /></td>  
  206.                         </tr><tr>  
  207.                             <th>新密码:</th>  
  208.                             <td><input ng-model="pwd1" placeholder="请输入密码"  /></td>  
  209.                         </tr><tr>  
  210.                             <th>确认:</th>  
  211.                             <td><input ng-model="pwd2" placeholder="请输入密码"  /></td>  
  212.                         </tr>  
  213.                         <tr align="center">  
  214.                             <td colspan="2"><input type="button" value="提交" ng-click="updatePwd()" /></td>  
  215.                         </tr>  
  216.                     </table>  
  217.                 </form>  
  218.             </script>  
  219.               
  220.               
  221.             <!-- 6.指定相应内容 -->  
  222.             <div ng-view>  
  223.                   
  224.             </div>  
  225.         </center>  
  226.     </body>  
  227. </html>  
原创粉丝点击