raphaelluiz128

MULTISELECTjs

Jul 18th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. 'use strict';
  2.  
  3. /**
  4. * @ngdoc function
  5. * @name frontEadApp.controller:MainCtrl
  6. * @description
  7. * # MainCtrl
  8. * Controller of the frontEadApp
  9. */
  10.  
  11.  
  12. var app = angular.module('frontEadApp');
  13. var validacao;
  14. var baseUrl = 'https://apiead.herokuapp.com/api/clientes';
  15.  
  16. var options = [
  17. 'Sistema WEB', 'Aplicativo iOS', 'Aplicativo Android', 'Website Estático', 'Website Gerenciável'
  18. ];
  19.  
  20. app.controller('MainCtrl', ['$scope', '$http', '$uibModal', '$rootScope', function ($scope, $http, $uibModal, $rootScope, data) {
  21. $rootScope.clientes = {};
  22.  
  23. $rootScope.options = options;
  24.  
  25. $http.get(baseUrl).then(function (response) {
  26. $rootScope.clientes = response.data;
  27. }, function (err) {
  28. console.log(err);
  29. });
  30.  
  31. const validacaoDeCampos = function () {
  32. if (angular.element('#documentoInput').val() == '' || angular.element('#dataNascimentoInput').val() == '' ||
  33. angular.element('#nomeInput').val() == '') {
  34. swal("Campos em branco", " Por favor informe todos os dados dos campos! ", "warning");
  35. validacao = 0;
  36. } else {
  37. validacao = 1;
  38. }
  39.  
  40. }
  41.  
  42. $scope.incluir = function () {
  43. validacaoDeCampos();
  44. //console.log(document.getElementById('selection').getElementsByTagName("option").innerText);
  45. var options = document.getElementById("selection");
  46. console.log(options);
  47.  
  48. if (validacao == 1) {
  49. var documento = angular.element('#documentoInput').val().replace(/\D/g, '');
  50. var tamanhoClientes = $rootScope.clientes.length;
  51. var objCliente = {
  52. "nome": angular.element('#nomeInput').val(),
  53. "dataNascimento": angular.element('#dataNascimentoInput').val(),
  54. "documento": documento,
  55. "servicos": ["Aplicativo Android"]
  56. }
  57.  
  58. var key = tamanhoClientes + 1;
  59. $http.post(baseUrl + '/', objCliente).then(function (response) {
  60. if (response) {
  61. $rootScope.clientes.push(response.data);
  62. swal("Adicionado", " Adicionado com Sucesso! ", "success");
  63. };
  64. });
  65. }
  66. };
  67.  
  68.  
  69. $scope.remover = function (cliente) {
  70.  
  71. var key = $rootScope.clientes.indexOf(cliente);
  72. $http.delete(baseUrl + '/delete/' + cliente._id).then(function (response) {
  73. if (response) {
  74. if (key !== -1) {
  75. $rootScope.clientes.splice(key, 1);
  76. swal("Removido!", " Removido com Sucesso! ", "success");
  77. }
  78. }
  79. });
  80. };
  81.  
  82.  
  83. $scope.modalEditar = function (eCliente) {
  84. var modalInstance = $uibModal.open({
  85. templateUrl: 'views/editarCliente.html',
  86. controller: 'editarCtrl',
  87. })
  88. sessionStorage.eCliente = JSON.stringify(eCliente);
  89. sessionStorage.key = $rootScope.clientes.indexOf(eCliente);
  90.  
  91.  
  92. }
  93. }]).controller('editarCtrl', function ($scope, $uibModalInstance, $http) {
  94.  
  95. $scope.eCliente = JSON.parse(sessionStorage.eCliente);
  96. sessionStorage.idEdicao = $scope.eCliente._id;
  97.  
  98.  
  99. $scope.alterar = function () {
  100. var documento = angular.element('#documentoInputE').val().replace(/\D/g, '');
  101. var objCliente = {
  102. "nome": angular.element('#nomeInputE').val(),
  103. "dataNascimento": angular.element('#dataNascimentoInputE').val(),
  104. "documento": documento,
  105. "servicos": ["Aplicativo Android"]
  106. }
  107.  
  108. $http.put(baseUrl + '/editar/' + sessionStorage.idEdicao, objCliente).then(function (response) {
  109. if (response) {
  110. for (var i = 0; i < $scope.clientes.length; i++) {
  111. if ($scope.clientes[i]._id == sessionStorage.idEdicao) {
  112. $scope.clientes[i].nome = objCliente.nome;
  113. $scope.clientes[i].dataNascimento = objCliente.dataNascimento;
  114. $scope.clientes[i].documento = objCliente.documento;
  115. swal("Alterado!", " Dados alterados com Sucesso! ", "success");
  116. };
  117. };
  118. }
  119. });
  120.  
  121. $uibModalInstance.close();
  122. };
  123. $scope.cancelar = function () {
  124. $uibModalInstance.dismiss('cancel');
  125. };
  126.  
  127.  
  128. });
Add Comment
Please, Sign In to add comment