jqGrid的默认值不起作用? [英] jqGrid default value does not work?

查看:77
本文介绍了jqGrid的默认值不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jqGrid,并且在添加新行时,一列应具有默认值.我已经遵循了文档,但从未显示过该值.

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.

这是colModel中的列:

This is the column from the colModel:

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

这就是我所说的addRowData:

And this is how I call 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!

完整的网格定义:

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

添加和删除代码

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

        }
    })

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

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

推荐答案

首先定义editoptions: { defaultValue: 'N'}.您可以阅读编辑选项的文档

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

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

The option can be string or function. This option is valid only in Form Editing module when used with editGridRow method 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.

您想要的是将在填充而不是在编辑过程中设置默认值.为此,存在defaulValue格式化程序选项,但它仅用于某些预定义的格式化程序(请参阅

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

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

You can solve the problem either by usage of custom formatter or, if you use addRowData method directly, by adding explicit the value of Lt column.

我不建议使用低级方法addRowData,该方法很慢并且通常会以错误的方式使用.例如,您使用-1作为行ID.如果您一次执行同一条语句,您将获得id重复,这是错误的,并且可能会产生非常奇怪的效果.如果希望jqGrid本身生成唯一ID,则应使用undefined作为rowid参数.

I don't recommend to use low-level method addRowData which is slow and will be mostly used in wrong way. You used for example -1 as 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 undefined as the rowid parameter.

这篇关于jqGrid的默认值不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆