javascript 如何返回选定行的jqgrid数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25826464/
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
How to return jqgrid data of selected rows
提问by shaan N
In JQGrid
在 JQGrid 中
var gridData=$("#SearchResults").jqGrid('getRowData')
The above line gives you the grid data of all the rows, is there a way where I can get the grid data of only the selected rows.
上面的行为您提供了所有行的网格数据,有没有办法只获取选定行的网格数据。
selRowId = myGrid.jqGrid ('getGridParam', 'selrow'),
selRowId = myGrid.jqGrid ('getGridParam', 'selrow'),
The above gives the selected row IDs but I want the data as well of all the selected rows as it returns with gridData but I need only of those of selected one
上面给出了选定的行 ID,但我想要所有选定行的数据,因为它与 gridData 一起返回,但我只需要选定行的那些
回答by Oleg
It's very simple. The second optional option parameter of getRowData
method is rowid of the row which data is requested (see the documentation). So you can use
这很简单。方法的第二个可选选项参数getRowData
是请求数据的行的 rowid(请参阅文档)。所以你可以使用
var selRowId = myGrid.jqGrid("getGridParam", "selrow");
to get last selected rowid first and then get the data of the row by
首先获取最后选择的rowid,然后通过以下方式获取行的数据
var rowData = myGrid.jqGrid("getRowData", selRowId);
If you use datatype: "local"
or some remote datatype
("xml"
or "json"
), but with loadonce: true
then jqGrid hold the data internally in data
array. In the case the usage of getLocalRow
method is more effective as the usage of getRowData
:
如果您使用datatype: "local"
或某些远程datatype
("xml"
或"json"
),但是使用loadonce: true
jqGrid 在内部将数据保存在data
数组中。在这种情况下,getLocalRow
方法的使用比使用更有效getRowData
:
var rowData = myGrid.jqGrid("getLocalRow", selRowId);
If you use multiselect: true
option then jqGrid supports selarrrow
array of selected rowids and you can get all required data in the loop:
如果您使用multiselect: true
选项,则 jqGrid 支持selarrrow
选定的rowids数组,您可以在循环中获取所有必需的数据:
var i, selRowIds = myGrid.jqGrid("getGridParam", "selarrrow"), n, rowData;
for (i = 0, n = selRowIds.length; i < n; i++) {
rowData = myGrid.jqGrid("getLocalRow", selRowIds[i]);
// one can uses the data here
}
回答by AaA
If you are trying to grab your data from inside a grid event such as onSelectRow
you can ignore the first part of Oleg's answer and get the data like following:
如果您试图从网格事件内部获取数据,例如onSelectRow
您可以忽略 Oleg 答案的第一部分并获取如下数据:
myGrid.jqGrid({
... // Grid create options ->
datatype: 'local',
data: gridData,
// <--
onSelectRow: function(id){
data = $(this).jqGrid("getLocalRow", id);
// if you need actual content of the cells
// data = myGrid.jqGrid("getRowData", id);
}
});
Obviously this only works on a single row, however if the intention is rows, I believe Oleg's answer is the only choice.
显然这仅适用于单行,但是如果意图是rows,我相信 Oleg 的答案是唯一的选择。
Note: this doesn't work in version 4.5.4 and below however it is fixed in oleg's free jqgrid 4.15.2
注意:这在 4.5.4 及以下版本中不起作用,但在oleg 的免费 jqgrid 4.15.2 中已修复