2011-05-26 72 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); 

此代码保留最后的位置网格