From ca80b59829a5c731c0e79fe8ad88478c539de2b4 Mon Sep 17 00:00:00 2001 From: zhaoxiang <756958008@qq.com> Date: Wed, 9 Nov 2016 19:42:53 +0800 Subject: [PATCH] =?UTF-8?q?added=20=E5=AE=8C=E5=96=84POST=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E7=9A=84=E5=8A=A8=E6=80=81=E5=88=B7=E6=96=B0=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/controller/Menu.php | 6 +- application/admin/view/user/login.html | 2 +- public/admin/static/js/template.js | 210 ++++++++++++++++++------- 3 files changed, 161 insertions(+), 57 deletions(-) diff --git a/application/admin/controller/Menu.php b/application/admin/controller/Menu.php index 6415a79..b7b2551 100644 --- a/application/admin/controller/Menu.php +++ b/application/admin/controller/Menu.php @@ -47,7 +47,7 @@ class Menu extends Base { ], [ 'href' => url('Menu/del'), - 'class'=> 'btn-danger', + 'class'=> 'btn-danger ajax-delete', 'info'=> '删除', 'icon' => 'fa fa-trash', 'confirm' => 1, @@ -66,7 +66,7 @@ class Menu extends Base { [ 'info' => '删除', 'href' => url('Menu/del'), - 'class'=> 'btn-danger', + 'class'=> 'btn-danger ajax-delete', 'param'=> [$this->primaryKey], 'icon' => 'fa fa-trash', 'confirm' => 1, @@ -239,7 +239,7 @@ class Menu extends Base { } public function del(){ - + $this->error('失败'); } } \ No newline at end of file diff --git a/application/admin/view/user/login.html b/application/admin/view/user/login.html index c43d086..884c809 100644 --- a/application/admin/view/user/login.html +++ b/application/admin/view/user/login.html @@ -34,7 +34,7 @@
'+msg+'
', closeButton: false }); setTimeout(function(){ dialog.modal('hide'); - }, 3000); + }, wait); }; /** - * Ajax Post 表单提交 + * 刷新数据,允许带参数刷新 + * @param url + * @param urlData + */ + $.refresh = function ( url, urlData = '' ) { + $.ajax({ + type: "GET", + url: url, + data: urlData, + success: function(data){ + if( data.code == 200 ){ + if( data.data.tempType == 'table' ){ + if( $.buildTable ){ + $('#content').html($.buildTable(data.data)); + $('#tableBox').hide().fadeIn(800); + }else{ + $.getScript(JS_PATH + '/template/table.js', function (){ + $('#content').html($.buildTable(data.data)); + $('#tableBox').hide().fadeIn(800); + }); + } + } + if( data.data.tempType == 'add' ){ + if( $.buildAddForm ){ + $('#content').html($.buildAddForm(data.data)); + $('#formBox').hide().fadeIn(800); + }else{ + $.getScript(JS_PATH + '/template/form.js', function (){ + $('#content').html($.buildAddForm(data.data)); + $('#formBox').hide().fadeIn(800); + }); + } + } + if( data.data.tempType == 'edit' ){ + if( $.buildEditForm ){ + $('#content').html($.buildEditForm(data.data)); + $('#formBox').hide().fadeIn(800); + }else{ + $.getScript(JS_PATH + '/template/form.js', function (){ + $('#content').html($.buildEditForm(data.data)); + $('#formBox').hide().fadeIn(800); + }); + } + } + }else{ + $.alertMsg(data.msg); + setTimeout(function() { + if (data.url) { + location.href = data.url; + } + }, 1000*data.wait); + } + } + }); + }; + + /** + * Ajax Post 表单提交(增) */ bodyDom.on('click', '.ajax-post', function() { + var message,query,form,target; + var target_form = $(this).attr('target-form'); + var isRedirect = $(this).hasClass('redirect'); + form = $('#' + target_form); + query = form.serialize(); + target = form.attr('action'); + $.post(target, query).success(function(data) { + var wait = 1000*data.wait; + if (data.code == 1) { + if (data.url) { + message = data.msg + ' 页面即将自动跳转...'; + } else { + message = data.msg; + } + $.alertMsg(message); + if( isRedirect ){ + setTimeout(function() { + if (data.url) { + location.href = data.url; + } else { + location.reload(); + } + }, wait); + }else{ + setTimeout(function() { + if (data.url) { + $.refresh(data.url); + } + }, wait); + } + } else { + $.alertMsg(data.msg); + } + }); + return false; + }); + + /** + * Ajax Put 表单提交(改) + */ + bodyDom.on('click', '.ajax-put', function() { var message,query,form,target; var target_form = $(this).attr('target-form'); form = $('#' + target_form); query = form.serialize(); target = form.attr('action'); - $.post(target, query).success(function(data) { + $.ajax({ + type: "PUT", + url: target, + data: query + }).done(function( data ) { var wait = 1000*data.wait; if (data.code == 1) { if (data.url) { @@ -76,60 +179,61 @@ return false; }); + /** + * Ajax Delete 请求(删) * + */ + bodyDom.on('click', '.ajax-delete', function() { + var url = $(this).attr('url'), urlData = ''; + if( $(this).attr('data') ){ + urlData = $(this).attr('data'); + } + if( $(this).hasClass('confirm') ){ + bootbox.confirm({ + title: "温馨提醒:", + message: "您确定要这么做么?", + buttons: { + cancel: { + label: ' 取消' + }, + confirm: { + label: ' 确定' + } + }, + callback: function (result) { + if( result ){ + $.ajax({ + type: "DELETE", + url: url, + data: urlData + }).done(function( data ) { + var wait = 1000*data.wait; + if (data.code == 1) { + $.alertMsg(data.msg); + setTimeout(function() { + if (data.url) { + $.refresh(data.url); + } + }, wait); + } else { + $.alertMsg(data.msg); + } + }); + } + } + }); + } + return false; + }); + + /** + * Ajax 刷新页面 * + */ bodyDom.on('click', '.refresh', function() { var url = $(this).attr('url'), urlData = ''; if( $(this).attr('data') ){ urlData = $(this).attr('data'); } - $.ajax({ - type: "GET", - url: url, - data: urlData, - success: function(data){ - if( data.code == 200 ){ - if( data.data.tempType == 'table' ){ - if( $.buildTable ){ - $('#content').html($.buildTable(data.data)); - $('#tableBox').hide().fadeIn(800); - }else{ - $.getScript(JS_PATH + '/template/table.js', function (){ - $('#content').html($.buildTable(data.data)); - $('#tableBox').hide().fadeIn(800); - }); - } - } - if( data.data.tempType == 'add' ){ - // if( $.buildAddForm ){ - // $('#content').html($.buildAddForm(data.data)); - // $('#formBox').hide().fadeIn(800); - // }else{ - $.getScript(JS_PATH + '/template/form.js', function (){ - $('#content').html($.buildAddForm(data.data)); - $('#formBox').hide().fadeIn(800); - }); - // } - } - if( data.data.tempType == 'edit' ){ - if( $.buildEditForm ){ - $('#content').html($.buildEditForm(data.data)); - $('#formBox').hide().fadeIn(800); - }else{ - $.getScript(JS_PATH + '/template/form.js', function (){ - $('#content').html($.buildEditForm(data.data)); - $('#formBox').hide().fadeIn(800); - }); - } - } - }else{ - $.alertMsg(data.msg); - setTimeout(function() { - if (data.url) { - location.href = data.url; - } - }, 1000*data.wait); - } - } - }); + $.refresh(url, urlData); }); })(jQuery);