2010-05-19 112 views

回答

7

我建議您使用如此命名的「內聯編輯」進行行編輯。這種方法的最大優點,就是它非常直觀和用戶。您可以在演示頁面http://trirand.com/blog/jqgrid/jqgrid.html上看到它的工作原理。在此演示中選擇「行編輯」,然後在左側樹部分選擇「使用事件」或「輸入類型」。使用這種方法,您可以實現任何自定義驗證,無論是否允許在事件句柄onSelectRowondblClickRow內編輯所選行。如果你允許編輯,那麼你可以調用jqGrid的editRow方法。此方法爲所有可編輯的列創建輸入控件,並且用戶可以以自然的方式修改行值。如果用戶按「enter」鍵或取消「esc」鍵,修改將被保存。

我個人更喜歡在ondblClickRow事件處理程序中實現調用editRow方法。因此,用戶可以像往常一樣繼續選擇行,並可以使用雙擊進行行編輯。僞代碼看起來像下面這樣:

var lastSel = -1; 
var isRowEditable = function (id) { 
    // implement your criteria here 
    return true; 
}; 
var grid = jQuery('#list').jqGrid({ 
    // ... 
    ondblClickRow: function(id, ri, ci) { 
     if (isRowEditable(id)) { 
      // edit the row and save it on press "enter" key 
      grid.jqGrid('editRow',id,true); 
     } 
    }, 
    onSelectRow: function(id) { 
     if (id && id !== lastSel) { 
      // cancel editing of the previous selected row if it was in editing state. 
      // jqGrid hold intern savedRow array inside of jqGrid object, 
      // so it is safe to call restoreRow method with any id parameter 
      // if jqGrid not in editing state 
      grid.jqGrid('restoreRow',lastSel); 
      lastSel = id; 
     } 
    }, 
    pager: '#pager' 
}).jqGrid('navGrid','#pager',{edit:false}); 
+0

感謝您的詳細輸入。聽起來像一個合理的解決方案。 – 2010-05-20 06:29:54

+0

@Oleg:這仍然允許行內聯和表單編輯。如何防止內聯和形式編輯禁用的行也? – Andrus 2012-07-31 14:12:38

2

您可以在邏輯上做到這一點。你必須有一些單元格的標準,一些單元格可以編輯,有些則不可以。

我已經實施它行明智。

爲jqgrid創建XML時,請爲每行提供一些id。

基於這些id,您可以使用jqgrid方法使這些行的單元格可編輯或不可編輯。

下面是beforeEditCell方法:

beforeEditCell: function(rowid, cellname, value, iRow, iCol) { 
    // here identify row based on rowid 
    // if the row should not be editable than simply make the cells noneditable using 
    editCell(iRow, iCol, false); 
    jQuery(gridid).jqGrid("restoreCell",iRow,iCol); 

} 

您可以進一步實現自己。

希望我的建議能幫助你。 :)