2016-12-20 29 views
1

我想添加一列,其中将包含一些列的总和。在jqgrid中创建一个新列与其他列的总和

基本上我想下面的转换:

enter image description here

以下几点:

enter image description here

但是这必须动态完成的,即我应提供前作列colModel ID我想要的总和。

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

+0

有多种方法可以解决问题,但最好的选择取决于其他细节。你使用哪种'datatype'?你需要编辑数据吗?您需要在“全部”(排序,搜索...)列中使用哪些其他功能? – Oleg

+0

@Oleg数据类型是default.I将数组数据提供给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) 
      }); 
     } 
    } 
    ... 
}); 
相关问题