问题
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.
Data:
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" }];
My function:
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');
}
Error:
Please someone help! Thanks.
回答1:
You need treat your var pdfData = this.fileDataSpecific
as array 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();
来源:https://stackoverflow.com/questions/55965954/how-to-fix-cannot-create-property-header-on-string