jqGrid 编辑自定义函数

jqGrid edit custom function

我在对可编辑单元格进行数据验证时遇到问题。我根据这些单元格的值使一列的某些单元格可编辑。这是网格的代码:

jQuery("#cart").jqGrid({
    datatype: 'local',
    autowidth: true,
    height: tabHeight,
    gridview: true,
    shrinkToFit: true,
    autoencode: true,
    loadtext: "იტვირთება",
    multiselect: true,
    idPrefix:"b",
    colNames: ['დასახელება', 'რაოდენობა მინ.', 'რაოდენობა მაქს.', 'პოზიცია', 'რაოდენობა', 'ფასი'],
    colModel: [{
        name: 'asktx',
        index: 'asktx',
        width: 40,
        sorttype: "string",
    },
    {
        name: 'menge_min',
        index: 'menge_min',
        width: 30,
        sorttype: "number",
        hidden: true
    },
    {
        name: 'menge_max',
        index: 'menge_max',
        width: 30,
        sorttype: "number",
        hidden: true
    },
    {
        name: 'srvpos',
        index: 'svrpos',
        hidden: true
    },
    {
        name: 'menge',
        index: 'menge',
        width: 30,
        sorttype: "number",
        editrules: {
            required: true, number: true, custom: true, custom_func: checkInterval
        },
        editable: true,
    },
    {
        name: 'price',
        index: 'price',
        width: 30,
        sorttype: "string",
        search: false,
    }],
    viewrecords: true,
    caption: "არჩეული მომსახურებები",
    gridComplete: function () {
        var $this = $(this), ids = $this.jqGrid('getDataIDs'), i, l = ids.length;
        for (i = 0; i < l; i++) {
            var rowData = jQuery(this).getRowData(ids[i]);
            if (rowData.menge_min != rowData.menge_max && !rowData.menge) {
                menge_min = rowData.menge_min;
                menge_max = rowData.menge_max;
                $this.jqGrid('editRow', ids[i], true);
            }
        }
    }
});

我正在使用 gridComplete 检查两个单元格的值是否相等,如果不相等,我将在该行中创建一个名为 "menge" 的列可编辑。我还对常量值进行了验证,比如如果我需要检查这个 menge 值是否在 a 和 b 之间,我可以这样做并且一切都很好,尽管现在我需要根据 "menge_min""menge_max" 字段值,它们是隐藏的。我看到自定义函数 "checkInterval" 只能有两个参数,所以我不能在那里传递行 ID。有什么方法可以获取有关当前正在自定义验证函数中编辑的行的某种信息吗?

我根据另一个网格上的点击事件填充网格,这里是现在的验证函数:

var checkInterval = function (value, colname) {
            value = parseInt(value);
            mange_min = parseInt(menge_min);
            menge_max = parseInt(menge_max);

            if (value < menge_min || value > menge_max) {
                return [false, "რაოდენობა უნდა იყოს " + menge_min + "-" + menge_max + " ინტერვალში"];
            } else {
                return [true];
            }
        }

对于多个可编辑行,这是一种要求,如果没有验证错误弹出窗口,用户知道 he/she 输入了正确的值并继续编辑其他行。 jqGrid版本为4.5.1.

这里是另一个格子的select事件:

onSelectRow: function (id) {
    if (id && id !== lastSel) {
        jQuery(this).restoreRow(lastSel);
        lastSel = id;
        var celValue = jQuery(this).getRowData(id);
        var rowCount = $("#cart").getGridParam("reccount") + 1;
        if (celValue.menge_min == celValue.menge_max )
            celValue.menge = celValue.menge_min;
        var newID = id + rowCount;
        jQuery("#cart").jqGrid('addRowData', newID, celValue);
    }
}

我看到您的问题是访问 custom_func 回调中 menge_min 列中 menge_max 的内容。

您当前的代码将 menge_minmenge_max 设置为 网格最后一行的值 gridComplete 中的循环会覆盖变量的值。

我建议您不要为多行启动编辑模式。它会产生很多问题。例如,可以修改行,但不能保存。排序和过滤(搜索)在网格中被阻止。只有在保存所有行的更改后才能使用这种可能性。通常,一个工具会在单击网格的任何单元格时开始编辑。用户看到编辑开始,he/she 可以按 Enter 保存数据。在编辑行之间,用户可以对网格进行排序或过滤(您可以添加调用 filterToolbar 方法来创建过滤栏)。这样用户可以轻松找到请求的数据行,然后编辑数据。

您目前使用的4.5.1版本已经很旧了。它发表于大约 3 年前。您没有异常 custom_func 的回调,您可以使用它,因为您同时编辑 多行

解决问题的最简单方法是更新到免费的 jqGrid 4.13.0。 Free jqGrid is the fork of jqGrid which I develop starting with the end 2014. It have enchantment which you need. I implemented alternative way to specify custom validation. Instead of usage custom: true, custom_func: checkInterval one can use custom: checkInterval instead (custom property is not Boolean, but callback function instead). In the case the custom callback would get one parameter options with many properties which you can use. The properties are described here (see the comment 了解更多信息)。您可能需要 options.rowid 并使用 var item = $(this).jqGrid("getLocalRow", options.rowid); 来获取 完整的数据项 其中可以使用 item.menge_maxitem.menge_min

此外,我建议您使用 addRow inside of onSelectRow or parent grid. You can use initdata option to specify the data of the grid and no rowID. The documentation of addRow described the default value of rowID incorrectly. The default value of rowID is null, even for the version 4.5.1 (see the line 的代码),jqGrid 在这种情况下会自动生成唯一的 rowid。然后您可以从 gridComplete 中删除代码。如果您要在选择网格中的行时实现开始内联编辑,那么用户可以不受任何限制地执行他需要的所有操作。

最后一句话。如果您要迁移到免费的 jqGrid,您可以从网格中删除 menge_minmenge_maxsvrpos 列。您可以使用 additionalProperties: ["menge_min", "menge_max", "svrpos"] 代替它。它通知 free jqGrid 保存 本地数据中的属性值,但不将信息放在网格的某些隐藏单元格中(DOM 中没有不需要的信息页面)。

您可以通过任何方式从 colModel.

中删除不需要的 sorttype: "string"index 属性