在jqGrid内联编辑中取消编辑事件

时间:2014-12-04 06:09:42

标签: jqgrid

我正在使用jqgrid inline edit我有一个方案来调用"cancel edit"按钮事件并发送消息"您确定要取消吗?&#34 ;

//代码:

//卸载网格。

$('#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');

在哪种情况下我必须编写代码并抛出警告消息?

如果可能,还需要知道上述代码是否可以优化。因为删除事件是在比较编辑和添加的单独位置编写的。如果这是正确的方法,我有点困惑。

1 个答案:

答案 0 :(得分:2)

调用"取消编辑"的最简单方法按钮将执行代码

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

其中CommentsData是网格的ID。