请选择 进入手机版 | 继续访问电脑版
 找回密码
 立即注册

QQ登录

只需一步,快速开始

Lenka.Guo 讲师达人认证 悬赏达人认证
超级版主   /  发表于:2021-3-1 15:21  /   查看:1621  /  回复:0
应用场景
ActiveReportsJS 擅长构建类报告的报表,报告类的报表往往是多页,且多页分为首页,目录页, 数据明细页,声明页,附件页,尾页等,由多种报表结构组成,而数据明细页,往往是根据数据来自动生成对应数量的页数的,所以就需要我们在运行时,根据实际的检测数据,来控制各个子报表是否显示,可能不同子报表对应的是不同的表格,有数据显示,无数据隐藏,是动态生成报告的核心所在。


解决方法:
子报表是ActiveReportsJS 特有的报表控件,可以实现报表文件嵌套其他报表文件的功能,达到复用报表结构的目的。 可以利用子报表控件来实现动态拼接Word报告类报表。
本文主要演示如何在前端代码实现动态控制报表结构。

操作步骤:
1. 创建多个子报表文件,用于在主报表中引用。

2. 创建一个空白子主报表


3. 在页面代码中,定义一个数组来表示要加载的子报表的顺序。
  1.    var subreports = ['reports/PFCP.rdlx-json', 'reports/DetailIllustration.rdlx-json', 'reports/SIFPP.rdlx-json', 'reports/DetailIllustration.rdlx-json'];
复制代码



4. 用代码定义报表容器控件,容器控件可用于存放各个子报表,用于控制分页
  1.   var containerItems = [];
  2.             var containerTemplate = {
  3.                 "Type": "rectangle",
  4.                 "Top": "0cm",
  5.                 "Left": "0cm",
  6.                 "Width": "19cm",
  7.                 "Height": "2cm",
  8.                 "ConsumeWhiteSpace": true,
  9.                 "PageBreak": "End",
  10.                 "Name": "Container",
  11.                 "ReportItems": [{
  12.                     "Type": "textbox",
  13.                     "Top": "0cm",
  14.                     "Left": "0cm",
  15.                     "Width": "5cm",
  16.                     "Height": "2.5cm",
  17.                     "Name": ""
  18.                 }]
  19.             };
复制代码
5. 定义子报表控件
  1.   var subreportTemplate = {
  2.                 "ZIndex": 1,
  3.                 "Type": "subreport",
  4.                 "Top": "0cm",
  5.                 "Width": "19cm",
  6.                 "Height": "2cm",
  7.                 "ReportName": "",
  8.                 "Name": "Subreport",
  9.                 "Parameters": [{
  10.                         "ParameterName": "p1",
  11.                         "Value": ""
  12.                     },
  13.                     {
  14.                         "ParameterName": "DS",
  15.                         "Value": ""
  16.                     }

  17.                 ]
  18.             };
复制代码
6. 循环子报表的加载列表,并生成对应的容器和子报表控件,为子报表对象指定ReportName与现有的子报表绑定



  1.             subreports.forEach(async (subreport, index) => {
  2.                 var subreportItem = JSON.parse(JSON.stringify(subreportTemplate));
  3.                 var mainParameter = JSON.parse(JSON.stringify(reportparameter));
  4.                 var containerTemplate01 = JSON.parse(JSON.stringify(containerTemplate));

  5.                 //组织子报表的json对象,并装入容器对象中,利用容器的PageBreak属性解决分页问题
  6.                 subreportItem.ReportName = subreport; //指定子报表控件的 ReportName属性。
  7.                 subreportItem.Name = "Subreport" + index; //设置子报表的名称
  8.                 //subreportItem.Top = topPosition + "cm";
  9.                 containerTemplate01.Name = "Container" + index;
  10.                 containerTemplate01.ReportItems.push(subreportItem);
  11.                 containerTemplate01.Top = topPosition + "cm";
  12.                 if (index == subreports.length - 1)
  13.                     containerTemplate01.PageBreak = "";
  14.                 // reportItems.push(subreportItem);
  15.                 containerItems.push(containerTemplate01);
  16.                 if (index = 0)
  17.                     topPosition = 0;
  18.                 else topPosition = topPosition + 5;
  19.             });

  20.             //fetch doesn't work in IE
  21.             fetch(baseUrl + '/reports/MainReport.rdlx-json').then(response => response.json()).then(reportDefinition => {
  22.                 //in case if report is empty at all
  23.                 if (!reportDefinition.Body.ReportItems) reportDefinition.Body.ReportItems = [];
  24.                 reportDefinition.Body.ReportItems.push(...containerItems);
  25.                 reportDefinition.ReportParameters.push(...parameterItems);


  26.                 const viewer = new ActiveReports.Viewer('#ARJSviewerDiv');
  27.                 viewer.open(reportDefinition, {
  28.                     ResourceLocator: resourceProvider
  29.                 });
  30.                 console.log(reportDefinition);
  31.             });
复制代码



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部