将预定义的数据源与 ActiveReportsJS 报表设计器一起用于嵌套数据

问题描述 投票:0回答:2

我在我的 React 应用程序中使用 @grapecity/activereports-react。我正在使用我的 mongodb 数据库作为数据源。我创建了服务器和端点来从数据库中读取我的数据。

// Define an endpoint that retrieves data from each collection
app.get('/:collectionName', async (req, res) => {
  try {
    const collectionName = req.params.collectionName;
    const collection = mongoose.connection.collection(collectionName);
    const data = await collection.find().toArray();
    res.json(data);
  } catch (err) {
    console.error(err);
    res.status(500).send('Server error');
  }
});

我定义了我的数据源;

  const Jforce_DataSource = {
    Name: 'Jforce',
    ConnectionProperties: {
      DataProvider: 'JSON',
      ConnectString: 'endpoint=http://localhost:3001/' ,
    },
  };

  const dataSources = [
    {
      id: 'Jforce',
      title: 'Jforce',
      template: Jforce_DataSource,
      canEdit: false,
      datasets: collections.map((collection) => {
        return {
          id: collection,
          title: collection,
          template: {
            Name: collection,
            Query: { DataSourceName: 'Jforce', CommandText: `uri=/${collection};jpath=$.[*]` },
            Fields: [
              { Name: '_id', DataField: '_id' },
              { Name: 'Date', DataField: 'Run.Date' },
              { Name: 'Index', DataField: 'Run.Index' },
              { Name: 'ScenarioName', DataField: 'Run.Scenario.Name' },
              { Name: 'ScenarioDatabase', DataField: 'Run.Scenario.Database' },
              { Name: 'SvnAddress', DataField: 'Run.Svn.Address' },
              { Name: 'SvnRevision', DataField: 'Run.Svn.Revision' },

              { Name: 'PlatformProfile', DataField: 'Statistics.Platforms.Profile' },
              { Name: 'PlatformType', DataField: 'Statistics.Platforms.Type' },
              { Name: 'PlatformForce', DataField: 'Statistics.Platforms.Force' },
              { Name: 'PlatformTotal', DataField: 'Statistics.Platforms.Total' },
              { Name: 'PlatformWrecked', DataField: 'Statistics.Platforms.Wrecked' },
              { Name: 'PlatformDestroyed', DataField: 'Statistics.Platforms.Destroyed' },
            ],
          },
          canEdit: true,
        };
      }),
    },
  ];

我的数据是嵌套的。我可以找回

_id,Run.Date,Run.Index,Run.Scenario.Name ,Run.Scenario.Database, Run.Svn.Address and Run.Svn.Revision
但我无法得到
Statistics.Platforms,Statistics.Weapons ,Variables or Measurements
designer viewer

这是我的嵌套数据;


{
  "_id": {
    "$oid": "639714f6dc06372526070821"
  },
  "Run": {
    "Date": {
      "$date": "2022-12-12T11:48:06Z"
    },
    "Index": 1,
    "Scenario": {
      "Name": "YZU_senaryo1",
      "Database": "default"
    },
    "Svn": {
      "Address": "",
      "Revision": 0
    }
  },
  "Statistics": {
    "Platforms": [
      {
        "Profile": "F",
        "Type": "W",
        "Force": "B",
        "Total": 1,
        "Wrecked": 0,
        "Destroyed": 0
      },
      {
        "Profile": "F",
        "Type": "W",
        "Force": "B",
        "Total": 1,
        "Wrecked": 0,
        "Destroyed": 0
      },
      {
        "Profile": "O",
        "Type": "S",
        "Force": "R",
        "Total": 1,
        "Wrecked": 0,
        "Destroyed": 0
      }
    ],
    "Weapons": [
      {
        "Profile": "S",
        "Type": "M",
        "Force": "B",
        "Total": 2,
        "Hit": 0,
        "Missed": 2
      },
      {
        "Profile": "R",
        "Type": "M",
        "Force": "R",
        "Total": 2,
        "Hit": 2,
        "Missed": 0
      }
    ]
  },
  "Variables": [
    {
      "Type": "PlatformR",
      "Configuration": "InitialValue: 0",
      "Vaue": 0
    },
    {
      "Type": "PlatformR",
      "Configuration": "InitialValue: 30",
      "Vaue": 30
    },
    {
      "Type": "PlatformD",
      "Configuration": "InitialValue: 50000",
      "Vaue": 50000
    },
    {
      "Type": "PlatformD",
      "Configuration": "InitialValue: 50000",
      "Vaue": 50000
    },
    {
      "Type": "WeaponR",
      "Configuration": "InitialValue: 10000",
      "Vaue": 50000
    },
    {
      "Type": "LoadedW",
      "Configuration": "InitialValue: 1",
      "Vaue": 1
    },
    {
      "Type": "LoadedW",
      "Configuration": "InitialValue: 1",
      "Vaue": 1
    }
  ],
  "Measurements": [
    {
      "Type": "WEAPON_L",
      "Configuration": " [Entity:Destroyer]",
      "Value": 0,
      "Details": {}
    },
    {
      "Type": "WEAPON_L",
      "Configuration": " [Force:RED]",
      "Value": 2,
      "Details": {
        "AverageRange": 24813,
        "Range": [
          24880,
          24747
        ]
      }
    },
    {
      "Type": "WEAPON_H",
      "Configuration": " [Force:BLUE]",
      "Value": 0,
      "Details": {}
    },
    {
      "Type": "WEAPON_I",
      "Configuration": " [Force:BLUE]",
      "Value": 2,
      "Details": {
        "AverageRange": 14375,
        "Range": [
          14420,
          14329
        ]
      }
    },
    {
      "Type": "PLATFORM",
      "Configuration": " [Entity:Destroyer]",
      "Value": 0,
      "Details": {}
    },
    {
      "Type": "WEAPON",
      "Configuration": " [Force:BLUE]",
      "Value": 2,
      "Details": {
        "AverageRange": 49913,
        "Range": [
          50166,
          49661
        ]
      }
    },
    {
      "Type": "WEAPON",
      "Configuration": " [Force:BLUE]",
      "Value": 0,
      "Details": {}
    }
  ]
}


我试过了;

          { Name: 'PlatformProfile', DataField: 'Statistics.Platforms[*].Profile' },
          { Name: 'PlatformType', DataField: 'Statistics.Platforms[*].Type' },
          { Name: 'PlatformForce', DataField: 'Statistics.Platforms[*].Force' },
          { Name: 'PlatformTotal', DataField: 'Statistics.Platforms[*].Total' },
          { Name: 'PlatformWrecked', DataField: 'Statistics.Platforms[*].Wrecked' },
          { Name: 'PlatformDestroyed', DataField: 'Statistics.Platforms[*].Destroyed' },

          { Name: 'WeaponProfile', DataField: 'Statistics.Weapons[0].Profile' },
          { Name: 'WeaponType', DataField: 'Statistics.Weapons[0].Type' },
          { Name: 'WeaponForce', DataField: 'Statistics.Weapons[0].Force' },
          { Name: 'WeaponTotal', DataField: 'Statistics.Weapons[0].Total' },
          { Name: 'WeaponHit', DataField: 'Statistics.Weapons[0].Hit' },
          { Name: 'WeaponMissed', DataField: 'Statistics.Weapons[0].Missed' },

他们没有工作。如果你有帮助,我会很高兴。

node.js reactjs activereports grapecity
2个回答
0
投票

要使用 ActiveReports React 访问 MongoDB 数据源中的嵌套数据,您可以使用点符号遍历嵌套对象。例如,要访问 Platforms 数组的第一个对象中的 Profile 字段,您可以使用 Statistics.Platforms.0.Profile。以下是如何更新字段定义的示例:

Fields: [
  { Name: '_id', DataField: '_id' },
  { Name: 'Date', DataField: 'Run.Date' },
  { Name: 'Index', DataField: 'Run.Index' },
  { Name: 'ScenarioName', DataField: 'Run.Scenario.Name' },
  { Name: 'ScenarioDatabase', DataField: 'Run.Scenario.Database' },
  { Name: 'SvnAddress', DataField: 'Run.Svn.Address' },
  { Name: 'SvnRevision', DataField: 'Run.Svn.Revision' },

  { Name: 'PlatformProfile', DataField: 'Statistics.Platforms.0.Profile' },
  { Name: 'PlatformType', DataField: 'Statistics.Platforms.0.Type' },
  { Name: 'PlatformForce', DataField: 'Statistics.Platforms.0.Force' },
  { Name: 'PlatformTotal', DataField: 'Statistics.Platforms.0.Total' },
  { Name: 'PlatformWrecked', DataField: 'Statistics.Platforms.0.Wrecked' },
  { Name: 'PlatformDestroyed', DataField: 'Statistics.Platforms.0.Destroyed' },

  { Name: 'WeaponProfile', DataField: 'Statistics.Weapons.0.Profile' },
  { Name: 'WeaponType', DataField: 'Statistics.Weapons.0.Type' },
  { Name: 'WeaponForce', DataField: 'Statistics.Weapons.0.Force' },
  { Name: 'WeaponTotal', DataField: 'Statistics.Weapons.0.Total' },
  { Name: 'WeaponHit', DataField: 'Statistics.Weapons.0.Hit' },
  { Name: 'WeaponMissed', DataField: 'Statistics.Weapons.0.Missed' },
]

注意,使用点号来遍历嵌套对象和数组,在字段名后追加数组元素的索引。例如,Statistics.Platforms.0.Profile 访问 Platforms 数组中第一个对象的 Profile 字段。您可以使用相同的方法访问数据中的其他嵌套字段。


0
投票

没问题。要访问案例中的嵌套数据,您需要在数据集模板的 CommandText 属性中使用 JSONPath 表示法。以下是如何获取 Platforms 数组的示例:

{ Name: 'PlatformProfile', DataField: 'Statistics.Platforms[*].Profile' },
{ Name: 'PlatformType', DataField: 'Statistics.Platforms[*].Type' },
{ Name: 'PlatformForce', DataField: 'Statistics.Platforms[*].Force' },
{ Name: 'PlatformTotal', DataField: 'Statistics.Platforms[*].Total' },
{ Name: 'PlatformWrecked', DataField: 'Statistics.Platforms[*].Wrecked' },
{ Name: 'PlatformDestroyed', DataField: 'Statistics.Platforms[*].Destroyed' },

请注意,[*] 语法用于指示我们要从 Platforms 数组中的所有元素中提取值。

获取Weapons数组,可以使用类似的表示法:

{ Name: 'WeaponProfile', DataField: 'Statistics.Weapons[*].Profile' },
{ Name: 'WeaponType', DataField: 'Statistics.Weapons[*].Type' },
{ Name: 'WeaponForce', DataField: 'Statistics.Weapons[*].Force' },
{ Name: 'WeaponTotal', DataField: 'Statistics.Weapons[*].Total' },
{ Name: 'WeaponHit', DataField: 'Statistics.Weapons[*].Hit' },
{ Name: 'WeaponMissed', DataField: 'Statistics.Weapons[*].Missed' },

要获取变量数组,您可以使用:

{ Name: 'VariableType', DataField: 'Variables[*].Type' },
{ Name: 'VariableConfiguration', DataField: 'Variables[*].Configuration' },
{ Name: 'VariableValue', DataField: 'Variables[*].Value' },

最后,要获取 Measurements 数组,您可以使用:

{ Name: 'MeasurementType', DataField: 'Measurements[*].Type' },
{ Name: 'MeasurementConfiguration', DataField: 'Measurements[*].Configuration' },
{ Name: 'MeasurementValue', DataField: 'Measurements[*].Value' },

确保调整每个数据集中的 CommandText 属性,以针对每种情况使用适当的 JSONPath 语法。

© www.soinside.com 2019 - 2024. All rights reserved.