0

我正在使用数据透视表来创建和显示动态报告,我想知道有没有办法在 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 格式才能获得预期的输出?

4

1 回答 1

0

PivotTable.js 无法处理这样的嵌套数组(请参阅此处的文档以及有关“标量值而非对象”的注释),因此您需要重新构造输入。

于 2018-01-29T02:26:21.417 回答