2016-12-20 28 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) 
      }); 
     } 
    } 
    ... 
}); 
相關問題