jQuery 如何获取jqGrid选定的行单元格值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6995129/
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 21:40:14  来源:igfitidea点击:

How to get a jqGrid selected row cells value

jqueryasp.net-mvcjqgridjqgrid-asp.net

提问by Saad

Does anyone know how to get the cells value of the selected row of JQGrid ? i m using mvc with JQGrid, i want to access the value of the hidden column of the selected row ?

有谁知道如何获取 JQGrid 所选行的单元格值?我在 JQGrid 中使用 mvc,我想访问所选行的隐藏列的值?

回答by Oleg

First you can get the rowidof the selected row with respect of getGridParammethod and 'selrow'as the parameter and then you can use getCellto get the cell value from the corresponding column:

首先,您可以rowid根据getGridParam方法获取所选行的'selrow'作为参数,然后您可以使用getCell从相应列中获取单元格值:

var myGrid = $('#list'),
    selRowId = myGrid.jqGrid ('getGridParam', 'selrow'),
    celValue = myGrid.jqGrid ('getCell', selRowId, 'columnName');

The 'columnName'should be the same name which you use in the 'name'property of the colModel. If you need values from many column of the selected row you can use getRowDatainstead of getCell.

'columnName'应该是你在使用相同的名称'name'的属性colModel。如果您需要来自所选行的许多列的值,您可以使用getRowData而不是getCell

回答by Mitul Maheshwari

You can use in this manner also

您也可以以这种方式使用

var rowId =$("#list").jqGrid('getGridParam','selrow');  
var rowData = jQuery("#list").getRowData(rowId);
var colData = rowData['UserId'];   // perticuler Column name of jqgrid that you want to access

回答by Mike Gledhill

Just to add, you can also retrieve a jqGrid cell value, based on the rowID plus column index (rather than the Column name):

只是补充一下,您还可以根据 rowID 加列索引(而不是列名称)检索 jqGrid 单元格值:

So, to fetch the value in the forth column (column index # 3) for the row with primary key ID 1234, we could use this:

因此,要获取主键 ID 为 1234 的行的第四列(列索引 # 3)中的值,我们可以使用以下命令:

var rowID = 1234;
var columnIndex = 3;
var cellValue = $("#" + rowID).find('td').eq(columnIndex).text();

Btw, on a completely unrelated topic (but please don't vote me down):

顺便说一句,关于一个完全不相关的话题(但请不要投票给我):

I didn't realise that you can, fairly easily, link text boxes to your jqGrid, so your users can do instant searching, without having to open the Search dialog.

我没有意识到您可以相当轻松地将文本框链接到您的 jqGrid,这样您的用户就可以进行即时搜索,而无需打开“搜索”对话框。

enter image description here

enter image description here

To do this, you need a bit of HTML like this:

为此,您需要一些像这样的 HTML:

<input type="text" name="employeeName" id="employeeName" style="width:250px" />

<!--  This will be my jqGrid control and pager -->
<table id="tblEmployees"></table>
<div id="pager"></div>

And a bit of JavaScript like this:

还有一些像这样的 JavaScript:

$("#employeeName").on('change keyup paste', function () {
    SearchByEmployeeName();
});

function SearchByEmployeeName()
{
    //  Fetch the text from our <input> control
    var searchString = $("#employeeName").val();

    //  Prepare to pass a new search filter to our jqGrid
    var f = { groupOp: "AND", rules: [] };

    //  Remember to change the following line to reflect the jqGrid column you want to search for your string in
    //  In this example, I'm searching through the UserName column.

    f.rules.push({ field: "UserName", op: "cn", data: searchString });

    var grid = $('#tblEmployees');
    grid[0].p.search = f.rules.length > 0;
    $.extend(grid[0].p.postData, { filters: JSON.stringify(f) });
    grid.trigger("reloadGrid", [{ page: 1 }]);
}

This is a real game-changer for me... it really makes jqGrid much more user friendly.

这对我来说是一个真正的游戏规则改变者......它确实使 jqGrid 更加用户友好。

Users can immediately start typing in their search string, rather than needing to open the Search dialog, remember to change the operator to "contains", then start typing, and close the search dialog again.

用户可以立即开始输入他们的搜索字符串,而不需要打开搜索对话框,记住将运算符更改为“包含”,然后开始输入,并再次关闭搜索对话框。

回答by Arun Garg

Use "selrow" to get the selected row Id

使用“selrow”获取选定的行 ID

var myGrid = $('#myGridId');

var myGrid = $('#myGridId');

var selectedRowId = myGrid.jqGrid("getGridParam", 'selrow');

var selectedRowId = myGrid.jqGrid("getGridParam", 'selrow');

and then use getRowData to get the selected row at index selectedRowId.

然后使用 getRowData 获取索引 selectedRowId 处的选定行。

var selectedRowData = myGrid.getRowData(selectedRowId);

var selectedRowData = myGrid.getRowData(selectedRowId);

If the multiselect is set to true on jqGrid, then use "selarrrow" to get list of selected rows:

如果 jqGrid 上的 multiselect 设置为 true,则使用“selarrrow”获取所选行的列表:

var selectedRowIds = myGrid.jqGrid("getGridParam", 'selarrrow');

var selectedRowIds = myGrid.jqGrid("getGridParam", 'selarrrow');

Use loop to iterate the list of selected rows:

使用循环迭代选定行的列表:

var selectedRowData;

var selectedRowData;

for(selectedRowIndex = 0; selectedRowIndex < selectedRowIds .length;selectedRowIds ++) {

for(selectedRowIndex = 0; selectedRowIndex < selectedRowIds .length;selectedRowIds ++) {

   selectedRowData = myGrid.getRowData(selectedRowIds[selectedRowIndex]);

}

}

回答by Sici

yo have to declarate de vars...

你必须声明 de vars ...

var selectedRowId = $('#list').jqGrid ('getGridParam', 'selrow');
var columnName = $('#list').jqGrid('getCell', selectedRowId, 'columnName');

var nombre_img_articulo = $('#list').jqGrid('getCell', selectedRowId, 'img_articulo');

var nombre_img_articulo = $('#list').jqGrid('getCell', selectedRowId, 'img_articulo');

回答by Sachin Sanchaniya

Just Checkout This :

只需结帐:

Solution 1 :

解决方案1:

In Subgrid Function You have to write following :

在子网格函数中,您必须编写以下内容:

var selectid = $(this).jqGrid('getCell', row_id, 'id');
alert(selectid);

Where row_idis the variable which you define in subgrid as parameter. And idis the column name which you want to get value of the cell.

row_id您在子网格中定义的变量在哪里作为参数。并且id是您想要获取单元格值的列名。

Solution 2 :

解决方案2:

If You Get Jqgrid Row Id In alert Then set your primary key id as key:trueIn ColModels. So You will get value of your database id in alert. Like this :

如果您在警报中获得 Jqgrid 行 ID,则将您的主键 ID 设置为key:true在 ColModels 中。因此,您将在警报中获得数据库 ID 的值。像这样 :

{name:"id",index:"id",hidden:true, width:15,key:true, jsonmap:"id"},