【问题标题】:JqGrid, how can I format error messages when using navGridJqG​​rid,使用 navGrid 时如何格式化错误消息
【发布时间】:2017-04-08 03:57:19
【问题描述】:
jqGrid 4.13.6-pre - free jqGrid

我正在使用 navGrid 和内联编辑,但在格式化从服务器发回的验证消息时遇到问题。验证消息从内联编辑返回时看起来不错,但在从网格导航访问的“添加/编辑”表单上看起来不太好。

我阅读了很多关于 errorTextFormat 事件的内容,它似乎完全符合我的要求,但是当从网格导航打开表单时,我似乎无法弄清楚如何访问它。我确信有一个简单的方法来配置它,但我一直无法弄清楚。

$(function() {
        var lastSel = 0;

        $("#Grid")
            .jqGrid({
                url: '/url/to/griddata',
                datatype: 'json',
                mtype: 'POST',
                colNames: ['Id', 'Name'],
                colModel: [
                    { name: 'Id', hidden: false, search: true, width: 150, align: 'center', sortable: true, editable: false, formatter: null, edittype: 'text' }, 
                    { name: 'Name', hidden: false, search: true, width: 150, align: 'center', sortable: true, editable: true, formatter: null, edittype: 'text', editoptions: { maxlength: 256, value: null, required: true } }],
                pager: '#GridPager',
                prmNames: {
                    page: 'PageNumber',
                    rows: 'PageSize',
                    sort: 'SortColumn',
                    order: 'SortOrder',
                    search: 'Search',
                    nd: 'nd',
                    npage: 'null'
                },
                rowNum: 60,
                rowList: [
                    15,
                    30,
                    60,
                    120
                ],
                sortname: "Name",
                sortorder: "asc",
                viewrecords: true,
                hidegrid: false,
                gridview: true,
                caption: '',
                width: 980,
                height: 100,
                editurl: '/my/edit/url',
                edit: {
                    errorTextFormat: function (data) {
                        alert('fired');
                        console.log(data);
                        return "error here";
                    }
                },
                jsonReader: {
                    total: 'TotalPages',
                    page: 'CurrentPage',
                    records: 'TotalRecords',
                    root: 'Rows',
                    id: 'Id',
                    repeatitems: false
                },
                onSelectRow: function(id) {
                    if (id && id !== lastSel) {
                        jQuery('#Grid').restoreRow(lastSel);
                        lastSel = id;
                    }
                    $('#Grid').jqGrid('editRow', id,
                    {
                        keys: true
                    });
                }
            });

        $("#Grid").filterToolbar({ autosearch: true, searchOnEnter: false });
        $("#Grid").navGrid('#GridPager', {
            del: false, search: false, editerrorTextFormat: function (data) {
                alert('fired');
                console.log(data);
                return "error here";
            }
        });

这里是标记。

【问题讨论】:

  • 请包含代码片段,它显示了您如何尝试使用表单编辑和回调errorTextFormat。典型错误:在错误的地方包含回调。您还应该始终包含有关您使用(或可以使用)的 jqGrid 的版本和分支的信息。有两个主要分支:free jqGrid、商业Guriddo jqGrid JS 和版本 errorTextFormat 有 不同的 种可能性。
  • 如果您搜索使用示例errorTextFormat,您可以找到更多信息,例如herehere。如果它没有帮助,那么您应该在您的问题中附加 JavaScript 代码和服务器响应示例,包括用于从服务器返回验证错误的 HTTP 代码。响应可以在 IE/Chrome/Firefox 的开发者工具(网络选项卡)或Fiddler中看到。
  • 我在问题中添加了 javascript/标记。
  • 添加 jqgrid 版本。
  • 我已经看到了与 errorTextFormat 相关的这两个答案。尽管在使用 navGrid 时,我找不到任何将其实际放置在配置代码中的示例。

标签: jquery jqgrid jqgrid-formatter


【解决方案1】:

jqGrid 没有edit 选项,navGrid 也没有editerrorTextFormat 属性。使用formEditing 选项很容易修复您的代码,它允许指定在网格中直接或间接使用的editGridRow 方法的默认值。您只需将edit 选项重命名为formEditing,您的代码就可以工作了。重要的是服务器应该使用一些错误 HTTP 状态代码以防报告错误(例如 400 或更高)。在我看来,错误代码 500 或更高版本是您的最佳选择。

同样你可以在free jqGrid的searching选项中指定filterToolbar或Searching Dialog的选项。您可以在 jqGrid 的 navOptions 选项中指定 navGrid 的默认选项。

我建议您另外使用 jqGrid 的 savedRow 参数而不是 lastSel 变量。该参数将在开始行内编辑或单元格编辑时由 jqGrid 填充。如果包含可编辑值数组之前 修改和id 属性另外。因为你调

我建议您另外使用pager: true 而不是pager: '#GridPager' 并跳过navGridinlineNav'#GridPager' 参数。免费的jqGrid会自动为pager生成<div>,它会为div分配唯一的id,并将pager: true参数修改为'#generatesIdValueOfTheDiv'。您的代码会更短,更易于阅读和维护。

最终代码如下所示

$(function() {
    $("#Grid")
        .jqGrid({
            url: '/url/to/griddata',
            datatype: 'json',
            mtype: 'POST',
            colModel: [
                { name: 'Id', align: 'center' }, 
                { name: 'Name', align: 'center', editable: true,
                    editoptions: { maxlength: 256, required: true } }
            ],
            pager: true,
            prmNames: {
                page: 'PageNumber',
                rows: 'PageSize',
                sort: 'SortColumn',
                order: 'SortOrder',
                search: 'Search'
            },
            rowNum: 60,
            rowList: [
                15,
                30,
                60,
                120
            ],
            sortname: "Name",
            viewrecords: true,
            hidegrid: false,
            width: 980,
            height: 100,
            editurl: '/my/edit/url',
            jsonReader: {
                total: 'TotalPages',
                page: 'CurrentPage',
                records: 'TotalRecords',
                root: 'Rows',
                id: 'Id',
                repeatitems: false
            },
            formEditing: {
                closeOnEscape: true,
                closeAfterEdit: true,
                savekey: [true, 13],
                errorTextFormat: function (data) {
                    alert('fired');
                    console.log(data);
                    return "error here";
                }
            },
            inlineEditing: {
                keys: true
            },
            searching: {
                searchOnEnter: false
            },
            navOptions: {
                del: false,
                search: false
            },
            onSelectRow: function (rowid) {
                var $self = $(this), i,
                    // savedRows array is not empty if some row is in inline editing mode
                    savedRows = $self.jqGrid("getGridParam", "savedRow");

                for (i = 0; i < savedRows.length; i++) {
                    if (savedRows[i].id !== rowid) {
                        // cancel editing of not saved rows
                        $self.jqGrid("restoreRow", savedRows[i].id);
                    }
                }

                $self.jqGrid("editRow", rowid);
            }
        })
        .jqGrid("filterToolbar");
        .jqGrid("navGrid");
});

(我在formEditing 中添加了一些经常使用的属性。您可以删除不需要的属性)。我删除了 colModel 的一些不需要的选项和一些不需要的属性,因为您指定了 default 值。

【讨论】:

  • 感谢您的回复,我会仔细查看,看看是否可以让它做我需要做的事情。我正在使用 400 响应,因为它们实际上是服务器端验证错误(就像给定名称的对象已经存在),我希望将其显示给用户,因为它不是成功的保存。我的 colmodel 东西也是基于我创建的一些 jqgrid 模型在服务器端生成的,所以这就是输出所有默认值的原因,因为我没有出于任何原因覆盖它们。
  • 感谢您的信息,将其移至 formEditing 参数效果很好。其他信息也很高兴知道。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-03
  • 1970-01-01
  • 1970-01-01
  • 2011-01-12
  • 1970-01-01
相关资源
最近更新 更多