How does one pro开发者_运维技巧grammatically select the top row of a JQGrid. I want to have the top row already selected when it is opened on the page. My grid is sorted by a descriptive column so the first row's id could be any number. I know the method to use I just don't know how to get the rowid for the top (first) row. The method is:
jQuery("#mygrid").setSelection(rowid, true);
The answer above was close, but the case was off. It should be:
$("#mygrid").getDataIDs()[0];
That should work properly.
Or, without using the jqGrid API, you should be able to retrieve the top row by navigating the DOM:
var top_rowid = $('#mygrid tbody:first-child tr:first').attr('id');
jqGrid supports a setSelection
method it just needs to be called correctly:
var grid = jQuery("#mygrid"),
ids = grid.jqGrid("getDataIDs");
if(ids && ids.length > 0)
grid.jqGrid("setSelection", ids[0]);
$("#mygrid").getDataIDs()[0]; // SO now requires 30 characters, so....
Complete code when table have header row:
var top_rowid = $('#mygrid tr:nth-child(2)').attr('id');
$("#mygrid").setSelection(top_rowid, true);
I've used the following:
var grid = $('#list');
grid.jqGrid({
...
gridComplete: function() {
var ids = grid.jqGrid("getDataIDs");
if(ids.length > 0) {
grid.jqGrid("setSelection", ids[0]);
}
},
...
});
When you have a header row, try this:
var top_rowid = $('#mygrid tbody:first-child tr:nth-child(2)').attr('id');
If you have header row try this :
$('#tb_par tbody:first-child tr:nth-child(2)').trigger("click");
If not than :
$('#mygrid tbody:first-child tr:first').trigger("click");
It will directly trigger click event of the JqGrid.
精彩评论