AngularJS实现对用户信息的增删改查

电玩女神 2022-06-07 12:22 293阅读 0赞

本篇是对表单进行增删改查的操作,用到的是AngularJS实现的,有需要的可以看一下。

实现效果:

Center

实现要求:

  1. 搭建页面

  2. 显示所有用户信息

  3. 根据用户名实现用户查询显示

  4. 根据年龄范围下拉菜单实现用户显示

  5. 根据用户年龄实现用户显示

  6. 实现批量删除功能:选中左侧多选框,点击批量删除,删除选中项,没选中提示用户,点击最上方的多选框,实现全选。

  7. 点击添加用户,下方页面显示添加用户界面,填写用户信息,满足条件如下进行添加

a) 用户名非空

b) 两次密码一致

c) 年龄在10到60之间

  1. 点击修改密码按钮,下方页面显示修改密码页面(如下),默认把要修改的用户名显示在用户名输入框,并且不能更改用户名,实现密码修改,要求如下

a) 旧密码要跟之前的密码一致

b) 两次输入的密码一致。

Center 1

源码:

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

发表评论

表情:
评论列表 (有 0 条评论,293人围观)

还没有评论,来说两句吧...

相关阅读

    相关 游客信息增删系统

    1 问题 在景区中来来往往的人很多,在登记表上填写游客信息非常麻烦而且不方便保存以及后续的增删改查,同样该问题也可以看作其他类似的填表信息录入问题,不仅是旅游存在这个问题。