如何解决:无法在字符串上创建属性“标头”

时间:2019-05-03 08:26:21

标签: jspdf jspdf-autotable

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

数据:

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');
  }

错误:

enter image description here

请有人帮助!谢谢。

1 个答案:

答案 0 :(得分:0)

您需要将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();