Slickgrid总计

时间:2014-04-29 07:12:47

标签: jquery slickgrid

有没有办法通过分组在Slickgrid中显示总计?

我找到了以下内容:

1)在文档Implementing a totals row via a data provider中。但后来我无法使用DataView和分组功能。

2)SlickGrid有一个名为Slickgrid totals plugin的插件。但它不允许使用分组功能。

2 个答案:

答案 0 :(得分:1)

  
      
  1. 添加TotalsDataProvider功能
  2.   
function TotalsDataProvider(dataView, columns) {
  var totals = {};
  var totalsMetadata = {
  // Style the totals row differently.
    cssClasses: "totals",
    columns: {}
  };
  // Make the totals not editable.
  for (var i = 0; i < columns.length; i++) {
    totalsMetadata.columns[i] = { editor: null };
  }
  this.getLength = function() {
    return dataView.getLength() + 1;
  }
  this.getItem = function(index) {
    return (index < dataView.getLength()) ? dataView.getItem(index) : totals;
  };

  // Some group-functions from DataView
  this.setRefreshHints = function(hints) {
    return dataView.setRefreshHints(hints);
  };
  this.collapseGroup = function(varArgs) {
    return dataView.collapseGroup(varArgs);
  };
  this.expandGroup = function(varArgs) {
    return dataView.expandGroup(varArgs);
  };

  this.updateTotals = function() {
    var columnIdx = columns.length;
    while (columnIdx--) {
      var column = columns[columnIdx];
      if (!column.hasTotal) { // Just add in your column "hasTotal: true" 
        continue;
      }
      var total = 0;
      var i = dataView.getLength();
      while (i--) {
        total += (dataView.getItem(i)[column.field] || 0);
      }
      totals[column.field] = total;
    }
  };
  this.getItemMetadata = function(index) {
    return (index != dataView.getLength()) ? dataView.getItemMetadata(index) : totalsMetadata;
  };
  this.updateTotals();
}
  
      
  1. 添加选项&#34; hasTotal:true&#34;在你的专栏中。例如:
  2.   
var columns = [
        {id: "id", name: "Index", field: "id", hasTotal: true},
        {id: "title", name: "Title", field: "title"},
        {id: "duration", name: "Duration", field: "duration", hasTotal: true},
        {id: "%", name: "% Complete", field: "percentComplete", hasTotal: true},
        {id: "start", name: "Start", field: "start"},
        {id: "finish", name: "Finish", field: "finish"},
        {id: "effort-driven", name: "Effort Driven", field: "effortDriven"}
      ];
  
      
  1. 更改网格变量并在上面添加dataProvider变量:
  2.   
//grid = new Slick.Grid("#myGrid", dataView, columns, options);
var dataProvider = new TotalsDataProvider(dataView, columns);
grid = new Slick.Grid("#myGrid", dataProvider, columns, options);
  
      
  1. 每次更改数据时(通过过滤或通过grid.onCellCange),您都需要更新总数据:
  2.   
// The data has changed - recalculate the totals.
dataProvider.updateTotals(); 
// Rerender the totals row (last row).     
grid.invalidateRow(dataProvider.getLength() - 1);
grid.render();

答案 1 :(得分:0)

我找到了一些解决方法。算法如下:

  1. 收集JSON数据
  2. 计算所需列的总计数
  3. 使用额外的参数(例如noGrouping:true
  4. )在JSON数据数组的底部推送额外的行(数组元素)
  5. 使用DataView
  6. 将数据推送到dataView.setItems(JSONDataArray)
  7. 在功能slick.dataview.js的{​​{1}}中,在行之后:

    addTotals添加:

    g.collapsed = groupCollapsed ^ toggledGroups[g.groupingKey];

    因此总计小组总是崩溃

  8. 如果需要,请在if(g.rows[0].noGrouping) { g.collapsed = true; } 中为功能slick.groupitemmetadataprovider.js添加一些格式