Cancel event edit in jqGrid inline edit

I am using jqgrid inline edit

in which I have a script to trigger a button event "cancel edit"

and type the message "Are you sure you will cancel?" ...

//Code:

// Unload the mesh.

$('#CommentsData').jqGrid('GridUnload');

               //Comments grid start.
               $("#CommentsData").jqGrid({

                   datastr: tableSrc,
                   hoverrows: false,
                   datatype: "jsonstring",
                   jsonReader: {
                       id: 'CommentId',
                       repeatitems: false
                   },
                   height: 'auto',
                   width: 'auto',
                   hidegrid: false,
                   gridview: true,
                   sortorder: 'desc',
                   sortname: 'DateTime',
                   pager: '#CommentsPager',
                   rowList: [],             // disable page size dropdown
                   pgbuttons: false,        // disable page control like next, back button
                   pgtext: null,            // disable pager text like 'Page 0 of 10'
                   viewrecords: false,      // disable current view record text like 'View 1-10 of 100' 
                   caption: "Comments",
                   colNames: ['DateTime', 'UserName', 'Comments'],
                   colModel: [

                       {
                           name: 'DateTime', index: 'DateTime', width: 120, formatter: "date", sorttype: "date",
                           formatoptions: { srcformat: "ISO8601Long", newformat: "m/d/Y h:i A" }
                       },
                       { name: 'UserName', index: 'UserName' },
                       { name: 'CommentText', index: 'CommentText', editable: true }],

                   //Events to add and edit comments.
                   serializeRowData: function (postdata) {

                       var filterResult;
                       var jsonResult;

                       if (tableSrc == "")
                           jsonResult = $.parseJSON(commentDetails);
                       else
                           //Parse values bind to the comments.
                           jsonResult = $.parseJSON(tableSrc);

                       var newResult = new Object();

                       //Check if operation is edit.
                       if (postdata.oper == "edit") {

                           //Filter the edited comments from main source.
                           newResult = Enumerable.From(jsonResult).Where(function (s) { return s.CommentId = postdata.id }).First();
                           newResult.CommentText = postdata.CommentText;
                       }
                       else {
                           filterResult = Enumerable.From(jsonResult).First();

                           newResult.CommentText = postdata.CommentText;
                           newResult.TransactionId = filterResult.TransactionId;
                           newResult.TaskId = filterResult.TaskId;
                       }

                       filterResult = JSON.stringify(newResult);

                       $.ajax({
                           url: '@Url.Action("UpdateComments", "Home")',
                           datatype: 'json',
                           data: { 'resultData': filterResult, 'action': postdata.oper },
                           type: 'POST',
                           success: OnCompleteComments,
                           error: function (xhr, status, error) {
                               if (xhr.statusText == "Session TimeOut/UnAuthorized") {
                                   alert(xhr.statusText);
                                   window.location.href = '@Url.Action("LogOut", "Account")';
                               }
                               else
                                   alert(xhr.responseText);
                           }
                       });
                       //After update Load the grid.
                       function OnCompleteComments(result) {
                           selectTaskComment = false;
                           $('#dialog').dialog("close");
                           myfilter = $("#TransactionsGrid").jqGrid("getGridParam", "postData").filters;
                           rowList = $('.ui-pg-selbox').val();

                           Loadgrid($("#TransactionsGrid").getGridParam('page'));

                       }

                   },

                   onSelectRow: function (id) {
                       selectTaskComment = true;
                       var thisId = $.jgrid.jqID(this.id);

                       $("#" + thisId + "_iledit").removeClass('ui-state-disabled');
                       $("#del_" + thisId).removeClass('ui-state-disabled');

                       var selectValues = jQuery('#CommentsData').jqGrid('getRowData', id);
                       thisId = $.jgrid.jqID(this.id);

                       if (selectValues.UserName == '@ViewBag.UserName' || '@ViewBag.IsAdmin' == 'True') {
                           $("#" + thisId + "_iledit").removeClass('ui-state-disabled');
                           $("#del_" + thisId).removeClass('ui-state-disabled');

                       }
                       else {

                           $("#" + thisId + "_iledit").addClass('ui-state-disabled');
                           $("#del_" + thisId).addClass('ui-state-disabled');

                       }

                   }

               });

               jQuery("#CommentsData").jqGrid('navGrid', '#CommentsPager', { edit: false, add: false, del: true, search: false, refresh: false }, {}, {},

                   {
                       //Delete event for comments
                       url: '@Url.Action("UpdateComments", "Home")',
                       serializeDelData: function (postData) {
                           return {
                               resultData: JSON.stringify(postData.id),
                               action: JSON.stringify(postData.oper),
                           }
                       },
                       errorTextFormat: function (xhr) {
                           if (xhr.statusText == "Session TimeOut/UnAuthorized") {
                               window.location.href = '@Url.Action("LogOut", "Account")';
                           } else {
                               return xhr.responseText;
                           }
                       },
                       beforeSubmit: function () {

                           myfilter = $("#TransactionsGrid").jqGrid("getGridParam", "postData").filters;
                           return [true, '', ''];
                       },
                       afterSubmit: function (response, postdata) {
                           selectTaskComment = false;
                           Loadgrid($("#TransactionsGrid").getGridParam('page'));

                           return [true, '', ''];
                       }

                   });

                   $('#CommentsData').jqGrid('inlineNav', '#CommentsPager', { edit: true, add: true, save: true, del: false, cancel: true });

                   $("#CommentsData_iledit").addClass('ui-state-disabled');
                   $("#del_CommentsData").addClass('ui-state-disabled');

      

When do I need to write code and send a warning message?

Also, if possible, you need to know if the above code can be optimized. Since the delete event is recorded in a separate location comparing edit and add. I am a bit confused if this is the correct method.

0


source to share


1 answer


The easiest way to invoke the Undo Editing button would be to execute the code

$("#CommentsData_ilcancel").click(); // trigger click event on Cancel button

      



where CommentsData

is the grid identifier.

+2


source







All Articles