方法1:此方法适用于Extjs按钮handler处理,一般在最前面说明
function changeMsgBoxFocus(buttonIdx){ function exmyth() { Ext.MessageBox.getDialog().buttons[buttonIdx].focus(); } exmyth.defer(100, this);}Ext.MessageBox.getDialog().addListener('show', changeMsgBoxFocus.createCallback(2));
Ext.MessageBox.buttonText.yes = 'Remove';Ext.MessageBox.buttonText.no = 'Do nothing';Ext.MessageBox.getDialog().on({ show: function() { Ext.MessageBox.getDialog().buttons[2].focus(); }, single:true //,delay:50 // maybe needed});Ext.MessageBox.show({ title : 'Please confirm', msg : 'Do your really want to delete node \'' + this.curNode.text + '\'?', buttons : Ext.MessageBox.YESNO, fn : this.removeNodeConfirmHandleMsgBox, scope: this, animEl : this.tree, icon : Ext.MessageBox.QUESTION});
方法2:此方法适用于页面全局,一般在页面return的init方法中指定
Ext.MessageBox.getDialog().defaultButton = 2;
附上我项目模块的代码
1.Smsd.widget.AddressGroupBtn.js
Ext.ns("Smsd.widget");var mgrAddrGroupForm;var mgrAddrGroupWin;var type;var groupTree;var root;var loader;/** * 管理组 * @author zhongmm * @class Smsd.widget.AddressGroupBtn * @extends Ext.Button */Smsd.widget.AddressGroupBtn =Ext.extend(Ext.Button,{ text: "管理组", width: 50, back: function (){}, handler: function (btn) { function changeMsgBoxFocus(buttonIdx){ function exmyth() { Ext.MessageBox.getDialog().buttons[buttonIdx].focus(); } exmyth.defer(100, this); } Ext.MessageBox.getDialog().addListener('show', changeMsgBoxFocus.createCallback(2)); root = new Ext.tree.AsyncTreeNode( {// href : '',// hrefTarget : '_blank',// iconCls : "me-iconCls", id : "0", text : "个性化通讯录", leaf:false }); loader = new Ext.tree.TreeLoader( { dataUrl:Ext.sysd.getWithToken('/addressGroup/listAddressGroup.do'), method : 'POST' }); groupTree = new Ext.tree.TreePanel( { id : 'groupId', name : 'groupName', root : root, animate : false, containerScroll : true, autoScroll : true, loader:loader, width: 325, height: 200, line: true, border : true }); root.expand(false, true); //默认展开一级 var topBtn = new Ext.Toolbar({ autoHeight : true, items : [{xtype : 'tbseparator'}, {text : '新增',iconCls : 'silk_page_add',handler :function(){mgrAddrGroup(0);}}, {xtype : 'tbseparator'}, {text : '编辑',iconCls : 'silk_page_edit',handler :function(){ var selNode = groupTree.getSelectionModel().getSelectedNode(); if(!selNode) { Ext.Msg.alert(Sysd.Msg.AlertTitle,"请选择一条组信息!"); } else if(selNode.id == groupTree.getRootNode().id) { Ext.Msg.alert(Sysd.Msg.AlertTitle,"无法编辑根节点!"); } else { mgrAddrGroup(1); Ext.getCmp("groupId").setValue(selNode.id); Ext.getCmp("groupName").setValue(selNode.text); } }}, {xtype : 'tbseparator'}, {text : '删除',iconCls : 'silk_page_delete', handler :function(){ var selNode = groupTree.getSelectionModel().getSelectedNode(); if(!selNode) { Ext.Msg.alert(Sysd.Msg.AlertTitle,"请选择一条组信息!"); } else if(selNode.id == groupTree.getRootNode().id) { Ext.Msg.alert(Sysd.Msg.AlertTitle,"无法删除根节点!"); } else { countAddressGroup(selNode.id); } }}, {xtype : 'tbseparator'}, {text : '返回',iconCls : 'silk_application_home',handler :function(){win.close();btn.back()}}, {xtype : 'tbseparator'}, '->', new Ext.sysd.Helper({ moduleCode : '100600' }) ] }); //组管理主窗口 var win=new Ext.Window({ tbar:topBtn, title:'管理组', width:340, height:258, closable:false, layout:'column', modal : true, resizable:false,// draggable:false, closable:true, items:[groupTree]}); win.show(); //数据检验 function confirmGroupName() { var groupName = Ext.getCmp("groupName").getValue(); if("" == groupName) { Ext.Msg.alert(Sysd.Msg.AlertTitle, "请输入组名!"); return false; } else if(20 < groupName.length) { Ext.Msg.alert(Sysd.Msg.AlertTitle, "组名过长,不能大于20个字符,请重新输入!"); return false; } return true; } //新增,编辑组信息,成功刷新组管理树 function saveUpdateGroupName(type) { var groupId = Ext.getCmp("groupId").getValue(); var groupName = Ext.getCmp("groupName").getValue(); if(0 == type) { Ext.Ajax.request({ url : Ext.sysd.getWithToken('/addressGroup/saveAddressGroup.do'), method : 'post', params : { groupName:groupName }, success : function(response) { Ext.Msg.alert(Sysd.Msg.AlertTitle, "保存成功!"); loader.load(root,function(){ root.expand(false, true); }); Ext.getCmp("mgrAddrGroupWin").close(); }, failure : function(response) { Ext.Msg.alert(Sysd.Msg.AlertTitle, "保存失败!"); } }); } else if(1 == type) { Ext.Ajax.request({ url : Ext.sysd.getWithToken('/addressGroup/updateAddressGroup.do'), method : 'post', params : { groupId : groupId, groupName:groupName }, success : function(response) { Ext.Msg.alert(Sysd.Msg.AlertTitle, "编辑成功!"); loader.load(root,function(){ root.expand(false, true); }); Ext.getCmp("mgrAddrGroupWin").close(); }, failure : function(response) { Ext.Msg.alert(Sysd.Msg.AlertTitle, "编辑失败!"); } }); } } //新增,编辑组信息弹出窗口 function mgrAddrGroup(type) { mgrAddrGroupForm = new Ext.form.FormPanel({ width:200, height:100, frame:true, labelWidth:50, labelAlign:"right", defaultType:"textfield", items:[ {id:"groupId", name:"groupId",fieldLabel:"",hidden:true,hideLabel:true}, {id:"groupName", name:"groupName",fieldLabel:"组名",width:100,allowBlank:false}], buttons:[{text:"确认",iconCls : 'silk_tick',handler:function(){ var groupName = Ext.getCmp("groupName").getValue(); var selNode = groupTree.getSelectionModel().getSelectedNode(); if(1 == type && groupName == selNode.text) { mgrAddrGroupWin.hide(); } else { countAddressGroupByName(groupName,type); } }},{text:"取消",iconCls:'silk_cancel',handler:function(){ var groupName = Ext.getCmp("groupName").getValue(); var selNode = groupTree.getSelectionModel().getSelectedNode(); if(0 == type && "" != groupName) { //组名未保存,确定取消? Ext.Msg.confirm(Sysd.Msg.AlertTitle,"是否确定要取消,取消将不保存数据?",function(btn) { if('yes' == btn) { mgrAddrGroupWin.hide(); return true; } else { return false; } }); } else if(1 == type && "" != groupName && groupName != selNode.text) { //组名已修改,确定取消? Ext.Msg.confirm(Sysd.Msg.AlertTitle,"是否确定要取消,取消将不保存数据?",function(btn) { if('yes' == btn) { mgrAddrGroupWin.hide(); return true; } else { return false; } }); } else if(1 == type && groupName == selNode.text) { mgrAddrGroupWin.hide(); } else { mgrAddrGroupWin.hide(); } }}] }) mgrAddrGroupWin = new Ext.Window( { id:"mgrAddrGroupWin", title:0 == type?"新增组信息":"编辑组信息", width:210, height:150, frame:true, labelWidth:60, labelAlign:"right", defaultType:"textfield", autoHeight: true, resizable:false, closable:false, // draggable:false, modal:true, items:[mgrAddrGroupForm] }); mgrAddrGroupWin.on("hide",function(){mgrAddrGroupWin.destroy()}); mgrAddrGroupWin.show(); } //统计组中联系人信息 function countAddressGroup(groupId) { Ext.Ajax.request ({ url : Ext.sysd.getWithToken('/addressGroup/countAddressGroup.do'), method : 'post', params : { groupId:groupId }, failure : function(){ Ext.Msg.alert(Sysd.Msg.AlertTitle,"获取数据失败!"); return false; }, success : function(response){ var count = Ext.decode(response.responseText); if(count>0) { Ext.Msg.alert(Sysd.Msg.AlertTitle, "该组有联系人信息,不能删除!"); return false; } else { comfirmDelete(); return true; } } }); return true; } //统计同名组数 function countAddressGroupByName(groupName,type) { Ext.Ajax.request ({ url : Ext.sysd.getWithToken('/addressGroup/countAddressGroupByName.do'), method : 'post', params : { groupName:groupName }, failure : function(){ Ext.Msg.alert(Sysd.Msg.AlertTitle,"获取数据失败!"); return false; }, success : function(response){ var count = Ext.decode(response.responseText); if(count>0) { Ext.Msg.alert(Sysd.Msg.AlertTitle, "已经存在此组名,请重新输入!"); return false; } else { if(confirmGroupName()) { saveUpdateGroupName(type); } } } }); return true; } function comfirmDelete() { var selNode = groupTree.getSelectionModel().getSelectedNode(); Ext.Msg.confirm(Sysd.Msg.AlertTitle,"是否删除该组信息?",function(btn) { if('yes' == btn) { deleteAddressGroup(selNode.id); return true; } else { return false; } }); } //删除组信息 function deleteAddressGroup(groupId) { Ext.Ajax.request({ url : Ext.sysd.getWithToken('/addressGroup/deleteAddressGroup.do'), method : 'post', params : { groupId:groupId }, success : function(response) { Ext.Msg.alert(Sysd.Msg.AlertTitle, "删除成功!"); loader.load(root,function(){ root.expand(false, true); }); }, failure : function(response) { Ext.Msg.alert(Sysd.Msg.AlertTitle, "删除成功!"); } }); } }});Ext.reg('addressGroupBtn', Smsd.widget.AddressGroupBtn);//管理组
2.sms.appSetup.js
Ext.ns("sms.appSetup");var editLevelForm;var gridDirect;var formDirect;var winDirect;var editLevelWin;var gridMain;var storeMain;var timeBlock;var editTimeForm;var editTimeWin;var seqNum = 0; //序号递增初始值var idArr; //业务代码var timeStartArr; //已选择记录的起始时间数组var timeEndArr //已选择记录的截止时间数组var count; //已选择记录的时间段数var iniCount;/** * 业务应用设置 * @author zhongmm */sms.appSetup.main = function(){ var storeMain = new Ext.data.JsonStore({ url : Ext.sysd.getWithToken("/smsAppSetup/listAppSetup.do"), root : '', totalProperty : 'totalCount', autoLoad : false, fields : [{name:'SEQ_CODE',type:'string'}, {name:'BUSINESS_NAME',type:'string'}, {name:'SMS_LEVEL',type:'string'}, {name:'SMS_LEVEL_BAK',type:'string'}, {name:'TIME_START',type:'string'}, {name:'TIME_END',type:'string'}, {name:'BUSINESS_CODE',type:'string'}, {name:'BUSINESS_NAME_BAK',type:'string'} ] }); function setCenter(v) { return "" + v + ""; } var tb = new Ext.Toolbar({ autoHeight : true, items : ['-', { text : '级别编辑', iconCls : 'silk_page_edit', handler : function() { if(hasSelected()) { editLevel(); var businessCode = String(gridMain.getSelectionModel().getSelected().get("BUSINESS_CODE")); var businessName = String(gridMain.getSelectionModel().getSelected().get("BUSINESS_NAME_BAK")); var smsLevel = String(gridMain.getSelectionModel().getSelected().get("SMS_LEVEL_BAK")); Ext.getCmp("businessCode").setValue(businessCode); Ext.getCmp("businessName").setValue(businessName); Ext.getCmp("smsLevel").setValue(smsLevel); } } }, { xtype : 'tbseparator' }, { text : '发送时间编辑', iconCls : 'silk_page_edit', handler : function() { if(hasSelected()) { var timeBusinessCode = String(gridMain.getSelectionModel().getSelected().get("BUSINESS_CODE")); var timeBusinessName = String(gridMain.getSelectionModel().getSelected().get("BUSINESS_NAME_BAK")); var recordCount = gridMain.getStore().getCount(); timeStartArr=new Array(); timeEndArr=new Array(); for(var i = 0; i < recordCount; i++) { var id = gridMain.getStore().getAt(i).get("BUSINESS_CODE"); if(timeBusinessCode == id) { var timeStartValue = gridMain.getStore().getAt(i).get("TIME_START"); var timeEndValue = gridMain.getStore().getAt(i).get("TIME_END"); timeStartArr.push(timeStartValue); timeEndArr.push(timeEndValue); } } count = timeStartArr.length > timeEndArr.length ? timeStartArr.length :timeEndArr.length; iniCount = count; editTime(); for(var i = 0; i < count; i++) { Ext.getCmp("timeStart" + i ).setValue(timeStartArr[i]); Ext.getCmp("timeEnd" + i ).setValue(timeEndArr[i]); } Ext.getCmp("timeBusinessCode").setValue(timeBusinessCode); Ext.getCmp("timeBusinessName").setValue(timeBusinessName); } } },{ xtype : 'tbseparator' },{ text : '级别说明', iconCls : 'silk_application_view_detail', handler : function() { directLevel(); } },{ xtype : 'tbseparator' }, '->', new Ext.sysd.Update({ moduleCode : '500100' }), '-', new Ext.sysd.Suggest({ moduleCode : '500100' }), '-', new Ext.sysd.Helper({ moduleCode : '500100' }) ] }); gridMain=new Ext.grid.GridPanel({ title:"业务应用设置", tbar:tb, region:'center', height:240, store:storeMain, stripeRows : true, autoScroll : true, enableHdMenu:false, columns:[ { header : setCenter('序号'), align:'center', dataIndex : 'SEQ_CODE', width : 60, sortable : false },{ header : setCenter('业务应用'), dataIndex : 'BUSINESS_NAME', align:'center', width : 100, sortable : false },{ header : setCenter('短信级别'), dataIndex : 'SMS_LEVEL', align:'center', width : 100, sortable : false },{ header : setCenter('允许发送起始时间'), dataIndex : 'TIME_START', align:'center', width : 100, sortable : false },{ header : setCenter('允许发送截止时间'), align:'center', dataIndex : 'TIME_END', width : 100, sortable : false }], loadMask:{msg:"请稍等..."}, viewConfig : { forceFit : true } }); //合并单元格 gridMain.store.on("load",function(){ initSeq(); gridSpan(gridMain,"row","[SEQ_CODE],[BUSINESS_NAME],[SMS_LEVEL]","BUSINESS_NAME"); }); function createWindow(){ var mainWin = new Ext.Viewport({ title : '业务应用设置', layout : 'border', defauls : { column : 1 }, closable : false, draggable : false, resizable : false, items : [gridMain] }); mainWin.show(); } return { init : function() { Ext.MessageBox.getDialog().defaultButton = 2; createWindow(); gridMain.getStore().reload( /*{ params: { //默认显示第一页记录 start:0, limit:Ext.sysd.getPageSize() } }*/); } }}();//选择记录判断function hasSelected(){ var selectModel = gridMain.getSelectionModel(); var selected = selectModel.hasSelection(); if(!selected) { Ext.Msg.alert(Sysd.Msg.AlertTitle,'请先选择一条记录再编辑!'); return false; } return true;}//数据检验function confirmBusinessName(){ var groupName = Ext.getCmp("groupName").getValue(); if("" == groupName) { Ext.Msg.alert(Sysd.Msg.AlertTitle, "请输入组名!"); return false; } return true;}function setCenter(v) { return "" + v + "";} function editLevel(){ //发送状态 1成功 0 失败(根据回执判断是否失败) var arrData = [['0','0'], ['1','1'], ['2', '2'], ['3', '3'], ['4', '4']]; editLevelForm = new Ext.form.FormPanel({ width:200, height:100, frame:true, labelWidth:60, labelAlign:"right", buttonAlign:'center', defaultType:"textfield", items:[ {id:"businessName", name:"businessName",fieldLabel:"业务应用",width:100,disabled:true}, {fieldWith:60,fieldLabel : "级别",arrData:arrData,xtype:"arrayComboBox", id : 'smsLevel',name : 'smsLevel',width : 100}, {id:"businessCode", name:"businessCode",hideLabel:true,hidden:true}], buttons:[{text:"确认",iconCls : 'silk_tick',handler:function(){ updateLevel(); }}, {text:"取消",iconCls:'silk_cancel',handler:function(){editLevelWin.hide()}}] }); editLevelWin = new Ext.Window( { id:"editLevelWin", title:"编辑优先级别", width:210, height:132,// autoHeight: true, resizable:false, modal:true, items:[editLevelForm] }); editLevelWin.on("hide",function(){editLevelWin.destroy()}); editLevelWin.show();}function updateLevel(){ var businessCode = Ext.getCmp("businessCode").getValue(); var smsLevel = Ext.getCmp("smsLevel").getValue(); Ext.Ajax.request({ url : Ext.sysd.getWithToken('/smsAppSetup/updateAppSetup.do'), method : 'post', params : { businessCode:businessCode, smsLevel:smsLevel }, success : function(response) { Ext.Msg.alert(Sysd.Msg.AlertTitle, "保存成功!");// seqNum = 0; gridMain.getStore().reload( /*{ params: { //默认显示第一页记录 start:0, limit:Ext.sysd.getPageSize() } }*/); Ext.getCmp("editLevelWin").close(); }, failure : function(response) { Ext.Msg.alert(Sysd.Msg.AlertTitle, "保存失败!"); } });}//时间交错判断function comfirmTimePeriod(){ var timePeriodStartStr=""; var timePeriodEndStr=""; for(var i = 0; i < count; i++) { var timeStartCtrl = editTimeForm.getForm().findField("timeStart" + i); var timeEndCtrl = editTimeForm.getForm().findField("timeEnd" + i); if(timeStartCtrl) { timePeriodStartStr += timeStartCtrl.getValue()+","; timePeriodEndStr += timeEndCtrl.getValue()+","; } } timePeriodStartStr=timePeriodStartStr.substr(0,timePeriodStartStr.length-1); timePeriodEndStr=timePeriodEndStr.substr(0,timePeriodEndStr.length-1); var timePeriodStartArr = timePeriodStartStr.split(','); var timePeriodEndArr = timePeriodEndStr.split(','); var periodFlag = false;//默认不存在时间交错情况 for(var i = 0; i< timePeriodStartArr.length; i++) { for(var j = 0; j< timePeriodEndArr.length; j++) { if(i == j) { continue; } else { if((timePeriodStartArr[j] < timePeriodStartArr[i])&&(timePeriodStartArr[i]0) { seqOldValue = arraySep[index1 - 1]; seqCurValue = arraySep[index1]; } if (allRecs[index1].get(colName) == preValue && (colName == sepCol || seqOldValue == seqCurValue)) { //alert(colName + "======" + seqOldValue + "======" + seqCurValue); allRecs[index1].set(colName, " "); array1[i].push(j); if (j == count2 - 1) { //var index = firstSameCell + Math.round((j + 1 - firstSameCell) / 2 - 1); var index = firstSameCell;//首行显示数值 if (rowOrCol == "row") { allRecs[index].set(colName, preValue); } else { allRecs[index1].set(grid.getColumnModel().getColumnId(index), preValue); } } } else { if (j != 0) { //var index = firstSameCell + Math.round((j + 1 - firstSameCell) / 2 - 1); var index = firstSameCell;//首行显示数值 if (rowOrCol == "row") { allRecs[index].set(colName, preValue); } else { allRecs[index1].set(grid.getColumnModel().getColumnId(index), preValue); } } firstSameCell = j; preValue = allRecs[index1].get(colName); allRecs[index1].set(colName, " "); if (j == count2 - 1) { allRecs[index1].set(colName, preValue); } } } } grid.getStore().commitChanges(); //添加所有分隔线 var rCount = grid.getStore().getCount(); for (i = 0; i < rCount; i ++) { for (j = 0; j < grid.getColumnModel().getColumnCount(); j ++) { aRow = grid.getView().getCell(i,j); if (i == 0) { aRow.style.borderTop = "none"; aRow.style.borderLeft = "1px solid #ccc"; } else if (i == rCount - 1) { aRow.style.borderTop = "1px solid #ccc"; aRow.style.borderLeft = "1px solid #ccc"; aRow.style.borderBottom = "1px solid #ccc"; } else { aRow.style.borderTop = "1px solid #ccc"; aRow.style.borderLeft = "1px solid #ccc"; } if(j == grid.getColumnModel().getColumnCount()-1) aRow.style.borderRight = "1px solid #ccc"; if(i == rCount-1) aRow.style.borderBottom = "1px solid #ccc"; //aRow.style.borderBottom = "1px solid #ccc"; } } //去除合并的单元格的分隔线 for (i = 0; i < array1.length; i++) { for (j = 0; j < array1[i].length; j++) { if (rowOrCol == "row") { aRow = grid.getView().getCell(array1[i][j],i); aRow.style.borderTop = "none"; } else { aRow = grid.getView().getCell(i, array1[i][j]); aRow.style.borderLeft = "none"; } } }}Ext.onReady(sms.appSetup.main.init,sms.appSetup.main);