我在页面上有一个jqGrid,用户可以单击按钮添加新行。如果页面上已有足够的行来填充网格的可见部分,则会添加新行并显示滚动条,但用户需要滚动才能看到新行。
有没有办法以编程方式执行此操作?
答案 0 :(得分:11)
使用jqGrid API执行此操作的快捷方法是:
editRow
(将焦点设置到已编辑的行)restoreRow
(因为您真的不想编辑该行) 否则你应该能够使用jQuery的 focus
函数将焦点设置到行,例如:jQuery("#" + row_id).focus()
- 但我没有测试过这个方法,所以YMMV。
实际上focus
不会滚动网格的div。但是您可以使用以下代码来保证网格滚动,以便可以查看具有给定id
的行:
function getGridRowHeight (targetGrid) {
var height = null; // Default
try{
height = jQuery(targetGrid).find('tbody').find('tr:first').outerHeight();
}
catch(e){
//catch and just suppress error
}
return height;
}
function scrollToRow (targetGrid, id) {
var rowHeight = getGridRowHeight(targetGrid) || 23; // Default height
var index = jQuery(targetGrid).getInd(id);
jQuery(targetGrid).closest(".ui-jqgrid-bdiv").scrollTop(rowHeight * index);
}
答案 1 :(得分:0)
//i. Set newly added row (with id = newRowId) as the currently selected row
$('#myGrid').jqGrid('setSelection', newRowId);
//ii. Set focus on the currently selected row
$("#" + $('#myGrid').jqGrid('getGridParam', 'selrow')).focus();
答案 2 :(得分:-1)
供参考:
我发现此示例很有帮助。来自这篇文章的http://gurarie.org/jqGrid.html http://www.trirand.com/blog/?page_id=393/bugs/setselection-is-not-scrolling-to-the-selected-row
我的问题是$(tableInstance).jqGrid('setSelection', id)
在jqGrid配置中的scrollrows: true
时甚至无效height: 'auto'
。我将高度设置为特定高度20并且'setSelection'工作。所选行在用户的视图中。超酷