Skip to content Skip to sidebar Skip to footer

Jqgrid Always Highlighting First Row When Using Addrowdata

Been trying to get the rowID to no avail and I've just suddenly realised there's a fault in my code that's affecting how jqGrid is operating. My code is as follows: function showSe

Solution 1:

Yes, when you say:

thegrid.addRowData(0,item);

You are assigning each row in the grid the same ID of 0.

From the documentation for addRowData:

Parameters

  • rowid
  • data
  • position
  • srcrowid

Description

Inserts a new row with id = rowid containing the data in data (an object) at the position specified (first in the table, last in the table or before or after the row specified in srcrowid) ...

This method can insert multiple rows at once. In this case the data parameter should be array defined as [{name1:value1,name2: value2…}, {name1:value1,name2: value2…} ] and the first option rowid should contain the name from data object which should act as id of the row. It is not necessary that the name of the rowid in this case should be a part from colModel.

Since you are inserting one row at a time (instead of multiple rows as noted above), you need to assign each row a unique ID when calling addRowData. I recommend you retrieve a unique ID as part of your web request, and refactor your code as necessary to assign the ID:

 thegrid.addRowData(item.id, item);

Obviously that is a problem if you do not have a unique ID on the back-end - although it may indicate a larger design problem. In any case if you cannot retrieve an ID variable, I suggest you use a temporary JavaScript variable to assign each row an ever increasing ID:

var counter = 0;
...
    thegrid.addRowData(counter, item);
    counter++;
...


Update

As Oleg points out, you can also pass undefined as the ID to allow jqGrid to automatically generate a unique ID. You can see this from the relevant portion of the source code in grid.base.js:

if(typeof(rowid) != 'undefined') { rowid = rowid+"";}
else {
    rowid = $.jgrid.randId();
    ...

I do not see this feature mentioned in the documentation so use it at your own risk. But that said, this seems like a nice feature that presumably the jqGrid team would have no reason to remove.

Post a Comment for "Jqgrid Always Highlighting First Row When Using Addrowdata"