有一个下拉框. 期望是当触发下拉事件的时候, 给出提示: 真的要切换吗?
用户选择是, 切换; 否则, 不与切换.
参考API, 得到:
[quote]
beforeselect : ( Ext.form.ComboBox combo, Ext.data.Record record, Number index )
Fires before a list item is selected. Return false to cancel the selection.
[/quote]
OK.
于是我的代码如下:
[code="js"]
Ext.onReady(function(){
var comboBox = new Ext.form.ComboBox({
store:new Ext.data.SimpleStore({
fields: ['name']
,data: [['A'], ['B']]
}),
valueField: 'name', displayField: 'name',
editable: false,forceSelection: true,mode:'local',
blankText:'选择', emptyText:'选择' ,triggerAction: 'all'
,listeners:{
'beforeselect':function(combo, record, index){
// confirm不起作用
Ext.MessageBox.confirm("提示","确定要切换?",
function(e){
if('yes' == e){
return true;
}else{
return false;
}
}
);
// return false; // 难道不能与用户交互?
}
}
});
comboBox.render(document.body);
});
[/code]
在beforeselect中有一个confirm与客户进行交互.
[color=red]实际效果却是, 当触发beforeselect事件, 出现confirm等待用户抉择是还是否的时候.
下拉框的值已经改变了.[/color]
Ext.MessageBox.只是模仿原生的浏览器警告框,它没办法提供真实的原生警告框的中断(或者说等待用户反馈)功能。你可以在Ext.MessageBox.语句后加一句alert,测试一下:结果是Ext.MessageBox.confirm执行后,alert跟着就执行而不会等待confirm的反馈结果。
我不确定beforeselect有没有效果,不过能确定你上面的写法是有问题的。
因为return false退出function这层,但是并没有给beforeselect这个事件返回false。
改一下:
[code="js"] 'beforeselect':function(combo, record, index){
var r;
// confirm不起作用
Ext.MessageBox.confirm("提示","确定要切换?",
function(e){
r = e;
}
);
if('yes' == r){
return true;
}else{
return false;
}
}[/code]
[code="js"] 'beforeselect':function(combo, record, index){
var r;
// confirm不起作用
Ext.MessageBox.confirm("提示","确定要切换?",
function(e){
r = e;
}
);
if('yes' == r){
return true;
}else{
return false;
}
}[/code]
[code="javascript"]
'beforeselect':function(combo, record, index){
return window.confirm("确定要切换?");
}
[/code]