我在頁面上有一個jqGrid,用戶可以點擊一個按鈕來添加一個新行。如果頁面上已有足夠的行填充網格的可見部分,則添加新行並出現滾動條,但用戶需要滾動才能看到新行。有沒有辦法讓jqGrid在添加新行時滾動到底部?
有沒有辦法做到這一點編程?
我在頁面上有一個jqGrid,用戶可以點擊一個按鈕來添加一個新行。如果頁面上已有足夠的行填充網格的可見部分,則添加新行並出現滾動條,但用戶需要滾動才能看到新行。有沒有辦法讓jqGrid在添加新行時滾動到底部?
有沒有辦法做到這一點編程?
一個快速簡便的方法來做到這一點使用的jqGrid API是:
editRow
(這將焦點設置爲編輯行)restoreRow
(因爲你不真要編輯的行)否則,你應該能夠使用jQuery的focus
功能將焦點設置到該行,例如:jQuery("#" + row_id).focus()
- 但我沒有t測試了這個方法,所以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);
}
FYI:
我發現這個例子很有幫助。 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)
沒有工作,甚至scrollrows: true
當height: 'auto'
在jqGrid的配置。我將高度設置爲特定高度20,'setSelection'工作。所選行在用戶的視野中。超級酷
//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();
其實,我想編輯行,所以我只是把它留在編輯模式 - 像魅力的作品! – HitLikeAHammer 2010-03-31 03:27:09
謝謝賈斯汀!我應該做的唯一修改是.scrollTop((rowHeight * index)-rowHeight)使可視目標行。問候。 – nerdcoder 2011-12-14 03:48:40