Uncaught TypeError в jsPdf autotable

0

Я пытаюсь распечатать динамические данные в PDF файл с помощью jsPdf Auto-таблицы. Когда я делаю это, я получаю какую-то ошибку, как

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 () {
    return [
        { title: "ID", dataKey: "id" },
        { title: "Name", dataKey: "first_name" },
        { title: "Email", dataKey: "email" },
        { title: "City", dataKey: "city" },
        { title: "Country", dataKey: "country" },
        { title: "Expenses", dataKey: "expenses" }
    ];
};

function getData(mainSteps) {
    mainSteps = mainSteps || 4;
    //var sentence = faker.lorem.words(12);
    var data = [];
    for (var j = 1; j <= rowCount; j++) {
        data.push({
            id: j,
            first_name: this.getSteps(),
            email: this.getSteps(),
            country: this.getSteps(),
            city: this.getSteps()(),
            expenses: this.getSteps()
            // text: shuffleSentence(sentence),
            //text2: shuffleSentence(sentence)
        });
    }
    return data;
}

var pdfsize = 'a4';
var doc = new jsPDF('p', 'pt', pdfsize);

doc.autoTable(getColumns, getData, {
    theme: 'grid', // 'striped', 'grid' or 'plain'
    headerStyles: {
        fillColor: [12, 234, 227],
        textColor: [12, 1, 1]
    },
    // margin: { top: 50, left: 20, right: 20, bottom: 0 },
    styles: {
        overflow: 'linebreak',
        columnWidth: 88
    },
    beforePageContent: function (data) {

        doc.text("Process Name :" + mainData.name + "  ||   " + "Description :" + mainData.description, 40, 30);

    },
    //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), поэтому я запутался здесь. Может кто-нибудь уточнить меня, пожалуйста.

Теги:
jspdf
jspdf-autotable

1 ответ

3
Лучший ответ

Поскольку ошибки объясняют, что входные данные должны быть массивами или объектами. В вашем случае это просто означает вызов функций вместо того, чтобы отправлять их в autotable. Заменить это

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

с

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

  • 1
    Да вы правы . Это была моя "неосторожная" ошибка :(
  • 0
    Эй, привет .. У меня есть одно небольшое сомнение. Я пытаюсь применить пользовательский столбец ширины. Так что я следовал вашим инструкциям на вашем сайте github. Но после подачи заявления кажется, что я не работал на меня. Можете ли вы помочь мне, пожалуйста. Вот мой код -> {title: "Category", dataKey: "cate", width: 20},
Показать ещё 6 комментариев

Ещё вопросы

Сообщество Overcoder
Наверх
Меню