Suhail Gupta
Suhail Gupta

Reputation: 23206

Getting id as undefined when jqgrid row is clicked

I need to get the id of the selected row, to highlight it. But I get id as undefined. What could be the reason for it?

I have attached a click event with UserDetails jqgrid. The following function is invoked when the UserDetails grid is clicked.

var selectUserGrid = function(event) {
   var grid = $('#UserDetails');
      grid.jqGrid( {
         onSelectRow : function(id) {
            var i = id;
         }
      });
   grid.setSelection(i,true);
}

How do I get the ID of the row clicked?

Upvotes: 0

Views: 636

Answers (1)

Oleg
Oleg

Reputation: 221997

I suppose that you have the problem with the order of processing of click event.

The first problem is in registering of additional click event handler. Why you do this? jqGrid already registered one click event handler. jqGrid process it and it calls beforeSelectRow callback, trigger jqGridBeforeSelectRow event then it selects the row, it calls onSelectRow callback, trigger jqGridSelectRow event, calls onCellSelect callback, trigger trigger jqGridCellSelect. In other words jqGrid process click event and allows to use callbacks and events to do some additional actions, which synchronized with internal work of jqGrid.

If you register the second click handler then you could not quarantine that your click handler will be called after the row of the grid is selected.

I recommend you to use callback or events described above. Moreover you use setSelection in the code fragment which you posted. On the other side it's not clear how you fill the grid. You can call setSelection only after the corresponding row will be inserted in jqGrd. I suppose that your real code is another. Nevertheless the code which you posted can't work. Moreover the code defines i variable inside of onSelectRow callback, but you try to use it in outside of the callback. In the case i will be undefined forever.

Upvotes: 2

Related Questions