Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function(){
- var janela = $('#janela');
- var conteudo = $('.modal-body');
- janela.click(function(){
- $.post('ajax/post.php', {acao: 'form_cad'}, function(retorno){
- $('#myModal').modal({backdrop: 'static'});
- conteudo.html(retorno);
- });
- });
- $("#myModal").on("submit", 'form[name="form_cad"]', function(){
- var form = $(this);
- var botao = form.find(':button');
- $.ajax({
- url: 'ajax/post_controller.php',
- type: 'POST',
- data: 'acao=cadastro&'+form.serialize(),
- beforeSend: function(){
- botao.attr('disabled', true);
- $('.load').fadeIn('slow');
- },
- success: function(retorno){
- botao.attr('disabled', false);
- $('.load').fadeOut('slow');
- if (retorno === 'cadastrou') {
- msg('Administrador Cadastrado com sucesso', 'sucesso');
- listarAdmin('ajax/post_controller.php', 'listar_admin', true);
- }else{
- msg('Erro ao cadastrar administrador', 'erro');
- }
- }
- });
- return false;
- });
- //BTN EDIT
- $('.table').on("click", '#btn_edit', function(){
- var id = $(this).attr('data-id');
- $.post('ajax/post_controller.php', {acao: 'form_edit', id: id}, function(retorno){
- $('#myModal').modal({backdrop: 'static'});
- conteudo.html(retorno);
- });
- return false;
- });
- //BTN ATUALIZA
- $('#myModal').on("submit", 'form[name="form_edit"]', function(){
- var dados = $(this);
- var botao = dados.find(':button');
- $.ajax({
- url: 'ajax/post_controller.php',
- type: 'POST',
- data: 'acao=edit&'+dados.serialize(),
- beforeSend: function(){
- botao.attr('disabled', true);
- $('.load').fadeIn('slow');
- },
- success: function(retorno){
- if(retorno === 'atualizou'){
- dados.fadeOut('slow', function(){
- msg('Administrador atualizado com sucesso!', 'sucesso');
- listarAdmin('ajax/post_controller.php', 'listar_admin', true);
- });
- }else{
- msg('Você não alterou nenhum dado do administrador!', 'alerta');
- $('.load').fadeOut('slow', function(){
- botao.attr('disabled', false);
- });
- }
- }
- });
- return false;
- });
- //BTN EXCLUIR
- $('.table').on('click', '#btn_excluir', function(){
- var id = $(this).attr('data-id');
- if(confirm('Você deseja realmente excluir este registro?')){
- $.post('ajax/controller.php', {acao: 'excluir', id: id}, function(retorno){
- if(retorno === 'deletou'){
- alert('Deletado com sucesso!');
- listarAdmin('ajax/post_controller.php', 'listar_admin', true);
- }else{
- alert('Erro ao excluir administrador');
- }
- });
- }else{
- return false;
- }
- });
- //FUNÇÔES GERAL
- function listarAdmin(url, acao, atualiza){
- $.post(url, {acao: acao}, function(retorno){
- var tbody = $('.table').find('tbody');
- var load = tbody.find('.load');
- if(atualiza === true){
- tbody.html(retorno);
- }else{
- load.fadeOut('slow', function(){
- tbody.html(retorno);
- });
- }
- });
- }
- listarAdmin('ajax/controller.php', 'listar_admin');
- //FUNÇÃO DE MENSAGEM
- function msg(msg, tipo){
- var retorno = $('.retorno');
- var tipo = (tipo === 'sucesso') ? 'success' : (tipo === 'alerta') ? 'warning' : (tipo === 'erro') ? 'danger' : (tipo === 'info') ? 'info' : alert('INFORME MENSAGENS DE SUCESSO, ALERTA, ERRO E INFO');
- retorno.empty().fadeOut('fast', function(){
- return $(this).html('<div class="alert alert-'+tipo+'">'+msg+'</div>').fadeIn('slow');
- });
- setTimeout(function(){
- retorno.fadeOut('slow').empty();
- }, 6000);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement