2011-05-20 107 views
1

我正在使用Olegsuggestion來使用beforeSelectRow事件來處理單擊在我的網格中的單元格。JQGrid:'beforeSelectRow'和'sortableRows' - 排除列可拖動?

奧列格在他的答案代碼(礦正是模仿):

你可以這樣定義以下

{ name: 'add', width: 18, sortable: false, search: false, 
formatter:function(){ 
    return "<span class='ui-icon ui-icon-plus'></span>" 
}} 

在上面的代碼我使用自定義的按鈕列jqGrid的格式化程序,但沒有任何事件綁定。的

beforeSelectRow: function (rowid, e) { 
     var iCol = $.jgrid.getCellIndex(e.target); 
     if (iCol >= firstButtonColumnIndex) { 
     alert("rowid="+rowid+"\nButton name: "+buttonNames[iCol]); 
    } 

    // prevent row selection if one click on the button 
    return (iCol >= firstButtonColumnIndex)? false: true; 
} 

其中firstButtonColumnIndex = 8buttonNames = {8:'Add',9:'Edit',10:'Remove',11:'Details'}代碼。在您的代碼中,您可以將警報替換爲相應的函數調用。

問題是我的網格也是可排序的 - (我在網格上使用了sortableRows方法)。如果用戶點擊單元格時即使移動鼠標,也不會觸發事件(可排序的事件)。

這在大多數情況下是可取的 - 但是,我認爲可以解決這個問題的方法是以某種方式將列從「句柄」中排除以拖動(排序)行,並讓我的事件在這些列上觸發。我似乎無法弄清楚如何做到這一點!任何幫助非常感謝:)

回答

2

,如果你添加下面的附加代碼

var grid = $('#list'), tbody = $("tbody:first",grid[0]), ptr, td; 
grid.bind('mouseover',function(e) { 
    var iCol = $.jgrid.getCellIndex(e.target); 
    if (iCol >= firstButtonColumnIndex) { 
     tbody.sortable("disable"); 
    } else { 
     tbody.sortable("enable"); 
    } 
}); 

您可以修復該問題的代碼將被禁用的jqGrid的排序功能,如果鼠標會在動作的按鈕。因此,您只能在另一列對行進行排序。

您可以看到修改的演示here

+0

這完美的作品!再次感謝你)! – icats 2011-05-20 22:04:20

+0

@icats:不客氣! – Oleg 2011-05-21 06:02:39