MikeD
MikeD

Reputation: 751

How to programmatically select top row of JQGrid?

How does one programmatically 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);

Upvotes: 12

Views: 41132

Answers (8)

user1325543
user1325543

Reputation: 531

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]);
        }
    },
    ...
});

Upvotes: 1

Rob Willis
Rob Willis

Reputation: 4844

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]);

Upvotes: 8

Yagnesh Agola
Yagnesh Agola

Reputation: 4639

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.

Upvotes: 0

Craig Stuntz
Craig Stuntz

Reputation: 126547

 $("#mygrid").getDataIDs()[0]; // SO now requires 30 characters, so....

Upvotes: 6

user941904
user941904

Reputation: 11

Complete code when table have header row:

var top_rowid = $('#mygrid tr:nth-child(2)').attr('id'); 
$("#mygrid").setSelection(top_rowid, true);

Upvotes: 1

rbdrbd
rbdrbd

Reputation: 181

The answer above was close, but the case was off. It should be:

$("#mygrid").getDataIDs()[0];

That should work properly.

Upvotes: 18

Yuri
Yuri

Reputation: 17502

When you have a header row, try this:

var top_rowid = $('#mygrid tbody:first-child tr:nth-child(2)').attr('id');

Upvotes: 0

Sam C
Sam C

Reputation: 729

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');

Upvotes: 9

Related Questions