前言:由於項目是接手維護,客戶需要把彈窗的高度調大一些,當時還不熟悉整個項目的結構,於是找度娘,發現並沒有完整的解決方案,自己琢磨了很久才搞明白這個彈窗是怎麼一回事。(如果只想知道怎麼修改彈窗大小可以直接看第3和第4點內容)
1.根據官方說明,實現彈窗功能的核心代碼如下
$.dialog({
content: 'url:'+addurl,
lock : true,
//zIndex:1990,
width:width,
height:height,
title:title,
opacity : 0.3,
cache:false,
ok: function(){
iframe = this.iframe.contentWindow;
saveObj();
return false;
},
cancelVal: '關閉',
cancel: true /*爲true等價於function(){}*/
})
官方說明如圖所示
鏈接:官方說明
2.根據核心代碼在curdtools.js中編寫一個函數
/**
* 創建添加或編輯窗口
*
* @param title
* @param addurl
* @param saveurl
*/
function createwindow(title, addurl,width,height) {
width = width?width:800;
height = height?height:500;
if(width=="100%" || height=="100%"){
width = document.body.offsetWidth;
height =document.body.offsetHeight-100;
}
if(typeof(windowapi) == 'undefined'){
$.dialog({
content: 'url:'+addurl,
lock : true,
width:width,
height:height,
title:title,
opacity : 0.3,
cache:false,
ok: function(){
iframe = this.iframe.contentWindow;
saveObj();
return false;
},
cancelVal: '關閉',
cancel: true /*爲true等價於function(){}*/
});
}else{
W.$.dialog({
content: 'url:'+addurl,
lock : true,
width:width,
height:height,
parent:windowapi,
title:title,
opacity : 0.3,
cache:false,
ok: function(){
iframe = this.iframe.contentWindow;
saveObj();
return false;
},
cancelVal: '關閉',
cancel: true /*爲true等價於function(){}*/
});
}
}
3.在curdtools.js中編寫一個調用函數createwindow的函數,在該函數中指定彈窗的大小
/*彈窗大小修改 */
function ritualUpdate(title,url,id,width,height) {
/* 此處爲業務代碼,我這裏的代碼:選定一條記錄,並修改該記錄,tip爲另一個函數
gridname=id;
var rowsData = $('#'+id).datagrid('getSelections');
if (!rowsData || rowsData.length==0) {
tip('請選擇編輯項目');
return;
}
if (rowsData.length>1) {
tip('請選擇一條記錄再編輯');
return;
}
*/
url += '&id='+rowsData[0].id;
openwindow(title,url,'',1300,640);
}
4.設置一個按鈕或鏈接,點擊後可彈出彈窗,在該按鈕中調用ritualUpdate函數(不需要填入參數)
<t:dgToolBar title="修改XXX記錄" icon="icon-search" url="honoredbirthController.do?myUpdate" funname="ritualUpdate"></t:dgToolBar>
請求路徑是指該按鈕(鏈接)的請求路徑,它一般是向controller請求一些數據,controller響應後返回的頁面就會在彈窗中顯示,這裏的controller的代碼與一般的controller一樣,區別在於,controller返回的視圖將會在彈窗中顯示。
5.總結
我個人理解,整個喚起彈窗的過程大概爲:
爲需要喚起彈窗的按鈕(鏈接)添加標籤,在標籤體中調用能夠喚起彈窗的函數(我這裏是通過ritualUpdate間接調用喚起彈窗的函數createwindow,目的是比較靈活,爲不同尺寸的彈窗編寫不同的函數,然後調用)。