【问题标题】:Create a new column with sum of few other columns in jqgrid在 jqgrid 中创建一个包含其他几列总和的新列
【发布时间】:2016-12-20 10:15:03
【问题描述】:

我想添加一列,其中包含一些列的总和。

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

到以下:

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

附:使用free-jqgrid 4.13.5版本。

【问题讨论】:

  • 解决问题的方法有多种,但最佳选择取决于其他细节。你用的是哪个datatype?您需要编辑数据吗?您还需要在“全部”列中使用哪些其他功能(排序、搜索……)?
  • @Oleg 数据类型为默认值。我将数组数据提供给 jqgrid 对象的数据键。不需要编辑数据。需要排序/搜索。

标签: jqgrid free-jqgrid


【解决方案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)
            });
        }
    }
    ...
});

【讨论】:

    猜你喜欢
    • 2019-02-13
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    相关资源
    最近更新 更多