我的extjs应用程序中有一个组合,我想显示’你是吗?
当然?’如果用户说不,则向用户确认窗口并防止更改.
当然?’如果用户说不,则向用户确认窗口并防止更改.
由于JavaScript的确认框是同步的,因此它可以正常工作.但是使用Ext JS,会显示确认消息,我的其余代码将在用户响应之前执行.这是我的代码:
// JavaScript confirm Box { xtype: 'combo',... ... ... listeners: { beforeselect: function(combo,record,index ) { if(confirm('Are you sure ?') == false) { return false; // prevent combo from changing } // else continue } } } // Ext JS message Box (to confirm) { xtype: 'combo',index ) { Ext.Msg.show({ title: 'Warning',msg: 'Are You Sure ?',buttons: Ext.Msg.YESNO,fn: function(btn) { if (btn == 'yes') { // continue and set new value on combo } else if (btn == 'no') { // prevent combo from changing } } }); } } }
问题是Ext.Msg.show获得了一个回调函数,并没有等待用户回答,我们无法阻止组合框更改.
我该怎么办?
解决方法
为了取消组合框更改,beforeSelect侦听器需要返回false.我的建议是:
beforeselect: function(combo,index ) { Ext.Msg.show({ title: 'Warning',fn: function(btn) { if (btn == 'yes') { // Here we have to manually set the combo value // since the original select event was cancelled combo.setValue( /* whatever value was selected */ ); } else if (btn == 'no') { // Don't do anything because the select event was already cancelled } } }); // Cancel the default action return false; }
ExtJS Modal不像本机对话框那样暂停脚本的执行,这意味着beforeSelect侦听器在用户操作之前返回.此代码的工作方式是立即停止select事件,并显示对话框.当用户选择“是”时,则在回调函数中以编程方式设置组合上的值.