如何解决:无法在字符串上创建属性“标头" [英] How to fix: Cannot create property 'header' on string

查看:54
本文介绍了如何解决:无法在字符串上创建属性“标头"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据json数据使用jspdf创建pdf.但这会导致我无法理解的错误.请帮我解决问题.

I'm trying to create a pdf using jspdf from json data. But it is causing an error which i'm not able to understand. Please help me solve the issue.

数据:

fileDataSpecific = [{ Date: "01/Jan/2019",
    ServerName: "prlhpcms01",
    ServerRequest: "Front End License returned",
    ServerStatus: "57 timed out",
    Time: "01:03:32",
    UserName: "root",
    srno: 1 }]

displayColumns = [
    { "val": "srno", "col": "Sr. No." },
    { "val": "ServerName", "col": "Server Name" },
    { "val": "UserName", "col": "User Name" },
    { "val": "Date", "col": "Date" },
    { "val": "Time", "col": "Time" },
    { "val": "ServerRequest", "col": "Server Request" },
    { "val": "ServerStatus", "col": "Server Status" }];

我的功能:

PrintData() {
    var pdfData = this.fileDataSpecific
    var pdfCol = this.displayedColumns
    var doc = new jsPDF()
    var col = [], row = []
    pdfCol.forEach(element => {
      col.push(element.col)
    });    
    pdfData.forEach(element => {
      var tempArray = []
      pdfCol.forEach(element1 => {
        tempArray.push(element[element1.val])
      });
      row.push(tempArray)
      tempArray = []
    });
    doc.autoTable(col, row);
    doc.save('Test.pdf');
  }

错误:

请有人帮助!谢谢.

推荐答案

您需要将var pdfData = this.fileDataSpecific视为数组var pdfData = [this.fileDataSpecific]

fileDataSpecific = { Date: "01/Jan/2019",
    ServerName: "prlhpcms01",
    ServerRequest: "Front End License returned",
    ServerStatus: "57 timed out",
    Time: "01:03:32",
    UserName: "root",
    srno: 1 }

let displayedColumns = [
    { "val": "srno", "col": "Sr. No." },
    { "val": "ServerName", "col": "Server Name" },
    { "val": "UserName", "col": "User Name" },
    { "val": "Date", "col": "Date" },
    { "val": "Time", "col": "Time" },
    { "val": "ServerRequest", "col": "Server Request" },
    { "val": "ServerStatus", "col": "Server Status" }];


function PrintData() {
    var pdfData = [fileDataSpecific]
    var pdfCol = displayedColumns
    //var doc = new jsPDF()
    var col = [], row = []
    pdfCol.forEach(element => {
      col.push(element.col)
    });    
    pdfData.forEach(element => {
      var tempArray = []
      pdfCol.forEach(element1 => {
        tempArray.push(element[element1.val])
      });
      row.push(tempArray)
      tempArray = []
    });
    console.log(row)
    //doc.autoTable(col, row);
    //doc.save('Test.pdf');
  }
  
  PrintData();

这篇关于如何解决:无法在字符串上创建属性“标头"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆