jQuery jqGrid 默认值不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10186343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:55:09  来源:igfitidea点击:

jqGrid default value does not work?

jqueryjqgrid

提问by kaze

I have a jqGrid, and one column should have a default value when adding a new row. I have followed the documentation, but the value never appears.

我有一个 jqGrid,当添加新行时,一列应该有一个默认值。我遵循了文档,但从未出现过该值。

This is the column from the colModel:

这是来自 colModel 的列:

{name: 'Lt', index: 'Lt', width: 25, editable: true, editoptions: {defaultValue: 'N'}}

And this is how I call addRowData:

这就是我调用 addRowData 的方式:

$("#grid").addRowData(-1,
    { Datum: $.datepicker.formatDate("yy-mm-dd", new Date()) }, "first", true)

As far as I can see, I'm doing it by the book!

就我所见,我是按书做的!

Complete grid def:

完整的网格定义:

    $("#dagbok_grid").jqGrid({
        datatype: 'json',
        mtype: 'GET',
        colNames: [
            'a',
            'b',
            'c',
            'd',
            'e',
            'f',
            'g',
            'h',
            'i',
            'j',
            'k',
            'l',
            'm',
            'n',
            'o',
            'p',
            'q',
            'r',
            's'],
        colModel: [
          { name: 'a', index: 'a', width: 30, formatter: 'checkbox', edittype: 'checkbox', editable: true },
          { name: 'b', index: 'b', width: 30, formatter: 'checkbox', edittype: 'checkbox', editable: true },
          { name: 'c', index: 'c', width: 70, formatter: 'date', editable: true, editrules: { required: true }, editoptions: { dataInit: function (elem) { $(elem).datepicker(); } } },
          { name: 'd', index: 'd', width: 65, editable: true, formatter: 'date', formatoptions: { srcformat: 'H:i:s', newformat: 'ShortTime' }, editrules: { time: true} },
          { name: 'e', index: 'e', width: 80, edittype: 'select', editable: true, formatter: 'select' },
          { name: 'f', index: 'f', width: 100, editable: true, edittype: 'textarea', editoptions: {rows: '7'} },
          { name: 'g', index: 'g', width: 80, editable: true, edittype: 'textarea', editoptions: { rows: '7'} },
          { name: 'h', index: 'h', width: 80, editable: true, editrules: { maxValue: 50} },
          { name: 'i', index: 'i', width: 120, edittype: 'select', editable: true, formatter: 'select' },
          { name: 'j', index: 'j', width: 200, edittype: 'select', editable: true, formatter: 'select' },
          { name: 'k', index: 'k', width: 70, edittype: 'select', editable: true, formatter: 'select' },
          { name: 'l', index: 'l', width: 70, editable: true, editrules: { maxValue: 10} },
          { name: 'm', index: 'm', width: 25, editable: true, editoptions: { defaultValue: 'N'} },
          { name: 'n', index: 'n', width: 70, editable: true, editrules: { integer: true, maxValue: 999999 }, formatter: formatPosition, unformat: unformatPosition },
          { name: 'o', index: 'o', width: 25, editable: true, editrules: { custom: true, custom_func: chkLongitudTecken} },
          { name: 'p', index: 'p', width: 80, editable: true, editrules: { integer: true, maxValue: 999999 }, formatter: formatPosition, unformat: unformatPosition },
          { name: 'q', index: 'q', width: 80, edittype: 'select', editable: true, formatter: 'select' },
          { name: 'r', index: 'r', width: 100, editable: true, editrules: { maxValue: 50} },
          { name: 's', index: 's', width: 65, edittype: 'select', editable: true, formatter: 'select' },
        ],
        sortname: 'c',
        sortorder: 'desc',
        shrinkToFit: false,
        viewrecords: true,
        gridview: true,
        onSelectRow: function (id) {
            if (id && id !== lastsel) {
                jQuery('#dagbok_grid').saveRow(lastsel);
                lastsel = id;
            }
            jQuery('#dagbok_grid').editRow(id, true);
        },
        height: 400,
        editurl: '@Url.Action("SaveGridRow")',
        caption: 'my grid'
    });

I also later on add select-values to some cols:

我稍后还会向某些列添加选择值:

    $.ajax({
        type: 'GET',
        url: '@Url.Action("GetGridComboValues")',
        async: false,
        success: function (data) {
            var grid = $("#dagbok_grid");
            grid.setColProp("e", { editoptions: { value: data.kallor} });
            grid.setColProp("i", { editoptions: { value: data.rubriker} });
            grid.setColProp("j", { editoptions: { value: data.verksamheter} });
            grid.setColProp("k", { editoptions: { value: data.opPadrag, dataEvents: [{ type: 'change', fn: function (e) {
                $.ajax({
                    type: "GET",
                    url: '@Url.Action("GetVerksamhetskod")',
                    data: { "opPadragId": e.currentTarget.value },
                    dataType: 'json',
                    success: function (data) {
                        var selr = jQuery('#dagbok_grid').jqGrid('getGridParam', 'selrow');
                        jQuery("#dagbok_grid").jqGrid('setCell', selr, "l", data);
                    }
                });
            }
            }]
            }
            });
            grid.setColProp("q", { editoptions: { value: data.medier} });
            grid.setColProp("s", { editoptions: { value: data.regioner} });
        }
    });

Add and delete code

添加和删​​除代码

    $("#toolbarAddButton")
        .button()
        .click(function () {
            $("#dagbok_grid").addRowData(-1, { Datum: $.datepicker.formatDate("yy-mm-dd", new Date()) }, "first", true)
        });

    $("#toolbarDeleteButton").click(function () {
        var radid = $("#dagbok_grid").jqGrid("getGridParam", "selrow");
        if (radid != null) {
            var su = $("#dagbok_grid").jqGrid("delRowData", radid);
            $("#dagbok_grid").jqGrid("delGridRow", radid, { url: '@Url.Action("DeleteGridRow")', reloadaftersubmit: false });

        }
    })

Save executes when the user press entering while in edit mode.

当用户在编辑模式下按 Enter 键时保存执行。

回答by Oleg

First of all you defined editoptions: { defaultValue: 'N'}. You can read in the documentation of the editoptions

首先你定义了editoptions: { defaultValue: 'N'}. 您可以阅读editoptions的文档

The option can be string or function. This option is valid only in Form Editing modulewhen used with editGridRowmethod in add mode. If defined the input element is set with this value if only element is empty. If used in selects the text should be provided and not the key. Also when a function is used the function should return value.

该选项可以是字符串或函数。此选项仅在表单编辑模块中editGridRow在添加模式下与方法一起使用时有效。如果定义了输入元素,则在只有元素为空时使用此值设置。如果在选择中使用,则应提供文本而不是键。同样,当使用函数时,函数应该返回值。

What you want is that default value will be set during fillingthe grid body and not during editing. For the purpose exist defaulValueformatter option, but it will be used only for some predefined formatters (see the documentation).

您想要的是在填充网格体期间而不是在编辑期间设置默认值。为此目的存在defaulValue格式化程序选项,但它将仅用于某些预定义的格式化程序(请参阅文档)。

You can solve the problem either by usage of custom formatteror, if you use addRowDatamethod directly, by adding explicit the value of Ltcolumn.

您可以通过使用自定义格式化程序来解决问题,或者如果您addRowData直接使用方法,则可以通过显式添加Lt列的值来解决该问题。

I don't recommend to use low-level method addRowDatawhich is slow and will be mostly used in wrong way. You used for example -1as the rowid. If you would execute the same statement more as one time you will have id duplicates which is error and can follow to very strange effects. If you want that jqGrid generate the unique ids itself you should use undefinedas the rowid parameter.

我不建议使用低级方法addRowData,它很慢,而且大多会以错误的方式使用。例如-1,您用作rowid。如果您多次执行相同的语句,您将有 id 重复,这是错误的,并且会产生非常奇怪的效果。如果您希望 jqGrid 自己生成唯一 ID,则应将其undefined用作 rowid 参数。