Advertisement
yeshuadesign

post.js

Jun 11th, 2015
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. $(document).ready(function(){
  2. var janela = $('#janela');
  3. var conteudo = $('.modal-body');
  4.  
  5. janela.click(function(){
  6.  
  7. $.post('ajax/post.php', {acao: 'form_cad'}, function(retorno){
  8. $('#myModal').modal({backdrop: 'static'});
  9.  
  10. conteudo.html(retorno);
  11. });
  12. });
  13.  
  14.  
  15. $("#myModal").on("submit", 'form[name="form_cad"]', function(){
  16.  
  17. var form = $(this);
  18. var botao = form.find(':button');
  19.  
  20. $.ajax({
  21. url: 'ajax/post_controller.php',
  22. type: 'POST',
  23. data: 'acao=cadastro&'+form.serialize(),
  24. beforeSend: function(){
  25. botao.attr('disabled', true);
  26. $('.load').fadeIn('slow');
  27. },
  28.  
  29. success: function(retorno){
  30. botao.attr('disabled', false);
  31. $('.load').fadeOut('slow');
  32.  
  33. if (retorno === 'cadastrou') {
  34. msg('Administrador Cadastrado com sucesso', 'sucesso');
  35. listarAdmin('ajax/post_controller.php', 'listar_admin', true);
  36. }else{
  37. msg('Erro ao cadastrar administrador', 'erro');
  38. }
  39. }
  40. });
  41.  
  42. return false;
  43. });
  44.  
  45. //BTN EDIT
  46. $('.table').on("click", '#btn_edit', function(){
  47. var id = $(this).attr('data-id');
  48. $.post('ajax/post_controller.php', {acao: 'form_edit', id: id}, function(retorno){
  49. $('#myModal').modal({backdrop: 'static'});
  50. conteudo.html(retorno);
  51. });
  52.  
  53. return false;
  54. });
  55.  
  56. //BTN ATUALIZA
  57. $('#myModal').on("submit", 'form[name="form_edit"]', function(){
  58. var dados = $(this);
  59. var botao = dados.find(':button');
  60.  
  61. $.ajax({
  62. url: 'ajax/post_controller.php',
  63. type: 'POST',
  64. data: 'acao=edit&'+dados.serialize(),
  65. beforeSend: function(){
  66. botao.attr('disabled', true);
  67. $('.load').fadeIn('slow');
  68. },
  69.  
  70. success: function(retorno){
  71. if(retorno === 'atualizou'){
  72. dados.fadeOut('slow', function(){
  73. msg('Administrador atualizado com sucesso!', 'sucesso');
  74. listarAdmin('ajax/post_controller.php', 'listar_admin', true);
  75. });
  76. }else{
  77. msg('Você não alterou nenhum dado do administrador!', 'alerta');
  78. $('.load').fadeOut('slow', function(){
  79. botao.attr('disabled', false);
  80. });
  81. }
  82. }
  83.  
  84.  
  85. });
  86.  
  87. return false;
  88. });
  89.  
  90. //BTN EXCLUIR
  91. $('.table').on('click', '#btn_excluir', function(){
  92. var id = $(this).attr('data-id');
  93.  
  94. if(confirm('Você deseja realmente excluir este registro?')){
  95. $.post('ajax/controller.php', {acao: 'excluir', id: id}, function(retorno){
  96. if(retorno === 'deletou'){
  97. alert('Deletado com sucesso!');
  98. listarAdmin('ajax/post_controller.php', 'listar_admin', true);
  99. }else{
  100. alert('Erro ao excluir administrador');
  101. }
  102. });
  103. }else{
  104. return false;
  105. }
  106. });
  107.  
  108. //FUNÇÔES GERAL
  109.  
  110. function listarAdmin(url, acao, atualiza){
  111. $.post(url, {acao: acao}, function(retorno){
  112. var tbody = $('.table').find('tbody');
  113. var load = tbody.find('.load');
  114. if(atualiza === true){
  115. tbody.html(retorno);
  116. }else{
  117. load.fadeOut('slow', function(){
  118. tbody.html(retorno);
  119. });
  120.  
  121. }
  122.  
  123. });
  124. }
  125.  
  126. listarAdmin('ajax/controller.php', 'listar_admin');
  127.  
  128. //FUNÇÃO DE MENSAGEM
  129. function msg(msg, tipo){
  130. var retorno = $('.retorno');
  131. var tipo = (tipo === 'sucesso') ? 'success' : (tipo === 'alerta') ? 'warning' : (tipo === 'erro') ? 'danger' : (tipo === 'info') ? 'info' : alert('INFORME MENSAGENS DE SUCESSO, ALERTA, ERRO E INFO');
  132.  
  133. retorno.empty().fadeOut('fast', function(){
  134. return $(this).html('<div class="alert alert-'+tipo+'">'+msg+'</div>').fadeIn('slow');
  135. });
  136.  
  137. setTimeout(function(){
  138. retorno.fadeOut('slow').empty();
  139. }, 6000);
  140.  
  141. }
  142. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement