Advertisement
Guest User

Untitled

a guest
Jan 26th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.74 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5. <title>Angular</title>
  6. <script type="text/javascript" src="angular.min.js"></script>
  7. <script type="text/javascript" src="app.js"></script>
  8. <link rel="stylesheet" href="style.css" />
  9. <meta charset="utf-8">
  10. </head>
  11.  
  12.  
  13. <body ng-app="miApp">
  14.  
  15. <div class="title">
  16. <label>Directiva ng-filter</label>
  17. </div>
  18.  
  19. <div class="content" ng-controller="tableController">
  20.  
  21.  
  22. <div class="form" >
  23. <form ng-submit="submit()" name="formu">
  24. <label>Usuario</label><br>
  25. <input type="text" name="useri" ng-model="user" placeholder="Usuario" required><br>
  26. <label class="warning" ng-show="!formu.useri.$pristine && formu.useri.$error.required">The user is required</label><br><br>
  27.  
  28. <label>Perfil</label><br>
  29. <select ng-model="profiles.selected" ng-options="options.profile for options in profiles.options track by options.id">
  30. <!-- OPTIONS -->
  31. </select><br><br><br>
  32.  
  33. <label>Contraseña</label><br>
  34. <input type="password" name="pass" ng-model="pass" placeholder="Contraseña" required/><br>
  35. <label class="warning" ng-show="!formu.pass.$pristine && formu.pass.$error.required">The password is required</label><br><br>
  36.  
  37.  
  38. <label>Confirme contraseña</label><br>
  39. <input type="password" name="passConfirm" ng-model="passConfirmation" placeholder="Contraseña" required data-password-verify="pass"><br>
  40. <label class="warning" ng-show="!formu.passConfirm.$pristine && formu.passConfirm.$error.required">The password is required</label><br>
  41. <label class="warning" ng-show="valida()">Diferent passwords</label><br>
  42.  
  43. <input type="submit" ng-disabled="formu.$invalid || valida()" value="Guardar">
  44. </form>
  45. </div><!--
  46.  
  47.  
  48.  
  49.  
  50. --><div class="table" >
  51.  
  52. <label>Usuarios</label><br><br>
  53. <table class="tableData">
  54. <thead>
  55. <tr>
  56. <td><input size="6" ng-model="buscar.clave" type="text" placeholder="Clave"></td>
  57. <td><input ng-model="buscar.user" type="text" placeholder="Usuario"></td>
  58. <td colspan="3" class="tede">Acciones</td>
  59.  
  60. </tr>
  61. <tr>
  62. <td>Clave</td>
  63. <td>Usuario</td>
  64. <td width="18%">Perfil</td>
  65. <td width="18%" class="tede">Editar</td>
  66. <td width="18%" class="tede">Borrar</td>
  67. </tr>
  68. </thead>
  69. <tbody>
  70. <tr ng-repeat="user in users | filter: buscar:strict">
  71. <td>{{ user.clave }}</td>
  72. <td>{{ user.user}}</td>
  73. <td>{{ user.profile}}</td>
  74. <td class="tede"><button ng-Click="edit($index, user.profile)">Editar</button></td>
  75. <td class="tede"><button ng-Click="delete($index)">Borrar</button></td>
  76. </tr>
  77. </tbody>
  78. </table>
  79. </div>
  80. </body>
  81.  
  82. </html>
  83.  
  84. var app = angular.module('miApp',[]);
  85.  
  86.  
  87. app.controller('tableController', function($scope)
  88. {
  89.  
  90. var OPC = -1;
  91.  
  92.  
  93. $scope.profiles =
  94. {
  95. options: [
  96. { id: '1', profile: "Dios"},
  97. { id: '2', profile: "Mortal"},
  98. ],
  99. selected: {id:'1', profile: "Dios"}
  100. };
  101.  
  102. $scope.users = [
  103. {clave:'1', user:'Adrian', profile:'Dios', pass:'adri', confPass:'adri'},
  104. {clave:'2', user:'González', profile:'Mortal', pass:'go', confPass:'go'},
  105. {clave:'3', user:'Ceja', profile:'Mortal', pass:'ce', confPass:'ce'},
  106. {clave:'4', user:'Maytro', profile:'Dios', pass:'ma', confPass:'ma'},
  107. ];
  108.  
  109. $scope.IDS = $scope.users.length + 1;
  110.  
  111. console.log("ID siguiente:" + $scope.IDS);
  112.  
  113. $scope.submit = function()
  114. {
  115. if (OPC < 0)
  116. {
  117. $scope.users.push({clave:$scope.IDS, user:this.user, profile: this.profiles.selected.profile, pass:this.pass, confPass: this.passConfirmation});
  118. clear_form();
  119. $scope.IDS += 1;
  120. console.log("ID siguiente:" + $scope.IDS);
  121. }
  122. else
  123. {
  124. $scope.users[OPC].user = $scope.user;
  125. $scope.users[OPC].profile = $scope.profiles.selected.profile;
  126. $scope.users[OPC].pass = $scope.pass;
  127. $scope.users[OPC].confPass = $scope.passConfirmation;
  128. clear_form();
  129. OPC = -1;
  130. }
  131. }
  132.  
  133. function clear_form()
  134. {
  135. $scope.user = "";
  136. $scope.pass = "";
  137. $scope.passConfirmation = "";
  138. $scope.profiles.selected = $scope.profiles.options[0];
  139. }
  140.  
  141.  
  142. $scope.delete = function(index)
  143. {
  144. $scope.users.splice(index,1);
  145. }
  146.  
  147. $scope.edit = function(index, perfil)
  148. {
  149. $scope.user = $scope.users[index].user;
  150. if (perfil == "Dios")
  151. $scope.profiles.selected = $scope.profiles.options[0];
  152. else
  153. $scope.profiles.selected = $scope.profiles.options[1];
  154. $scope.pass = $scope.users[index].pass;
  155. $scope.passConfirmation = $scope.users[index].confPass;
  156. OPC = index;
  157. }
  158.  
  159. $scope.valida = function()
  160. {
  161. if ($scope.pass != $scope.passConfirmation || $scope.passConfirmation != $scope.pass)
  162. return true;
  163. else
  164. return false;
  165. }
  166.  
  167. });
  168.  
  169.  
  170.  
  171.  
  172. app.directive("passwordVerify", function()
  173. {
  174. return {
  175. require: "ngModel",
  176. scope: {
  177. passwordVerify: '='
  178. },
  179. link: function(scope, element, attrs, ctrl)
  180. {
  181. scope.$watch(function()
  182. {
  183. var combined;
  184. if (scope.passwordVerify || ctrl.$viewValue)
  185. {
  186. combined = scope.passwordVerify + '_' + ctrl.$viewValue;
  187. }
  188. return combined;
  189.  
  190. },
  191. function(value)
  192. {
  193. if (value)
  194. {
  195. ctrl.$parsers.unshift(function(viewValue)
  196. {
  197. var origin = scope.passwordVerify;
  198. if (origin !== viewValue)
  199. {
  200. ctrl.$setValidity("passwordVerify", false);
  201. return undefined;
  202. }
  203. else
  204. {
  205. ctrl.$setValidity("passwordVerify", true);
  206. return viewValue;
  207. }
  208. });
  209. }
  210. });
  211. }
  212. };
  213. });
  214.  
  215. input,select {
  216. width: 200px;
  217. margin-bottom: 10px;
  218. height: 27px;
  219. font-size: 13px;
  220. }
  221.  
  222. button {
  223. width: 100px;
  224. height: 36px;
  225. margin: 10px;
  226. font-size: 14px;
  227. }
  228. table, th , td {
  229. border: 1px solid black ;
  230. border-collapse: collapse;
  231. padding: 5px;
  232. text-align: center;
  233. }
  234. table tr:nth-child(odd) {
  235. background-color: #f1f1f1 ;
  236. }
  237. table tr:nth-child(even) {
  238. background-color: #ffffff ;
  239. }
  240. form span {
  241. color: red ;
  242. font-style: italic;
  243. }
  244. .controles:hover {
  245. cursor: pointer;
  246. }
  247.  
  248. .controles.ed:hover {
  249. color: blue !important;
  250. }
  251. .controles.el:hover {
  252. color: red !important;
  253. }
  254. .tabla_usuarios {
  255. margin-top: 32px;
  256. }
  257. .tabla_usuarios table{
  258. margin-top: 10px;
  259. }
  260. form {
  261. margin-top: 20px;
  262. }
  263. .title {
  264. font-size: 30px;
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement