我有一个选项卡,用户必须在其中输入信息,并且我想在用户尝试更改选项卡时验证数据。如果数据输入不完整,我会提示用户验证他是否要离开。
我在标签面板上添加了监听器activeitemchange:,可以提示用户。但是,Ext.Msg 似乎并没有阻止方法调用,而且我总是从 activeitemchange 中“返回 true”。
如何更改代码,以便仅在用户单击“是”时更改选项卡?
Ext.Loader.setConfig({enabled: true});
Ext.application({
name: "Sencha",
launch: function () {
var tabPanel = Ext.create('Ext.tab.Panel', {
layout: 'card',
tabBarPosition: 'bottom',
listeners: {
activeitemchange: function (me, value, oldValue, eOpts) {
console.log("activeitemchange");
var oldTabIdx = me.getInnerItems().indexOf(oldValue);
var newTabIdx = me.getInnerItems().indexOf(me.getActiveItem());
console.log(oldTabIdx + " => " + newTabIdx);
if (oldTabIdx == 2) {
// me.getTabBar().setActiveItem(2);
Ext.Msg.confirm("Leave Screen?", "Are you sure you?", function (response) {
console.log(response);
if (response === 'no') {
console.log("User says stay.");
return false;
} else {
console.log("User says leave.");
return true;
}
});
}
// Always returns true because Ext.Msg.confirm doesn't block
return true; // false prevents tab change
}
},
items: [
{
id: 'tab1',
title: 'Home',
layout: 'fit',
xtype: 'container',
iconCls: 'home',
items: [
{html: 'page #1'}
]
},
{
id: 'tab2',
title: 'Two',
layout: 'fit',
xtype: 'container',
iconCls: 'star',
items: [
{html: 'page #2'}
]
},
{
id: 'tab3',
title: 'three',
layout: 'fit',
xtype: 'container',
iconCls: 'team',
items: [
{html: 'page #3'}
]
}
]
});
Ext.Viewport.add(tabPanel);
}
});
尝试修复:
Ext.Loader.setConfig({enabled: true});
Ext.application({
name: "Sencha",
launch: function () {
var tabPanel = Ext.create('Ext.tab.Panel', {
layout: 'card',
tabBarPosition: 'bottom',
confirm: true,
listeners: {
activeitemchange: {
order: 'before',
fn: function (list, value, oldValue, eOpts) {
var me = this;
console.log(me.getConfirm()); // ERROR: getConfirm() is undefined
var oldTabIdx = me.getInnerItems().indexOf(oldValue);
var newTabIdx = me.getInnerItems().indexOf(value);
console.log(oldTabIdx + " => " + newTabIdx);
if (oldTabIdx == 2 && me.getConfirm()) {
Ext.Msg.confirm("Leave Screen?", "Are you sure you?", function (response) {
console.log(response);
if (response === 'no') {
console.log("User says stay.");
} else {
console.log("User says leave.");
me.setConfirm(false);
me.setActiveItem(newTabIdx);
}
});
return false;
} else {
me.setConfirm(true);
}
}
}
},
items: [
{
id: 'tab1',
title: 'Home',
layout: 'fit',
xtype: 'container',
iconCls: 'home',
items: [
{html: 'page #1'}
]
},
{
id: 'tab2',
title: 'Two',
layout: 'fit',
xtype: 'container',
iconCls: 'star',
items: [
{html: 'page #2'}
]
},
{
id: 'tab3',
title: 'three',
layout: 'fit',
xtype: 'container',
iconCls: 'team',
items: [
{html: 'page #3'}
]
}
]
});
Ext.Viewport.add(tabPanel);
}
});