onSelectRow fix for jqGrid

Oct 7, 2010 · Follow on Twitter and Mastodon javascript

I love the jqGrid jQuery plugin. If you haven’t tried it, I think you should. However, the onSelectRow event doesn’t work that well. Let’s fix it.

When you look at online demos that describe how to edit a grid row, you may have noticed that after editing a row, you must select a new one before you can edit the first one again.

To fix this, I replaced the original onSelectRow implementation:

onSelectRow: function(id) {
   if (id && id!==lastsel) {
      jQuery('#rowed3').jqGrid('restoreRow',lastsel)
      jQuery('#rowed3').jqGrid('editRow',id,true);
      lastsel=id;
   }
}

…with this one:

onSelectRow: function(id) {
   if (id) {
      if (id !== lastsel) {
         articleGrid.jqGrid('restoreRow', lastsel);
         articleGrid.jqGrid('editRow', id, true);
         lastsel = id;
      } else {
         articleGrid.jqGrid('restoreRow', lastsel);
         lastsel = "";
      }
   }
}

With this tiny adjustment, the grid now behaves a lot more like I want it to.

Discussions & More

Please share any ideas, feedback or comments you may have in the Disqus section below, or by replying on Twitter or Mastodon..

If you found this text interesting, make sure to follow me on Twitter and Mastodon for more content like this, and to be notified when new content is published.

If you like & want to support my work, please consider sponsoring me on GitHub Sponsors.