2016-08-01 23 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

或者有沒有其他方法可以做到這一點? –