如何让用户确认ExtJs中的组合框更改事件?

Omi*_*ati 6 javascript combobox extjs callback client-side

我的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',
    ...
    ...
    ...
    listeners: {
        beforeselect: function(combo, record, 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
                    }
                }
            });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是Ext.Msg.show获取回调函数而不是等待用户回答,我们无法阻止组合框更改.

我该怎么办?

小智 9

为了取消组合框更改,beforeSelect侦听器需要返回false.我的建议是:

beforeselect: function(combo, record, index ) {
  Ext.Msg.show({
    title: 'Warning',
    msg: 'Are You Sure ?',
    buttons: Ext.Msg.YESNO,
    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;
}
Run Code Online (Sandbox Code Playgroud)

ExtJS Modal不像本机对话框那样停止脚本的执行,这意味着beforeSelect监听器在用户操作之前返回.此代码的工作方式是立即停止select事件,并显示对话框.当用户选择"是"时,则在回调函数中以编程方式设置组合上的值.

  • 你知道`beforeselect`中的选定值,它是第二个参数(记录在上面的那个人的答案中).要获得该值,您可以使用`record.get([组合值字段]);` (4认同)