如何防止右键单击行取消选择

时间:2013-10-26 15:29:18

标签: extjs extjs4.1

我有一个带有上下文菜单的网格。我知道如何防止右键单击行选择。我只是这样做:

var allowSelection=true;
Ext.getCmp('grid').on('beforeitemmousedown', function(grid, record, item, index, event, eOpts) { 
if (event.button==0) allowSelection=true ;
else allowSelection=false;
});
Ext.getCmp('grid').on('beforeselect', function(grid, record, index, eOpts) { 
 return allowSelection;
});

但我现在需要的是防止行取消选择。实际上,即使当前代码阻止了行选择,它也不会阻止行取消选择。

修改

我的右键单击事件会弹出一个上下文菜单。执行此操作的代码部分是这一部分

listeners:{
    itemcontextmenu:function(view,record,item,index,e){
      e.stopEvent();
      gridMenu.showAt(e.getXY());
    },
    containercontextmenu:function(view, e){
      e.stopEvent();
      gridMenu.showAt(e.getXY());
    }
...

此代码嵌套在网格的viewconfig中。所以,当我的上下文菜单弹出时,我只是不想触发行取消选择。

修改

嗯,我自己做了。刚刚添加return false

if (event.button==0) allowSelection=true ;
else {
  allowSelection=false;
  return false;
}

2 个答案:

答案 0 :(得分:1)

这有效

if (event.button==0) allowSelection=true ;
else {
  allowSelection=false;
  return false;
}

答案 1 :(得分:1)

这是通过使用布尔值作为标志的变量以及网格上的一些动作侦听器来完成的。确保默认情况下将标志设置为true,否则您最初无法选择任何内容。

allowDeselect: true,

接下来,在网格上添加三个动作的监听器,beforeitemclick,beforeitemcontextmenu,beforedeselect。

this.control({
  'yourGridPanelsXtype': {
    beforeitemclick: this.onBeforeItemClickTheXtype,
    beforeitemcontextmenu: this.onBeforeItemContextMenuTheXtype,
    beforedeselect: this.onBeforeDeselectTheXtype,
  }
});

和听众

/**
 * this action listener sets allow deselect for all left clicks. The context
 * menu.. right click, blocks this event. This means its safe to assume that
 * it always gets fired on only left clicks.
 */
onBeforeItemClickSchedulerList: function() {
    this.allowDeselect = true;
},

/**
 * action listener that gets triggered before we display the context menu.
 * This function checks to see if the record we triggered the event on is
 * selected or not. if it is selected block deselecting records, or if it's
 * not selected, allow selection of the new record. Mimics OS behavior. We
 * assume that this event is always triggered with a right click.
 * @param view is the view that fired the event; the grid
 * @param record is the record that triggered the event
 */
onBeforeItemContextMenuSchedulerList: function(view, record) {
  var grid = this.getReferenceToYourGridFromRefs(),
      sm = grid.getSelectionModel(),
      selected = selectionModel.isSelected(record);

  this.allowDeselect = !selected;
},

/**
 * action listener for before the grid deselects something. This listener
 * checks the allowDeselect variable to see if we are blocking deselects or
 * not.
 * @return a boolean of weather to allow deselection or not.
 */
onBeforeDeselectSchedulerList: function() {
  if (!this.allowDeselect) {
    return false;
  }
},

当您右键单击当前未选中的记录时,将允许取消选择。右键单击所选的记录将阻止取消选择。