【问题标题】:Before Submit function not firing in jqgrid在jqgrid中未触发提交之前的功能
【发布时间】:2014-11-07 11:59:04
【问题描述】:

您好,我有一个通过格式操作的 jqgrid 编辑功能。是否可以在提交之前使用该方法并执行验证。

formatoptions: {
                        keys: true, // we want use [Enter] key to save the row and [Esc] to cancel editing.
                        onEdit: function (rowid) {
                            //alert("in onEdit: rowid=" + rowid + "\nWe don't need return anything");
                        },
                        onSuccess: function (jqXHR) {
                            $('input[id*="gs_"]').val("");
                            $grid.setGridParam({ search: false, postData: { "filters": ""} ,datatype: 'json'}).trigger("reloadGrid");
                            // the function will be used as "succesfunc" parameter of editRow function
                            // (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:inline_editing#editrow)
                            /*alert("in onSuccess used only for remote editing:" +
                                "\nresponseText=" + jqXHR.responseText +
                                "\n\nWe can verify the server response and return false in case of" +
                                " error response. return true confirm that the response is successful");
                            // we can verify the server response and interpret it do as an error
                            // in the case we should return false. In the case onError will be called
                            return true;*/
                        },
                        onError: function (rowid, jqXHR, textStatus) {
                            // the function will be used as "errorfunc" parameter of editRow function
                            // (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:inline_editing#editrow)
                            // and saveRow function
                            // (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:inline_editing#saverow)
                            /*alert("in onError used only for remote editing:" +
                                "\nresponseText=" + jqXHR.responseText +
                                "\nstatus=" + jqXHR.status +
                                "\nstatusText" + jqXHR.statusText +
                                "\n\nWe don't need return anything");*/
                        },
                        afterSave: function (rowid) {
                            //alert("in afterSave (Submit): rowid=" + rowid + "\nWe don't need return anything");
                        },
                        afterRestore: function (rowid) {
                            //alert("in afterRestore (Cancel): rowid=" + rowid + "\nWe don't need return anything");
                        },
                        delOptions: myDelOptions
                    }
    }       ],

函数永远不会被触发。我希望执行 ajax 请求以进行验证,

请提供帮助和感谢

【问题讨论】:

    标签: jqgrid jqgrid-formatter


    【解决方案1】:

    在内联编辑中有beforeSaveRow(如果你不使用一些旧版本的jqGrid)。不能在formatter: "actions" 的formatoptions 内部设置回调,但可以改用$.jgrid.inlineEdit:

    $.extend(true, $.jgrid.inlineEdit, {
        beforeSaveRow: function (options, rowid) {
            ...
            return true; // return false break submiting
        }
    });
    

    【讨论】:

    • 嗨 Oleg,如果我必须将行数据发送到服务器,我将如何序列化数据以便将其发送到服务器进行验证
    • @user2375298:对不起,我不完全明白你想做什么。我理解你的问题:为什么beforeSubmit 不开火?我的回答是您尝试使用表单编辑回调beforeSubmit 而不是可用于内联编辑的beforeSaveRow。验证的最佳方式是另一个问题。我不知道你的全部情况。我认为根本不需要服务器端验证的额外步骤。如果服务器发现输入数据不正确,它应该只返回相应的错误消息和一些错误 HTTP 代码。
    • @user2375298:在errorfunc 内部(在使用formatter: "actions" 的情况下对应onError)可以向用户显示错误消息。如果您使用restoreAfterError: false,则用户可以继续编辑该行并修复输入以使其有效。如果在 HTTP 响应中设置错误状态码有问题,可以使用 successfunc (onSuccess) 分析行保存响应的 body 并返回 [false, "error massage from the server body"] 通知错误。
    • @user2375298:我不是spring开发人员,但是搜索显示可以使用return new ResponseEntity<String>("some error message", HttpStatus.BAD_REQUEST);报告验证错误,使用return new ResponseEntity<String>(json, HttpStatus.OK);进行无错误响应。您需要将控制器操作的返回类型更改为 ResponseEntity<String> of Cause。
    猜你喜欢
    • 1970-01-01
    • 2012-08-25
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 2014-11-01
    • 2023-04-02
    相关资源
    最近更新 更多