在 jqgrid 中创建一个包含其他几列总和的新列

Create a new column with sum of few other columns in jqgrid

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

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

以下内容:

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

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

实现您的要求的最简单方法是使用 jsonmapsorttype 定义为函数,returns 列的计算值。此外,您需要实施 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)
            });
        }
    }
    ...
});