Encountering an error with Vue-tables-2 when trying to generate a PDF using pdfmake: "Malformed table row" issue

Almost ready to download a .pdf file containing multiple selected rows from vue-tables-2 datatables.

In my code, I am pushing the checkedRows from this table.body which includes both the header columns and the content of the checkedRows, which consists of a checkbox input and 'sku' string. Here is a snippet of my code:

<template>
    <el-container>
        <side-nav></side-nav>
        <el-main>
            <v-server-table url="/getListings" :data="tableData" :columns="columns" :options="options">
                <input slot="selected" slot-scope="props" type="checkbox" :checked="props.row.selected" v-model="checkedRows" :value="props.row">
                <button slot="afterFilter" type="button" class="btn btn-primary btn-pdf" @click="createPDF">Create Purchase Order</button>
            </v-server-table>
        </el-main>
    </el-container>
</template>


<script>

import {ServerTable, Event} from 'vue-tables-2';    
Vue.use(ServerTable, {}, false, 'bootstrap4');

var pdfMake = require('pdfmake/build/pdfmake.js');
    var pdfFonts = require('pdfmake/build/vfs_fonts.js');
    pdfMake.vfs = pdfFonts.pdfMake.vfs;

export default {
    data() {
        return {
            toggle:[],
            tableData: [],
            checkedRows: [],
            columns: [
                'selected',
                'sku',
            ],     
            options: {

            }
        }
    },

    methods: {

        createPDF() {
            var docDefinition = {
            content: [
                {
                    table: {
                        headerRows: 1,
                        widths: [ 'auto', 'auto' ],

                        body: [

                        ]
                    }
                }
            ]
            };
            docDefinition.content[0].table.body.push(this.columns);
            for(var i=0;i<this.checkedRows.length;i++){
                docDefinition.content[0].table.body.push(Object.values(this.checkedRows[i]));  
            }
            pdfMake.createPdf(docDefinition).download('PO.pdf');
        }            
    }
}

</script>

The issue arises with the mismatch between the 2 header columns 'selected' and 'sku' and the row data column {'sku#'}, resulting in a 'malformed table row error' as well as a 'cell is undefined' error. Here are screenshots of these errors:

https://i.sstatic.net/cHmmP.png

https://i.sstatic.net/XfCHr.png

https://i.sstatic.net/47f7X.png

I actually do not need the checkbox column 'selected' in the downloaded .pdf, but I am unsure how to include only necessary header columns like 'sku' while excluding the 'selected' column.

Answer №1

If you find yourself in this situation, the solution you require is as follows:

docDefinition.content[0].table.body.push(["product_code"]);
        for(var i=0;i<this.checkedRows.length;i++){

      docDefinition.content[0].table.body.push([this.checkedRows[i]["product_code"]]);  
        }

However, for a more general approach, consider the following:

docDefinition.content[0].table.body.push(["column1","column2",..,"columnN"]);
        for(var i=0;i<this.checkedRows.length;i++){

      docDefinition.content[0].table.body.push([this.checkedRows[i]["column1"],this.checkedRows[i]["column2"],...,this.checkedRows[i]["columnN"]]);  
        }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Wiki experiencing issues with NodeJS HttpGet functionality

Goal Retrieve the HTML content of a Wiki Page. Introduction In an attempt to fetch the HTML of a Wiki page () for data parsing purposes, I am utilizing NodeJS and its HTTP Request methods. Code Snippet Below is the simple code snippet that accesses th ...

Even though the onSubmit attribute is set to false in the HTML form, it still submits

I've been struggling with a form that just won't stop submitting, no matter what I do. I have checked similar questions on SO, but none of the solutions seem to work for me. It's frustrating because it's such a simple task. The reason w ...

Guide on utilizing the disable feature in SortableJS for a Vue project

I have successfully implemented the draggable effect on my el table using element-ui-el-table-draggable and it's working perfectly. Now, I am looking to incorporate the disable option from SortableJS, but I'm unsure how to integrate these two fu ...

Adding color dynamically to text within ion-card based on a regex pattern

My goal is to enhance the appearance of certain text elements by wrapping them in a span tag whenever a # or a @ symbol is detected, creating the look of usernames and hashtags on Twitter. Below is the code I am currently using: TS FILE: ngOnInit(): void ...

How can I set a condition to open a specific page in ReactJs?

Currently, I am in the process of developing a website and have successfully implemented a profile page. This profile page consists of three buttons - Settings, Favourites, and Posts. To enhance user experience, I decided to structure it as a multi-step fo ...

The issue of onClick failing to function when paired with the addEventListener for the

Looking into a react component for a profile button that opens a menu with three options: My Profile, Settings, and Logout. The issue seems to be with the onClick event on the a tags not working as expected (the console.log is not being printed). Interes ...

Comprehending the intricacies of routing within AngularJS

Question: I've been looking into this issue, but there seems to be conflicting answers. I created a simple example in Plunker to understand how routers work in AngularJS, but I'm having trouble getting it to function properly... Below is my inde ...

Using the for loop to asynchronously fetch data and access the finished result variable

Exploring the world of asynchronous JavaScript, I find myself pondering a question: how can I ensure that my code only starts working on a created array once all queries are completed? My current approach involves fetching pages in a for loop. Here's ...

Charting with multiple series

I am exploring a unique approach to creating a timeline chart. I am seeking advice on the best way to implement this in the world of JavaScript. My challenge is to create interactive milestones with descriptive text displayed on the Y axis, while displayi ...

Multiple individual image uploads via ajax on a single webpage

Currently, I am facing an issue with my ajax upload script. It only allows for one image upload at a time which is causing problems for me as I am working on a shop CMS that requires multiple image uploads for each product. Each product needs a thumbnail i ...

The Material-UI Select component does not reflect changes after an onChange event

I've encountered this issue all over the internet, but no explanation seems to resolve it for me. Using Material-UI Select and traditional setState(...) in React (not using hooks). The core of my component is as follows: class MyComponent extends Co ...

Performing various calculations using a v-for loop to determine multiple totals

I have a unique scenario where I am using nested v-for loops in Vue to display information about users and their accumulated leave. Here is a simplified version of what I am trying to achieve: v-for user in users //Display user's name v-for ...

AngularJS Module Instantiation Error

My angularjs module is not loading correctly and I'm struggling to figure out why. Despite reading numerous tutorials, I can't seem to get it to work. [17:22:36.338] Error: [$injector:modulerr] Failed to instantiate module wc2013mongoMod due t ...

What is the process for setting the first option in a select as the default in Vue3?

I'm having trouble setting the first option as selected when populating a select element in Vue 3. Despite reading this question, none of the solutions seem to work for me. Below is my code: JS: const newOfferApp = createApp({ data() { r ...

Updating the state of a nested array using React Hooks

After spending some time working with React Hooks, my main struggle has been dealing with arrays. Currently, I am developing a registration form for teams. Each team consists of a list of players (an array of strings). The goal is to allow users to add t ...

Switching Between Background Images in Angular

Looking to change the background-image of an element upon clicking it. Currently, this is what's in place: <div class="item-center text-center" toggle-class="active cable"> <div class="quiz-background"></div> <p class="size ...

The most effective way to code overlapping html buttons

Can you help me figure out how to make clickable areas on my HTML5 web page without them overlapping? Here's what I'm trying to do: I want the red, blue, and green areas to be clickable links, but not overlap. For example, when clicking on the f ...

Remove data from Firebase that is older than two hours

I need to implement a solution to automatically delete data that is older than two hours. Currently, I am iterating through all the data on the client-side and deleting outdated entries. However, this approach triggers the db.on('value') function ...

Show occurrences of an array categorized by date using JSON format

I'm interested in analyzing a JSON array to find the occurrences of a specific item by date. Let me demonstrate with the following JSON example: "data": [ { "tags": [ "foo", "bar", "hello", "world", " ...

The text/font-weight of the header button is mysteriously shifting without warning

While creating a header for my webpage, I encountered an issue where the text family was changing when the dropdown menu was launched in Safari 8. Curiously, this behavior did not occur when using Chrome to launch the jQuery function. Even after attempting ...