有没有办法通过分组在Slickgrid中显示总计?
我找到了以下内容:
1)在文档Implementing a totals row via a data provider中。但后来我无法使用DataView和分组功能。
2)SlickGrid有一个名为Slickgrid totals plugin的插件。但它不允许使用分组功能。
答案 0 :(得分:1)
- 添加TotalsDataProvider功能
醇>
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();
}
- 添加选项&#34; hasTotal:true&#34;在你的专栏中。例如:
醇>
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"}
];
- 更改网格变量并在上面添加dataProvider变量:
醇>
//grid = new Slick.Grid("#myGrid", dataView, columns, options);
var dataProvider = new TotalsDataProvider(dataView, columns);
grid = new Slick.Grid("#myGrid", dataProvider, columns, options);
- 每次更改数据时(通过过滤或通过grid.onCellCange),您都需要更新总数据:
醇>
// The data has changed - recalculate the totals.
dataProvider.updateTotals();
// Rerender the totals row (last row).
grid.invalidateRow(dataProvider.getLength() - 1);
grid.render();
答案 1 :(得分:0)
我找到了一些解决方法。算法如下:
noGrouping:true
DataView
dataView.setItems(JSONDataArray)
在功能slick.dataview.js
的{{1}}中,在行之后:
addTotals
添加:
g.collapsed = groupCollapsed ^ toggledGroups[g.groupingKey];
因此总计小组总是崩溃
如果需要,请在if(g.rows[0].noGrouping) {
g.collapsed = true;
}
中为功能slick.groupitemmetadataprovider.js
添加一些格式