Matt Ruwe
Matt Ruwe

Reputation: 3406

Event for row deselect on JQGrid

I'm using the JQGrid and have an "Add new" button defined.

$(gridId).jqGrid('navButtonAdd', pagerId, {
    caption: "Add Record",
    buttonicon: "ui-icon-add",
    onClickButton: function (id) {
        var newId = -1;
        $('#addButton').addClass('ui-state-disabled');
        var datarow = { Id: newId, 'Active': true, 'Location': '', 'LabCode': '', 'Crops': '', 'LabManager': '', 'MarkerChoice': '', 'AllowPlates': '', 'AllowTape': '' };
        var su = jQuery(gridId).jqGrid('addRowData', newId, datarow, "last");
        if (su) {
            $(gridId).jqGrid('setSelection', newId);
        }
    },
    position: "last",
    id: "addButton"
});

As you can see, when the button is clicked it disables so it cannot be clicked again. My problem is that when a user clicks add and then hits esc I need to re-enable the add button:

$('#addButton').removeClass('ui-state-disabled');

But I'm not sure where to do that? Is there a row deselected event or equivalent that I could use?

Upvotes: 1

Views: 5243

Answers (1)

Terry
Terry

Reputation: 14219

There is the onSelectRow method. You could capture the selected row and if it changes or is -1 you can call your removeClass() code.

var lastSel;
jQuery("#gridid").jqGrid({
...
   onSelectRow: function(id){ 
      if(id && id!==lastSel){ 
         ... 
         lastSel=id; 
      } 
   },
...
});

Edit: To trap the ESC key:

/* could use window or a specific element if required */
$(window).keydown(function(e) {   // or keyup, keypress
        if (e.keyCode == '27') {  // ESC
            e.preventDefault();
            // do something with the grid
        }
});

Upvotes: 2

Related Questions