node.js - 使用 FluentReports 添加多个表

标签 node.js

我有三个数组“listA、listB、listC”,我想从这些列表创建三个表。到目前为止,我只能创建一个表,而且我无法弄清楚如何使用文档来做我想做的事情。 fluentreports documentaiton

这是我到目前为止所拥有的,我正在查看 .addReport 并尝试使用它,但我收到错误“未处理的拒绝 TypeError: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 选项从最后一个同级调用 Render。头文件被定义为给出列名称

获取请求:

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()

   
  })  

关于node.js - 使用 FluentReports 添加多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44467522/

相关文章:

php - Socket.io和ajax请求php页面

node.js - 在 Node.js 退出之前执行清理操作

javascript - Yargs.argv 始终是 [object Object]

node.js - 如何调试对通过 Grunt 构建的 Connect 服务器的请求?

javascript - Express.js 服务器端渲染 - 请求 '/json/version/

node.js - Firebase 经过身份验证的 HTTP 端点 - 云功能,它们安全吗?

node.js - nodejs 中的 Typescript Reflect-MetaData

javascript - 如何在 node.js 中保持 mysql 连接?

node.js - "npm --save install <module>"没有实际安装模块

node.js - Vue Electron 在异步调用一些 Node 库时卡住了