jQuery JQgrid - 获取行号而不是 ID

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

JQgrid - Get Row Number instead of ID

jqueryjqgridmethodsrowid

提问by mariojjsimoes

I am creating a JQGrid from a database table that does not contain a single field primary key. Therefore, the field i am supplying as id is not unique and the same one exists in several rows.

我正在从不包含单个字段主键的数据库表中创建 JQGrid。因此,我作为 id 提供的字段不是唯一的,并且同一字段存在于多行中。

Because of this, when passing a reference to the data with ondblClickRow to a function external to the grid i need to use the rownumber and not the id.

因此,当使用 ondblClickRow 将数据引用传递给网格外部的函数时,我需要使用行号而不是 id。

To test, I'm using ondblClickRow: function(id){alert($("#grid1").getInd('rowid'));},, and i should be getting and alert with the row number, except that it isn't working.

为了测试,我正在使用ondblClickRow: function(id){alert($("#grid1").getInd('rowid'));},,我应该得到行号并发出警报,但它不起作用。

I've been over the documentation and can't understand what i am doing wrong...

我已经阅读了文档,但无法理解我做错了什么......

Any help would be greatly appreciated!

任何帮助将不胜感激!

Thanks in advance, Mario.

提前致谢,马里奥。

Bellow is my full grid:

波纹管是我的完整网格:

jQuery(document).ready(function(){
var mygrid = jQuery("#grid1").jqGrid({
    datatype: 'xmlstring',
    datastr : grid1RsXML,
    width: 1024,
    height: 500,
    colNames:['DEVICE_ID','JOB_SIZE_IN_BYTES', 'USER_NAME','HOST_NAME','DAY_OF_WEEK','JOB_ID'],
    colModel:[ 
                {name:'DEVICE_ID',index:'DEVICE_ID', width:55, sortable:true},
                {name:'JOB_SIZE_IN_BYTES',index:'JOB_SIZE_IN_BYTES', width:40, sortable:true},
                {name:'USER_NAME',index:'USER_NAME', width:60, sortable:true},
                {name:'HOST_NAME',index:'HOST_NAME', width:50,align:"right", sortable:true},
                {name:'DAY_OF_WEEK',index:'DAY_OF_WEEK', width:10, sortable:true},
                {name:'JOB_ID',index:'JOB_ID', width:30, sortable:true}

             ],
    rowNum:1000,
    autowidth: true, 
    //rowList:[10,20,30],
    rowList:[1],
    pager: '#grid1Pager',
    sortname: 'DEVICE_ID',
    viewrecords: true,
    rownumbers: true,
    sortorder: "desc",
    sortable: true,
    gridview : true,
    xmlReader: { root : "recordset", row: "record", repeatitems: false, id: "DEVICE_ID" },
    caption:"All Jobs - Double Click for detailed history",
    ondblClickRow: function(id){alert($("#grid1").getInd('rowid'));}, 
    toolbar: [true,"top"],
    url: grid1RsXML
});

回答by Yin Yang

I think you can use the getInd() method, like this:

我认为您可以使用 getInd() 方法,如下所示:

var sel_id = $grid.getGridParam('selrow');
var index = $grid1.jqGrid('getInd',sel_id); // counting from 1

http://www.trirand.com/jqgridwiki/doku.php?id=wiki:methods

http://www.trirand.com/jqgridwiki/doku.php?id=wiki:methods

回答by great_llama

What you're looking for is already passed to the event, you just need to include more of the available parameters in your function declaration:

您要查找的内容已经传递给事件,您只需要在函数声明中包含更多可用参数:

ondblClickRow: function(id, iRow, iCol, e) {
    alert(iRow);
},

回答by Oleg

The answer of your main question gives you great_llama, but it seams to me, that you a little misunderstand idas a XML input used by jqGrid. Default xmlReaderhas id: "[id]". So you just remove id: "DEVICE_ID"from the definition of xmlReaderand place idattribute in your data:

您的主要问题的答案为您提供了 great_llama,但在我看来,您有点误解id为 jqGrid 使用的 XML 输入。默认xmlReaderid: "[id]". 因此,您只需id: "DEVICE_ID"从数据中的定义xmlReader和放置id属性中删除:

var grid1RsXML = "<?xml version='1.0' encoding='utf-8'?>"+
  "<recordset>"+
      "<rows>"+
          "<record id='1'>"+
              "<DEVICE_ID>data1</DEVICE_ID>"+
              "<JOB_SIZE_IN_BYTES>data2</JOB_SIZE_IN_BYTES>"+
              "<USER_NAME>data3</USER_NAME>"+
              "<HOST_NAME>data4</HOST_NAME>"+
              "<DAY_OF_WEEK>data5</DAY_OF_WEEK>"+
              "<JOB_ID>data6</JOB_ID>"+
          "</record>"+
          "<record id='2'>"+
              "<DEVICE_ID>data1</DEVICE_ID>"+
              "<JOB_SIZE_IN_BYTES>data2</JOB_SIZE_IN_BYTES>"+
              "<USER_NAME>data3</USER_NAME>"+
              "<HOST_NAME>data4</HOST_NAME>"+
              "<DAY_OF_WEEK>data5</DAY_OF_WEEK>"+
              "<JOB_ID>data6</JOB_ID>"+
          "</record>"+
          "<record id='3'>"+
              "<DEVICE_ID>data1</DEVICE_ID>"+
              "<JOB_SIZE_IN_BYTES>data2</JOB_SIZE_IN_BYTES>"+
              "<USER_NAME>data3</USER_NAME>"+
              "<HOST_NAME>data4</HOST_NAME>"+
              "<DAY_OF_WEEK>data5</DAY_OF_WEEK>"+
              "<JOB_ID>data6</JOB_ID>"+
          "</record>"+
      "</rows>"+
  "</recordset>";

So your main data which send your server could have double rows, but if you just add idattribute which can be a row counter you can solve your problem. The value of idattribute must not be a number, you can use any string instead. If the nature of your data allow this, you can produce an unique idas a string composed from your other data.

因此,发送服务器的主要数据可能有双行,但是如果您只添加id可以是行计数器的属性,您就可以解决您的问题。id属性的值不能是数字,您可以使用任何字符串代替。如果您的数据的性质允许这样做,您可以生成一个id由其他数据组成的唯一字符串。

回答by Kisor Biswal

Recently I had same requirement, i.e to access the row data by row number rather than the row id. I don't have any row id. Below code works for me. NOTE: The row count starts from 1.

最近我有同样的要求,即按行号而不是行 id 访问行数据。我没有任何行 ID。下面的代码对我有用。注意:行数从 1 开始。

var rowNum=$("#grid1").getGridParam("selarrrow"); $("#grid1").getRowData(rowNum);

var rowNum=$("#grid1").getGridParam("selarrrow"); $("#grid1").getRowData(rowNum);

回答by iwwenbo

I make it like this:

我这样做:

ondblClickRow:function(rowid,iRow,iCol,e){var curPage =$("#gridTable").getGridParam("page");//current page var pageSize = $("#gridTable").getGridParam("rowNum");//records per pagevar count = parseInt((curPage-1)*pageSize) + parseInt(rowid);}