gpt4 book ai didi

javascript - KendoUI 数据源分组并按多个字段聚合

转载 作者:行者123 更新时间:2023-11-30 15:45:29 26 4
gpt4 key购买 nike

我正在尝试按两个字段对数据源进行分组,并获取它们值的平均值或总和。但即使我在数据源中指定了组和聚合属性,我也无法获取它。

代码如下:

var dataSource = new kendo.data.DataSource({
data: [
{ id: 1, name: "Amazon US", stock: 15, year: 2015},
{id: 2,name: "Amazon US", stock: 20, year: 2016 },
{id: 3,name: "Amazon US", stock: 7, year: 2016 },
{ id: 4, name: "Amazon EU", stock: 30, year: 2015 },
{ id: 5, name: "Amazon EU", stock: 7, year: 2015 },
{ id: 6, name: "Amazon EU", stock: 12, year: 2016 },
{ id: 7, name: "Amazon EU", stock: 26 , year: 2016}
],
schema: { // required if get method will be used
model: {
id: "id"
}
}
})

dataSource.read();

dataSource.bind("change", function() {
var log = $("#log");
log.append("<P>total records in datasource is " + this.total() + "</p>" );
var A = this.aggregates();
log.append("<p>total stock is " + A.stock.sum + "</p>" );
})
.aggregate({ field: "stock", aggregate: "sum" });

这里是一个工作示例: http://jsfiddle.net/rusev/NwG9A/2/

总和的预期结果应该是:

  • 亚马逊美国 | 2015 |十五岁
  • 亚马逊美国 | 2016年 | 27岁
  • 亚马逊欧盟 | 2015 | 37
  • 亚马逊欧盟 | 2016年 | 38

有人知道 KendoUI 数据源是否可行吗?

最佳答案

如果 calinaadi 是正确的方法。还有一种方法可以通过递归以编程方式从 DataSource 中提取聚合信息。每个分组项目都有一个 valueaggregatesitems 字段,您可以使用这些字段 - 在调试器中查看它们。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled</title>

<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.914/styles/kendo.common.min.css">
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.914/styles/kendo.default.min.css">

<script src="http://code.jquery.com/jquery-1.12.3.min.js"></script>
<script src="http://kendo.cdn.telerik.com/2016.3.914/js/kendo.all.min.js"></script>
</head>
<body>

<div id="aggregates"></div>

<div id="grid"></div>

<script>

var dataSource = new kendo.data.DataSource({
data: [
{ id: 1, name: "Amazon US", stock: 15, year: 2015},
{id: 2,name: "Amazon US", stock: 20, year: 2016 },
{id: 3,name: "Amazon US", stock: 7, year: 2016 },
{ id: 4, name: "Amazon EU", stock: 30, year: 2015 },
{ id: 5, name: "Amazon EU", stock: 7, year: 2015 },
{ id: 6, name: "Amazon EU", stock: 12, year: 2016 },
{ id: 7, name: "Amazon EU", stock: 26 , year: 2016}
],
schema: {
model: {
id: "id"
}
},
group: [
{ field: "name", aggregates: [{ field: "stock", aggregate: "sum" }] },
{ field: "year", aggregates: [{ field: "stock", aggregate: "sum" }] }
],
aggregate: [
{ field: "stock", aggregate: "sum" }
]
});

$("#grid").kendoGrid({
dataSource: dataSource,
dataBound: onDataBound,
groupable: true,
columns: [
{ field: "stock", groupFooterTemplate: "#= sum #", footerTemplate: "#= sum #" },
{ field: "name" },
{ field: "year" }
]
});

function onDataBound(e) {
var ds = e.sender.dataSource;
var dataItems = ds.view();
var aggregatesResult = '<p>Sum of all stock: ' + ds.aggregates().stock.sum + '</p>'
aggregatesResult += '<ul>';
for (var i = 0; i < dataItems.length; i++) {
aggregatesResult += getFieldAndSum(dataItems[i], '');
}
aggregatesResult += '</ul>';

$("#aggregates").html(aggregatesResult);
}

function getFieldAndSum(dataItem, parentValue) {
var result = '<li>' + (dataItem.value ? (parentValue + ' ' + dataItem.value + ' ' + dataItem.aggregates.stock.sum) : dataItem.stock);
if (dataItem.items && dataItem.items.length) {
for (var j = 0; j < dataItem.items.length; j++) {
result += '<ul>';
result += getFieldAndSum(dataItem.items[j], parentValue + ' ' + dataItem.value);
result += '</ul>';
}
}
result += '</li>';
return result;
}

</script>

</body>
</html>

关于javascript - KendoUI 数据源分组并按多个字段聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40129356/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com