//表单异步提交 function formPost(form, url) { $('#' + form).form('submit', { url: url, onSubmit: function () { return $(this).form('validate'); }, success: function (result) { result = eval('(' + result + ')'); if (result.Statu = 'OK') { alert("操作成功"); } else { if (result.Msg) { alert("保存失败!" + result.Msg); } else { alert("保存失败!"); } } } }); } //对话框表单异步提交(成功关闭对话框) function formPost(form, url, dlg, dg, tree, redirect) { $('#' + form).form('submit', { url: url, onSubmit: function () { return $(this).form('validate'); }, success: function (result) { result = eval('(' + result + ')'); if (result.Statu == 'OK') { //关闭对话框 if (dialog) { $('#' + dlg).dialog('close'); } //刷新datagrid或treegrid if (dg) { if (t) { $('#' + dg).treegrid('reload'); } else { $('#' + dg).datagrid('reload'); } } } else { if (result.Msg) { alert("保存失败!" + result.Msg); } else { alert("保存失败!"); } } //重定向 if (redirect) { window.open(redirect); } } }); } //删除数据 function delData(url, dg, tree) { if (url) { $.messager.confirm('确认', '确定删除?', function (r) { if (r) $.post(url, function () { }).success(function (data) { var msgStr = "删除成功"; $.messager.show({ title: '', msg: msgStr, timeout: 3000, showType: 'slide' }); if (tree) { $('#' + dg).treegrid('reload'); } else { $('#' + dg).datagrid('reload'); } }).error(function () { $.messager.alert('错误', '删除发生错误'); }); }); } } //显示对话框 function showDlg(url, dlg, handle) { $('#' + dlg).html(); $('#' + dlg).load(url, function () { $(this).dialog({ title: '', modal: true, rownumbers: true, loadingMessage: '正在加载...', buttons: [{ text: '提交', iconCls: 'icon-ok', handler: handle }, { text: '取消', handler: function () { $('#' + dlg).dialog('close'); } }] }); }).dialog('open'); } //查询对话框 function showSearch(url, dlg, dg) { $('#' + dlg).load(url, function () { $(this).dialog({ title: '查询', modal: true, loadingMessage: '正在加载...', buttons: [{ text: '提交', iconCls: 'icon-ok', handler: function () { var search = ''; $('#' + dlg).find(":text,:selected,:textare,:hidden,:checked,:password").each(function () { search = search + this.id + '&' + this.value + '^'; }); //执行查询 $('#' + dg).datagrid('reload', { serach: search }); $('#' + dlg).dialog('close'); } }, { text: '取消', handler: function () { $('#' + dlg).dialog('close'); } }] }); }).dialog('open'); } function GetPic(v) { return '<img src=' + v + ' height="64px" width="64px"/>'; } //关闭tab时释放iframe占用内存 $.fn.panel.defaults.onBeforeDestroy = function () { var frame = $('iframe', this); try { if (frame.length > 0) { for (var i = 0; i < frame.length; i++) { frame[i].contentWindow.document.write(''); frame[i].contentWindow.close(); } frame.remove(); if ($.browser.msie) { CollectGarbage(); } } } catch (e) { } };
在mvc中使用
//create function c_dlg() { $('#c_dlg').html(); var url = '@Url.Action("add", "checksku")'; showDlg(url, 'c_dlg', ajaxAdd); } function ajaxAdd() { formPost('c_form', '@Url.Action("add", "checksku")', 'c_dlg', 'dg'); } //update function u_dlg() { var id = getSelectedRow(); if (id) { var url = '@Url.Action("edit", "checksku")/' + id; showDlg(url, 'u_dlg', ajaxEdit); } } function ajaxEdit() { formPost('u_form', '@Url.Action("edit", "checksku")', 'u_dlg', 'dg'); } function del() { var id = getSelectedRow(); if (id) { var url = '@Url.Action("delete", "checksku")/' + id; delData(url, 'dg'); } } function getSelectedRow() { var row = $('#dg').datagrid('getSelected'); if (row) { return row.Id; } else { $.messager.show({ title: '提示', msg: '请选择行', timeout: 3000, showType: 'slide' }); } }
注意不能使用分布视图,否则会导致dialog无效,或者在分布视图中加入
@{ Layout = null; }