2016-08-01 22 views
0

我正尝试使用jsPdf自动表将动态数据打印到pdf文件中。当我这样做,我得到了某种错误的像jsPdf autotable中未捕获的类型错误

The headers should be an object or array, is: function (jspdf.plugin.autotable.js:10)

The data should be an object or array, is: function  (jspdf.plugin.autotable.js:10) 

TypeError: t.forEach is not a function  (angular.js:12314) 

这里是我的代码,

  var getColumns = function() { 
 
       \t  return [ 
 
       \t   {title: "ID", dataKey: "id"}, 
 
       \t   {title: "Name", dataKey: "first_name"}, 
 
       \t   {title: "Email", dataKey: "email"}, 
 
       \t   {title: "City", dataKey: "city"}, 
 
       \t   {title: "Country", dataKey: "country"}, 
 
       \t   {title: "Expenses", dataKey: "expenses"} 
 
       \t  ]; 
 
       \t }; 
 
       \t function getData(mainSteps) { 
 
       \t  mainSteps = mainSteps || 4; 
 
       \t  //var sentence = faker.lorem.words(12); 
 
       \t  var data = []; 
 
       \t  for (var j = 1; j <= rowCount; j++) { 
 
       \t   data.push({ 
 
       \t    id: j, 
 
       \t    first_name: this.getSteps(), 
 
       \t    email: this.getSteps(), 
 
       \t    country: this.getSteps(), 
 
       \t    city: this.getSteps()(), 
 
       \t    expenses: this.getSteps() 
 
       \t   // text: shuffleSentence(sentence), 
 
       \t    //text2: shuffleSentence(sentence) 
 
       \t   }); 
 
       \t  } 
 
       \t  return data; 
 
       \t } 
 
       var pdfsize='a4'; 
 
       var doc = new jsPDF('p', 'pt',pdfsize); 
 
     
 
       doc.autoTable(getColumns, getData, { 
 
       \t theme: 'grid', // 'striped', 'grid' or 'plain' 
 
       \t headerStyles: { 
 
         fillColor: [12, 234, 227], 
 
         textColor: [12, 1, 1] 
 
        }, 
 
        // margin: { top: 50, left: 20, right: 20, bottom: 0 }, 
 
       \t styles: { 
 
       \t  overflow: 'linebreak', 
 
       \t  columnWidth: 88 
 
       \t  }, 
 
       \t  beforePageContent: function(data) { 
 
       \t  \t 
 
       \t   doc.text("Process Name :"+mainData.name+" || "+"Description :"+mainData.description, 40, 30); 
 
       \t   
 
       \t  }, 
 
       \t  //startY: doc.autoTableEndPosY() + 20, 
 
        columnStyles: { 
 
         0: {columnWidth: 200} 
 
        } 
 
        }); 
 
       //startY: doc.autoTableEndPosY() + 20 
 
        doc.save(mainData.name+".pdf"); 
 
      } 
 
     };
注:如果错误是在我的代码的意思是我能找到解决方案,但它说错误在(jspdf.plugin.autotable.js:10)和(angular.js:12314),所以我在这里感到困惑。有人能澄清我吗?

回答

2

由于错误说明输入数据必须是数组或对象。在你的情况下,仅仅意味着调用函数而不是将它们发送到可自动键入。替换此

doc.autoTable(getColumns, getData, {...});

doc.autoTable(getColumns(), getData(), {...});

+1

是的,你是正确的。这是我的“粗心大意”的错误:( –

+0

,嘿嗨..我有一个小小的怀疑,我正在尝试应用自定义宽度列,所以我按照你的指示在你的github网站。但应用后,似乎我没有我的代码 - > {title:“Category”,dataKey:“cate”,width:20}, –

+0

或者有没有其他方法可以做到这一点? –