25 July 2015

displaying custom message and preventing the delete action in jqgrid

Sometimes you will have business requirements not to delete some rows in jqgrid, while allowing others to be deleted. So while making some rows not to be deleted, it will not make sense to show default confirmation message, instead, you want to show your own alert message, saying "You cannot delete the record because of XYZ reason".

Preventing JQGrid default confirmation dialog box

It's pretty easy to achieve the above requirement and one of the approach is below:

  1. Handle the beforeShowForm event for the DELETE button and display your custom message based on the selected row.
    beforeShowForm: function ($form) {
      // Identity whether you want to display the below 
      // message or not based on selected row.
      // var selRowId = myGrid.jqGrid('getGridParam', 'selrow');
      alert('You cannot delete the record because of XYZ reason');
  2. Handle the afterShowForm event for the DELETE button and don't show the delete confirmation box. One way to do this is automatically clicking the Cancel button in the confirmation box so that it gets closed.
    try {
    // do this based on the selected row
    $("#eData", $form.parent()).click();
    catch (ex) {}
    • Catch any exception and eat it by clicking the Cancel button.
    • To know the ID of the buttons on the confirmation box, you can launch the Developer Tools in your browsers and inspect the window. In IE browser you can launch the Developer Tools by clicking F12 key.
IE Browser Developer Tool inspection

No comments:

Post a Comment