使用jqgrid中的少数其他列的总和创建一个新列

时间:2016-12-20 10:15:03

标签: jqgrid free-jqgrid

我想添加一个包含某些列总和的列。

基本上我想转换以下内容:

enter image description here

以下内容:

enter image description here

但这必须动态完成,即我将提供我希望求和的列的colModel id。

P.S。使用4.13.5版本的free-jqgrid。

1 个答案:

答案 0 :(得分:1)

实现您的要求的最简单方法是使用jsonmapsorttype定义为函数,它返回列的计算值。此外,您需要实现afterSetRow回调,修改行后修复该值(setRowData之后)。

相应的实现可能类似于the demo。该演示使用total列定义网格,该列显示amounttax列的总和。演示代码如下:

var calculateTotal = function (item) {
        return parseFloat(item.amount) + parseFloat(item.tax);
    };

$("#list").jqGrid({
    ...
    colModel: [
        ...
        { name: "amount", template: "number", ... },
        { name: "tax", template: "number", ... },
        { name: "total", width: 76, template: "number", editable: false,
            jsonmap: function (item) {
                return calculateTotal(item);
            },
            sorttype: function (cellValue, item) {
                return calculateTotal(item);
            }},
        ...
    ],
    afterSetRow: function (options) {
        var item = options.inputData;
        if (item.total === undefined) {
            // test is required to prevent recursion
            $(this).jqGrid("setRowData", options.rowid, {
                total: calculateTotal(item)
            });
        }
    }
    ...
});