jQuery 如何以编程方式选择 JQGrid 的顶行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1785997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:07:54  来源:igfitidea点击:

How to programmatically select top row of JQGrid?

jqueryjqgridselectionrow

提问by MikeD

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:

如何以编程方式选择 JQGrid 的第一行。我希望在页面上打开时已经选择了顶行。我的网格按描述性列排序,因此第一行的 id 可以是任何数字。我知道使用的方法我只是不知道如何获取顶(第一)行的 rowid。方法是:

jQuery("#mygrid").setSelection(rowid, true);

采纳答案by Sam C

Or, without using the jqGrid API, you should be able to retrieve the top row by navigating the DOM:

或者,不使用 jqGrid API,您应该能够通过导航 DOM 来检索顶行:

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

回答by rbdrbd

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

上面的答案很接近,但案子已经结束。它应该是:

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

That should work properly.

那应该可以正常工作。

回答by Rob Willis

jqGrid supports a setSelectionmethod it just needs to be called correctly:

jqGrid 支持一种setSelection只需要正确调用的方法:

var grid = jQuery("#mygrid"),
    ids = grid.jqGrid("getDataIDs");
if(ids && ids.length > 0)
    grid.jqGrid("setSelection", ids[0]);

回答by Craig Stuntz

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

回答by user1325543

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

回答by user941904

Complete code when table have header row:

表有标题行时的完整代码:

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

回答by Yagnesh Agola

If you have header row try this :

如果你有标题行试试这个:

$('#tb_par tbody:first-child tr:nth-child(2)').trigger("click");

$('#tb_par tbody:first-child tr:nth-child(2)').trigger("click");

If not than :

如果不是:

$('#mygrid tbody:first-child tr:first').trigger("click");

$('#mygrid tbody:first-child tr:first').trigger("click");

It will directly trigger click event of the JqGrid.

它会直接触发 JqGrid 的点击事件。

回答by Yuri

When you have a header row, try this:

当你有一个标题行时,试试这个:

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