2013-10-26 45 views
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; 
} 
+1

你應該給自己的答案作爲真正的答案,而不是編輯。 –

+0

您提供的答案正在工作,並且對按鈕 –

回答

1

這工作

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

進行了額外檢查,您的解決方案無法正常工作。它在4.2.2中使用了錯誤的事件。另外,如果您使用itemcontextmenu事件和項目單擊事件,則不需要檢查該按鈕。右鍵單擊事件和左鍵單擊事件。 –

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

這將允許取消當你右擊當前未選中的記錄。右鍵單擊選定的記錄將阻止取消選擇。

相關問題