如果在按 jqgrid 中的保存操作按钮返回错误,如何保持内联添加

How to keep inline add if error is returned on pressing save action button in jqgrid

本文关键字:错误 何保持 添加 返回 按钮 jqgrid 操作 保存 如果      更新时间:2023-09-26

jgGrid在工具栏中包含内联添加按钮,在操作列中包含保存操作按钮。使用远程 JSON 数据。如果按下保存操作按钮终止内联添加并且服务器返回错误,添加的行将从网格中删除,输入的行数据将丢失。我添加了恢复之后错误:假到格式选项和内联添加按钮,如下面的代码所示,但如果按下保存操作按钮,这些设置将被忽略。

如何将行保持在内联添加模式,以便在按下保存操作按钮时可以在错误后继续编辑?

colModel: [ {
  name:"_actions",
  formatter:"actions",
  formatoptions:{
    editbutton:true,
    keys:true,
    // this is ignored if action column save button is pressed:
    restoreAfterError:false,
    delbutton:true
    }
} , ...
], 
editurl: '/Grid/Edit',
datatype: "json",

使用以下方法添加内联添加按钮:

$grid.jqGrid('inlineNav', '#grid_toppager', {
    addParams: {
        position: "beforeSelected", 
        rowID: '_empty',
        useDefValues: true,
        addRowParams: {
            keys: true,
            // this is ignored if action column save button is pressed:
            restoreAfterError: false,
        }
    },
    editParams: {
            keys: true,
            // this is ignored if action column save button is pressed:
            restoreAfterError: false,
    },
   add: true,
   edit: false,
   save: true,
   cancel: true
});

我在addParams.addRowParamseditParams内部测试了restoreAfterError: false设置,效果很好。如果出现错误,在我的自定义errorfunc显示错误消息后,编辑(或新添加的行(将保持编辑模式。我想你只有在使用formatter: 'actions'的情况下才有问题.

如果您使用该formatter: 'actions'则无法直接定义restoreAfterError(至少在当前的 jqGrid 版本 3.4.1 中(。所以我建议你将restoreAfterError的默认值更改为false

$.extend($.jgrid.inlineEdit, {
    restoreAfterError: false
});

此外,我建议您从addRowParamseditParams中删除尾随逗号(如此处restoreAfterError: false,}(。许多(但不是全部(Web 浏览器会忽略尾随逗号,但仍然存在错误。