我正在使用pivottable创建和显示动态报表,我想知道有什么方法可以在json上透视子数组中的数据吗?
示例数据:

<script type="text/javascript">
// This example is the most basic usage of pivot()
var data = [
{
"accountId": "50000X",
"productId": 1,
"name": "PRODUCT A",
"sku": "SKU-A",
"fulfillmentSku": "SKU-A",
"friendlySku": "SKU-A",
"quantityStart": 19524,
"quantityEnd": 18523,
"activity": [
{
"eventType": "Assemby",
"createdOnUtc": "2018-01-26T00:00:00",
"quantity": -1
},
{
"eventType": "Deduction",
"createdOnUtc": "2018-01-26T00:00:00",
"quantity": -1500
},
{
"eventType": "Received",
"createdOnUtc": "2018-01-26T00:00:00",
"quantity": 500
}
]
},
{
"accountId": "50000X",
"productId": 97,
"name": "PRODUCT B",
"sku": "SKU-B",
"fulfillmentSku": null,
"friendlySku": "SKU-B",
"quantityStart": -22,
"quantityEnd": 48,
"activity": [
{
"eventType": "Assemby",
"createdOnUtc": "2018-01-26T00:00:00",
"quantity": 60
},
{
"eventType": "Received",
"createdOnUtc": "2018-01-26T00:00:00",
"quantity": 10
}
]
},
{
"accountId": "50000X",
"productId": 96,
"name": "PRODUCT C",
"sku": "SKU-C",
"fulfillmentSku": null,
"friendlySku": "SKU-C",
"quantityStart": 2755,
"quantityEnd": 2755,
"activity": []
},
{
"accountId": "50000X",
"productId": 95,
"name": "PRODUCT D",
"sku": "SKU-C",
"fulfillmentSku": null,
"friendlySku": "SKU-C",
"quantityStart": -11,
"quantityEnd": -6,
"activity": [
{
"eventType": "Assemby",
"createdOnUtc": "2018-01-26T00:00:00",
"quantity": 5
}
]
}
];
$(function () {
$("#output").pivot(data,
{
rows: ["productId", "name", "sku", "quantityStart", "quantityEnd"],
cols: ["activity"]
}
);
});
</script>我想要透视活动数据,活动中的所有内容都是分组和计算的。
或者我必须改变我的json格式才能得到预期的输出?
发布于 2018-01-29 10:26:21
PivotTable.js不能处理这样的嵌套数组(请参阅带有“标量值而不是对象”注释的documentation here ),因此您需要重构输入。
https://stackoverflow.com/questions/48483284
复制相似问题