简体   繁体   English

jqgrid - 内联编辑,仅发布更改的数据

[英]jqgrid - inline editing, post only changed data

I have a jqGrid where I would like to check for cell data and only post the columns of which the data has changed. 我有一个jqGrid,我想检查单元格数据,只发布数据已更改的列。

Consider this is my colModel 考虑这是我的colModel

colModel: [{
    name: 'I_PK',
    index: 'u.I_PK',
    align: 'right',
    editable: false,
    sopt: ['cn', 'eq', 'ne', 'lt', 'le', 'gt', 'ge', 'bw', 'ew', 'nc']
}, {
    name: 'W3LabelSelected',
    index: 'u.W3LabelSelected',
    align: 'center',
    width: 170,
    editable: false,
    edittype: 'checkbox',
    formatter: "checkbox",
    search: false,
    formatoptions: {
        disabled: false
    },
    editoptions: {
        value: "1:0"
    }
}, {
    name: 'I_ItemNumID',
    index: 'u.I_ItemNumID',
    align: 'right',
    editable: true,
    editoptions: {
        dataEvents: [{
            type: 'focusin',
            fn: function (e) {
                var elem = e.target;
                setTimeout(function () {
                    elem.select();
                }, 50);
            }
        }]
    }
}, {
    name: 'Quantity',
    index: 'u.Quantity',
    align: 'right',
    editable: true,
    editoptions: {
        dataEvents: [{
            type: 'focusin',
            fn: function (e) {
                var elem = e.target;
                setTimeout(function () {
                    elem.select();
                }, 50);
            }
        }]
    }
}],

In this grid, two of my columns are editable. 在此网格中,我的两列可编辑。 Now let's say if I make changes in one of the columns of any row in inline editing, then only that cell's value should be posted. 现在让我们说如果我在内联编辑中的任何一行的列中进行更改,那么只应该发布该单元格的值。 Current functionality posts all the cells of that specific row. 当前功能发布该特定行的所有单元格。 Is this possible? 这可能吗?

I found some questions here and there about this but none seem to be addressing this specific problem. 我在这里那里发现了一些问题,但似乎都没有解决这个具体问题。

Basically the idea I can think of is that before saving, if I can somehow compare the original data of all the editable cells of that row to the new value before getting posted, I can eliminate the cells of which the data has not changed and only post the cell which has changed. 基本上我能想到的想法是,在保存之前,如果我能够以某种方式将该行的所有可编辑单元格的原始数据与发布之前的新值进行比较,我可以消除数据未更改的单元格,仅发布已更改的单元格。

Sample grid: http://jsfiddle.net/dipenshah8/HJema/203/ 示例网格: http//jsfiddle.net/dipenshah8/HJema/203/

I would suggest you to use serializeSaveData callback of inline editing or serializeRowData callback of old versions of jqGrid. 我建议你使用serializeSaveData联编辑或回调serializeRowData旧版本的jqGrid的回调。 The callback allows you to customize the data, which will be send to the server. 回调允许您自定义将发送到服务器的数据。 The modified demo http://jsfiddle.net/OlegKi/HJema/206/ uses the following options: 修改后的演示http://jsfiddle.net/OlegKi/HJema/206/使用以下选项:

inlineEditing: {
    keys: true,
    serializeSaveData: function (postData) {
        var changedData = {}, prop, p = $(this).jqGrid("getGridParam"),
            idname = p.keyName || p.prmNames.id;

        for (prop in postData) {
            if (postData.hasOwnProperty(prop) &&
                (postData[prop] !== p.savedRow[0][prop] || prop === idname)) {
                changedData[prop] = postData[prop];
            }
        }
        alert(JSON.stringify(changedData));
        return changedData;
    }
}

The code of serializeSaveData callback enumerate all properties which will be send to the server by default and generate new object changedData instead of postData . serializeSaveData回调的代码枚举默认情况下将发送到服务器的所有属性,并生成新对象changedData而不是postData The code compare the value of all properties of postData with the corresponding values of savedRow[0] parameter, which contains the row before starting editing. 代码将postData的所有属性的值与savedRow[0]参数的对应值进行savedRow[0] ,该参数包含开始编辑之前的行。

UPDATED : The above code should be a little complexer if the data could have formatter: "date" . 更新 :如果数据可能有formatter: "date" ,上面的代码应该有点复杂。 jqGrid saves formatted value in savedRow[0] . jqGrid在savedRow[0]保存格式化值。 One can modify the above code to the following: 可以将上面的代码修改为以下代码:

inlineEditing: {
    keys: true,
    serializeSaveData: function (postData) {
        var changedData = {}, prop, p = $(this).jqGrid("getGridParam"),
            idname = p.keyName || p.prmNames.id,
            oldValue, cm, formatoptions;

        for (prop in postData) {
            oldValue = p.savedRow[0][prop];
            if (p.iColByName[prop] != null) {
                cm = p.colModel[p.iColByName[prop]];
                formatoptions = cm.formatoptions || {};
                if (cm.formatter === "date" && formatoptions.sendFormatted !== true) {
                    oldValue = $.unformat.date.call(this, oldValue, cm);
                }
            }
            if (postData.hasOwnProperty(prop) &&
                    (postData[prop] !== oldValue || prop === idname)) {
                changedData[prop] = postData[prop];
            }
        }
        alert(JSON.stringify(changedData));
        return changedData;
    }
}

See the modified demo http://jsfiddle.net/OlegKi/HJema/209/ 查看修改后的演示http://jsfiddle.net/OlegKi/HJema/209/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM