使用流畅的报表添加多个表

  • 本文关键字:添加 报表 node.js
  • 更新时间 :
  • 英文 :


我有三个数组"listA,listB,listC",我想从这些列表中创建三个表。到目前为止,我只能创建一个表,并且我无法弄清楚如何使用文档做我想做的事情。FluentReports Documentaiton

这是我到目前为止所拥有的,我正在查看 .addReport 并尝试使用它,但我收到错误"未处理的拒绝类型错误:rpt.addReport 不是一个函数"。我可以得到一些帮助来显示多个表格吗?我的代码如下。

const express = require('express');
const db = require('../config/db');
const router = new express.Router();
const PDFDocument =  require('pdfkit') 
const Report = require('fluentreports').Report
var fs = require("fs")
router.get('/getpdf', (req, res) => {
  res.type('application/pdf');
   // Our Simple Data in Object format: 
  
  var results1;
  db.sequelize.query("SELECT device."deviceID", device."deviceType", event.data, event."event_time_stamp" FROM  public.device, public.event WHERE  event."device_deviceID" = device."deviceID"").spread((results, metadata) => {
    
      //console.log(results)
      var listA = [];
      var listB = [];
      var listC = [];
      results.map(cv => {
        switch (cv.deviceType) {
          case 'type1':
            listA.push(cv);
            break;
          case 'type2':
            listB.push(cv);
            break;
          case 'type3':
            listC.push(cv);
            break;
            
          }
      });
    
   
   
    listA.map(cv => {
  [cv.quantity, cv.additional] = cv.data.split(','); 
  cv.deviceType =  'Poison Grizach';
  cv.quantity = cv.quantity + '%'
  delete cv.data;
});
    listB.map(cv => {
  [cv.mouse, cv.rat, cv.other, cv.additional] = cv.data.split(','); 
  cv.deviceType =  'Non-Poison Grizach';
  delete cv.data;
});
    listC.map(cv => {
  [cv.flies, cv.quantity, cv.additional] = cv.data.split(','); 
  cv.deviceType =  'Insect Lamp';
  cv.flies =  cv.flies + '%'
  delete cv.data;
});
   var all = listA.concat(listB).concat(listC)
   
 var headerFunction = function(Report) {
        Report.print("Device Scans", {fontSize: 22, bold: true, underline:true, align: "center"});
        Report.newLine(2);
        Report.band([
          {data: 'Device Type', width: 100},
          {data: 'Quantity', width: 50},
          {data: 'Mouse', width: 50},
           {data: 'Rat', width: 50},
          {data: 'Other', width: 50},
          {data: 'Additional', width: 50},
          {data: '% Myxi', width: 50},
      ]);
    };
// Create a Report   
  var rpt = new Report(res,({fontSize: 10}))     
        .pageHeader( ["Device Scans"] )     // Add a simple (optional) page Header...         
        .margins(40)
        .data( listA )               // Add some Data (This is required) 
        .pageHeader(headerFunction) 
        .detail( [ ['deviceType', 100], ['quantity', 50] , ['mouse', 50], ['rat', 50], ['other', 50], ['additional', 50] , ['flies', 50]]) // Layout the report in a Grid of 200px & 50px 
        .render()
  var rpt2 = new Report(res,({fontSize: 10}))     
        .pageHeader( ["Device Scans"] )     // Add a simple (optional) page Header...         
        .margins(40)
        .data( listB )               // Add some Data (This is required) 
        .pageHeader(headerFunction) 
        .detail( [ ['deviceType', 100] , ['mouse', 50], ['rat', 50], ['other', 50], ['additional', 50] ]) // Layout the report in a Grid of 200px & 50px 
  
    rpt.addReport(rpt2)
  })  

       
  });
module.exports = router;

解决方案

包含在下面。父报告 rpt 包含在"同级"报告的创建中。渲染是从此实现中的最后一个同级调用的,选项为 isSibling:true。头文件被定义为提供列名称

获取请求:

router.get('/getpdf', (req, res) => {
  res.type('application/pdf');
   // Our Simple Data in Object format: 
 
  db.sequelize.query("SELECT device."deviceID", device."deviceType", event.data, event."event_time_stamp" FROM  public.device, public.event WHERE  event."device_deviceID" = device."deviceID"").spread((results, metadata) => {
    
      //console.log(results)
      var listA = [];
      var listB = [];
      var listC = [];
      results.map(cv => {
        switch (cv.deviceType) {
          case 'type1':
            listA.push(cv);
            break;
          case 'type2':
            listB.push(cv);
            break;
          case 'type3':
            listC.push(cv);
            break;
            
          }
      });
    
   
   
    listA.map(cv => {
  [cv.quantity, cv.additional] = cv.data.split(','); 
  cv.deviceType =  'Poison Grizach';
  cv.quantity = cv.quantity + '%'
  delete cv.data;
});
    listB.map(cv => {
  [cv.mouse, cv.rat, cv.other, cv.additional] = cv.data.split(','); 
  cv.deviceType =  'Non-Poison Grizach';
  delete cv.data;
});
    listC.map(cv => {
  [cv.flies, cv.quantity, cv.additional] = cv.data.split(','); 
  cv.deviceType =  'Insect Lamp';
  cv.flies =  cv.flies + '%'
  delete cv.data;
});
   var all = listA.concat(listB).concat(listC)
   
 var headerFunction = function(Report) {
        
       
        Report.newLine(2);
        Report.band([
          {data: 'Device Type', width: 100},
          {data: 'Quantity', width: 50},
          {data: 'Additional', width: 50},
          
      ]);
};
 var headerFunction2 = function(Report) {
        
        Report.newLine(2);
        Report.band([
          {data: 'Device Type', width: 100},
          {data: 'Mouse', width: 50},
          {data: 'Rat', width: 50},
          {data: 'Other', width: 50},
          {data: 'Additional', width: 50},
          
      ]);
};
 var headerFunction3 = function(Report) {
        
        Report.newLine(2);
        Report.band([
          {data: 'Device Type', width: 100},
          {data: 'Burned Bulbs', width: 80},
          {data: '% Flies', width: 50},
          {data: 'Change Board', width: 80},
          
      ]);
};
// Create a Report   
  var rpt = new Report(res,({fontSize: 10}))
        .titleHeader("Device Scans")
        .margins(40)
        .data( {})               // Add some Data (This is required) 
       
  var rpt1 = new Report(rpt,({fontSize: 10}))
        .margins(40)
        .data( listA )               // Add some Data (This is required) 
        .pageHeader(headerFunction) 
        .detail( [ ['deviceType', 100], ['quantity', 50] , ['additional', 50]]) // Layout the report in a Grid of 200px & 50px       
  var rpt2 = new Report(rpt, ({isSibling: true}))     
        .margins(40)
        .data( listB )               // Add some Data (This is required) 
        .pageHeader(headerFunction2) 
        .detail( [ ['deviceType', 100] , ['mouse', 50], ['rat', 50], ['other', 50], ['additional', 50] ]) // Layout the report in a Grid of 200px & 50px 
        
  var rpt3 = new Report(rpt, ({isSibling: true}))     
         .pageHeader( ["Device Scans"] )
        .margins(40)
        .data( listC )               // Add some Data (This is required) 
        .pageHeader(headerFunction3) 
        .detail( [ ['deviceType', 100] , ['quantity', 80] , ['flies', 50] ,['additional', 80] ]) // Layout the report in a Grid of 200px & 50px 
        .render()
   
  })  

最新更新