views:

962

answers:

2

Hello, all,

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.

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.

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.

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
});
+2  A: 

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);
},
great_llama
+1 - Just curious, this should not matter for this particular question, but what happens to iRow when the grid is sorted? Presumably it would change to match the new ordering of rows?
Justin Ethier
Yes, it would. Rather than doing things based on row, I would send down the composite key into hidden columns so that they could be used for subsequent requests.
great_llama
+1  A: 

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

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 id attribute which can be a row counter you can solve your problem. The value of id attribute must not be a number, you can use any string instead. If the nature of your data allow this, you can produce an unique id as a string composed from your other data.

Oleg