2

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

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;
}
4

2 回答 2

1

这有效

if (event.button==0) allowSelection=true ;
else {
  allowSelection=false;
  return false;
}
于 2013-11-09T09:29:20.770 回答
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;
  }
},

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

于 2015-03-20T14:58:30.180 回答