当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - How to programically select top row of JQGrid?

问题描述:

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

I know there must be a simple solution to this but I just can't figure out how to do it.

网友答案:

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');
网友答案:

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

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

That should work properly.

网友答案:

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);
网友答案:

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.

网友答案:

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]);
        }
    },
    ...
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: