dwz框架---(2)表单回调函数
dwz中的表单回调函数大概有下面几种:
/** * 普通ajax表单提交 * @param {Object} form * @param {Object} callback * @param {String} confirmMsg 提示确认信息 */ function validateCallback(form, callback, confirmMsg) { var $form = $(form); if (!$form.valid()) { return false; } var _submitFn = function(){ $.ajax({ type: form.method || 'POST', url:$form.attr("action"), data:$form.serializeArray(), dataType:"json", cache: false, success: callback || DWZ.ajaxDone, error: DWZ.ajaxError }); } if (confirmMsg) { alertMsg.confirm(confirmMsg, {okCall: _submitFn}); } else { _submitFn(); } return false; }
/** * 带文件上传的ajax表单提交 * @param {Object} form * @param {Object} callback */ function iframeCallback(form, callback){ var $form = $(form), $iframe = $("#callbackframe"); if(!$form.valid()) {return false;} if ($iframe.size() == 0) { $iframe = $("<iframe id='callbackframe' name='callbackframe' src='about:blank' style='display:none'></iframe>").appendTo("body"); } if(!form.ajax) { $form.append('<input type="hidden" name="ajax" value="1" />'); } form.target = "callbackframe"; _iframeResponse($iframe[0], callback || DWZ.ajaxDone); } function _iframeResponse(iframe, callback){ var $iframe = $(iframe), $document = $(document); $document.trigger("ajaxStart"); $iframe.bind("load", function(event){ $iframe.unbind("load"); $document.trigger("ajaxStop"); if (iframe.src == "javascript:'%3Chtml%3E%3C/html%3E';" || // For Safari iframe.src == "javascript:'<html></html>';") { // For FF, IE return; } var doc = iframe.contentDocument || iframe.document; // fixing Opera 9.26,10.00 if (doc.readyState && doc.readyState != 'complete') return; // fixing Opera 9.64 if (doc.body && doc.body.innerHTML == "false") return; var response; if (doc.XMLDocument) { // response is a xml document Internet Explorer property response = doc.XMLDocument; } else if (doc.body){ try{ response = $iframe.contents().find("body").text(); response = jQuery.parseJSON(response); } catch (e){ // response is html document or plain text response = doc.body.innerHTML; } } else { // response is a xml document response = doc; } callback(response); }); } /** * navTabAjaxDone是DWZ框架中提前定义的表单提交回调函数. * server转回navTabId能够把那个navTab标记为reloadFlag=1, 下次切换到那个navTab时会又一次加载内容. * callbackType假设是closeCurrent就会关闭当前tab * 仅仅有callbackType="forward"时须要forwardUrl值 * navTabAjaxDone这个回调函数基本能够通用了,假设还有特殊须要也能够自己定义回调函数. * 假设表单提交仅仅提示操作是否成功, 就能够不指定回调函数. 框架会默认调用DWZ.ajaxDone() * <form action="/user.do?method=save" onsubmit="return validateCallback(this, navTabAjaxDone)"> * * form提交后返回json数据结构statusCode=DWZ.statusCode.ok表示操作成功, 做页面跳转等操作. statusCode=DWZ.statusCode.error表示操作失败, 提示错误原因. * statusCode=DWZ.statusCode.timeout表示session超时,下次点击时跳转到DWZ.loginUrl * {"statusCode":"200", "message":"操作成功", "navTabId":"navNewsLi", "forwardUrl":"", "callbackType":"closeCurrent", "rel"."xxxId"} * {"statusCode":"300", "message":"操作失败"} * {"statusCode":"301", "message":"会话超时"} * */ function navTabAjaxDone(json){ DWZ.ajaxDone(json); if (json.statusCode == DWZ.statusCode.ok){ if (json.navTabId){ //把指定navTab页面标记为须要“又一次加载”。注意navTabId不能是当前navTab页面的 navTab.reloadFlag(json.navTabId); } else { //又一次加载当前navTab页面 var $pagerForm = $("#pagerForm", navTab.getCurrentPanel()); var args = $pagerForm.size()>0 ? $pagerForm.serializeArray() : {} navTabPageBreak(args, json.rel); } if ("closeCurrent" == json.callbackType) { setTimeout(function(){navTab.closeCurrentTab(json.navTabId);}, 100); } else if ("forward" == json.callbackType) { navTab.reload(json.forwardUrl); } else if ("forwardConfirm" == json.callbackType) { alertMsg.confirm(json.confirmMsg || DWZ.msg("forwardConfirmMsg"), { okCall: function(){ navTab.reload(json.forwardUrl); }, cancelCall: function(){ navTab.closeCurrentTab(json.navTabId); } }); } else { navTab.getCurrentPanel().find(":input[initValue]").each(function(){ var initVal = $(this).attr("initValue"); $(this).val(initVal); }); } } } /** * dialog上的表单提交回调函数 * server转回navTabId。能够又一次加载指定的navTab. statusCode=DWZ.statusCode.ok表示操作成功, 自己主动关闭当前dialog * * form提交后返回json数据结构,json格式和navTabAjaxDone一致 */ function dialogAjaxDone(json){ DWZ.ajaxDone(json); if (json.statusCode == DWZ.statusCode.ok){ if (json.navTabId){ navTab.reload(json.forwardUrl, {navTabId: json.navTabId}); } else if (json.rel) { var $pagerForm = $("#pagerForm", navTab.getCurrentPanel()); var args = $pagerForm.size()>0 ? $pagerForm.serializeArray() : {} navTabPageBreak(args, json.rel); } if ("closeCurrent" == json.callbackType) { $.pdialog.closeCurrent(); } } } /** * 处理navTab上的查询, 会又一次加载当前navTab * @param {Object} form */ function navTabSearch(form, navTabId){ var $form = $(form); if (form[DWZ.pageInfo.pageNum]) form[DWZ.pageInfo.pageNum].value = 1; navTab.reload($form.attr('action'), {data: $form.serializeArray(), navTabId:navTabId}); return false; } /** * 处理dialog弹出层上的查询, 会又一次加载当前dialog * @param {Object} form */ function dialogSearch(form){ var $form = $(form); if (form[DWZ.pageInfo.pageNum]) form[DWZ.pageInfo.pageNum].value = 1; $.pdialog.reload($form.attr('action'), {data: $form.serializeArray()}); return false; } function dwzSearch(form, targetType){ if (targetType == "dialog") dialogSearch(form); else navTabSearch(form); return false; } /** * 处理div上的局部查询, 会又一次加载指定div * @param {Object} form */ function divSearch(form, rel){ var $form = $(form); if (form[DWZ.pageInfo.pageNum]) form[DWZ.pageInfo.pageNum].value = 1; if (rel) { var $box = $("#" + rel); $box.ajaxUrl({ type:"POST", url:$form.attr("action"), data: $form.serializeArray(), callback:function(){ $box.find("[layoutH]").layoutH(); } }); } return false; }
用法曾经两个为例:
form一般用this取代。callback指的是回调函数。能够自己定义,在js里面写一个callback(json)方法,回调时会调用,confirmMsg显示提示信息。
下面是我写的iframeCallback(form, callback)带文件上传的样例,可供參考:
<form id="canshuwwwwww" method="post" action="<%=basePath %>/sdsc/job/jobparamcreate" enctype="multipart/form-data" class="pageForm required-validate" onsubmit="return iframeCallback(this,callback);" > <!-- onsubmit="return iframeCallback(this);" return validateCallback(this,ajaxDone2); --> <table style="line-height:20px;margin-left:20px;"> <!-- input禁止输入 --> <tr> <td><label>选择參数:</label> <input id="softparamid" name="softwareparam.id" value="" type="hidden"/> <td><input id="softname" class="required" name="softwareparam.name" type="text" readonly="readonly" style="width:220px;" disabled="disabled"/> <td><strong><a id="find" href="" style="float:left;" class="btnLook" lookupgroup="softwareparam" title="选择软件參数" width="400" >查找带回</a></strong> <tr> <td><label>參数值:</label> <td><input id="paramvalue" type="text" name="paramvalue" class="required" size="20" maxlength="32" style="width:220px;" disabled="disabled"/> <tr> <td><label>文件:</label> <td><input id="uploadfile" name="uploadfile" type="file" /> <input id="softwareid" name="softwareid" value="" type="hidden"/> <input id="jobinfoid" name="jobinfoid" value="" type="hidden"/> <td><div class="buttonActive"><div class="buttonContent"><button type="submit" >确定</button></div></div> </table> </form>
function callback(json){ //alert(json.id); //提交參数的回调函数 //表格加入一行 newRow=document.all.yltable.insertRow(-1); j_1++; newRow.id=json.id; newcell=newRow.insertCell(); newcell.className="cell"; //newRow.bgColor='#FFFFFF'; newcell.align='center'; //newcell.innerHTML=""; newcell.innerHTML="第"+j_1+"行"; newcell=newRow.insertCell(); //newRow.bgColor='#FFFFFF'; newcell.className="cell"; newcell.align='center'; newcell.innerHTML=json.paramname; newcell=newRow.insertCell() ; //newRow.bgColor='#FFFFFF'; newcell.className="cell"; newcell.align='center'; //alert(document.getElementById("aaaaa")) //newcell.innerHTML='<input name="button3" type="button" onClick="delrow1()" value="删除1"> <input name="button3" type="button" onClick="delrow2()" value="删除2">'; newcell.innerHTML=json.paramvalue; newcell=newRow.insertCell() ; //newRow.bgColor='#FFFFFF'; newcell.className="cell"; newcell.align='center'; //newcell.innerHTML='<shiro:hasPermission name="dojob:delete"><a class="delete" target="ajaxTodo" href="/workbench/sdsc/job/jobparamdelete/'+json.id+'" title="确认要删除该參数?"><span>删除</span></a></shiro:hasPermission>'; newcell.innerHTML='<a title="确认要删除该參数?" onClick="submit('+json.id+')"><span>删除</span></a>'; document.all.yltable.focus(); } function submit(a){ $.ajax({ url : "/workbench/sdsc/job/jobparamdelete/"+a, data : { }, type:"post", dataType : 'json', success : function(data) { var table = document.getElementById('yltable'); document.getElementById(a).parentNode.removeChild(document.getElementById(a)); }, error : function() { alert("失败"); } }); }