2011-05-26 47 views
3

我的頁面上有一個jqGrid表格,裏面有很多列,所以網格比瀏覽器窗口更寬,出現水平滾動條。如何在內嵌編輯時保持jqGrid的水平滾動位置?

問題是,當用戶單擊一行開始編輯時,瀏覽器框架會向左滾動,從而令用戶感到困惑,甚至有時會隱藏選定的單元格。

我試過了我找到的方法herehere,但它們不適用於我 - 我可以檢索當前的滾動位置,但我無法設置它。 細胞設定爲columnsModel可編輯的,所以我得到滾動位置formatCell事件,並嘗試設置滾動位置afterEditCell用下面的代碼:

afterEditCell: function(rowid, cellname, value, irow, icol) { 

jQuery("#list").closest(".ui-jqgrid-bdiv").scrollLeft(scrollPosition); 
window.scrollTo(scrollPosition,0); 
if (window.pageXOffset) {window.pageXOffset = scrollPosition;}; 
if (document.body.parentElement) {document.body.parentElement.scrollLeft = scrollPosition;}; 
if (document.body) {document.body.scrollLeft = scrollPosition;}; 

而且,這並不無論如何影響電網的行爲 - 它滾動到最左邊的位置。

還有其他方法可以實現嗎? 非常感謝提前!

回答

0

移動滾動碼內的setTimeout()之類:

的setTimeout(函數(){。 grid2.closest( 「UI-的jqGrid-BDIV」)scrollLeft(的scrollPosition); },100);

0

@安德魯斯 - 爲什麼超時?無論如何,我實現你的代碼到我的解決方案。

從jqGrid的頁面

內置編輯:

onSelectRow: function(id){ 
    if(id && id!==lastSel){ 
     jQuery('#grid_id').restoreRow(lastSel); 
     lastSel=id; 
    } 
    jQuery('#grid_id').editRow(id, true); 
}, 

我的版本與滾動不 「跳」 到左:

onSelectRow: function(id){ 
    scrollPosition = jQuery('#grid_id').closest(".ui-jqgrid-bdiv").scrollLeft(); 
    if(id && id!==lastSel){ 
     jQuery('#grid_id').restoreRow(lastSel); 
     lastSel=id; 
    } 
    jQuery('#grid_id').editRow(id, true).scrollLeft(scrollPosition); 
}, 
0
onSelectRow: function(rowid, status, e){ 
    if (lastID) grid.jqGrid("restoreRow", lastID); 

    if (rowid !== lastID) { 
    var scrollPosition = grid.closest(".ui-jqgrid-bdiv").scrollLeft(); 

    grid.jqGrid("editRow", rowid, false, function(){ 
     setTimeout(function(){ $("input, select", e.target).focus(); }, 10); 
    }); 

    setTimeout(function(){ grid.closest(".ui-jqgrid-bdiv").scrollLeft(scrollPosition); }, 0); 

    lastID = rowid; 
    } 
    else lastID = null; 
} 
2

嘗試這一項上loadComplete功能

var offset = $("#" + *subgrid_table_id*).offset(); 
var w = $(window); 
var fromTop = offset.top - w.scrollTop(); 
$('html, body').animate({ 
    scrollTop: offset.top - fromTop 
}, 1); 

此代碼保留最後的位置網格