Vue Axios推送阵列未实时更新

时间:2018-10-07 10:23:06

标签: javascript vue.js axios jspdf-autotable

当我使用jspdf autotable生成表格pdf时遇到问题 我正在使用axios来获取动态列表,问题是当我单击按钮预览时没有结果

单击预览按钮 enter image description here

但是当我再次单击预览时,就会有结果

再点击一次预览按钮 enter image description here

这是我的代码

HTML

<template>
    <div id="report" class="container">
        <div class="row justify-content-center">
            <div class="col-md-12">
                <div class="card card-body">
                    <div class="form-group">
                        <v-flex xs12>
                        <v-radio-group v-model="document_category" row>
                        <v-radio label="Draft" value="DRAFT"></v-radio>
                        <v-radio label="Original" value="ORIGINAL"></v-radio>
                        </v-radio-group>
                        </v-flex>
                        <v-flex xs12>
                            <v-text-field v-model="profile_id" prepend-icon="text_fields" placeholder="Enter Title" label="Title" @keyup.enter="Report('preview')"></v-text-field>
                        </v-flex>
                        <v-flex xs12>
                        <v-text-field v-model="profile_name" prepend-icon="sort" placeholder="Enter Content" label="Content" @keyup.enter="Report('preview')"></v-text-field>
                        </v-flex>
                    </div>
                    <v-btn color="success" flat @click="Report('preview')">Preview</v-btn>
                    <v-btn color="error" flat @click="Report('newwindow')">Preview On New Window</v-btn>
                    <v-btn color="primary" flat @click="Report('print')">Preview and Print</v-btn><br><br>
                    <app-dialogloading :visible="dialog_loading" />
                    <iframe width="100%" height="500px" :src="urlpdf" v-if="urlpdf"></iframe>
                </div>
            </div>
        </div>
        <v-dialog v-model="dialog_preview_print" fullscreen hide-overlay transition="dialog-bottom-transition">
        <v-card>
            <v-toolbar flat dark color="primary">
                <v-btn icon dark @click.stop="dialog_preview_print = false">
                <v-icon>arrow_back</v-icon>
                </v-btn>
                <v-toolbar-title>Preview</v-toolbar-title>
                <v-spacer></v-spacer>
            </v-toolbar>
                <iframe width="100%" height="600px" :src="urlpdf" v-if="urlpdf"></iframe>
        </v-card>
        </v-dialog>
    </div>
</template>

JS

import jsPDF from 'jspdf'
require('jspdf-autotable')
export default {
    name: 'PN001',
    props: {
        referenceno: String
    },
    data: () => ({
        document_category: 'DRAFT',
        profile_id: '',
        profile_name: '',
        urlpdf: '',
        dialog_loading: false,
        dialog_preview_print: false,
        rows: []
    }),
    created() {
        this.profile_id = this.$props.referenceno
        if (this.$route.query.download === 'Y') {
            this.Report('download')
        }
    },
    methods: {
        Report (flag) {
            if (flag !== '') {
                let pdfName = 'riva'
                let doc = new jsPDF()
                doc.setFontType('bold')
                doc.setFontSize(12)
                let pageHeight = doc.internal.pageSize.height || doc.internal.pageSize.getHeight()
                let pageWidth = doc.internal.pageSize.width || doc.internal.pageSize.getWidth()
                doc.setFillColor(255,255,255)
                doc.rect(172,4,30,8,'FD')
                doc.text(this.document_category,180,10)
                doc.text(this.profile_id, pageWidth / 2, pageHeight  - 285, 'center')
                doc.setFontType('')
                doc.setFontSize(10)
                doc.text(this.profile_name, 10,20, {maxWidth: 190, align: 'justify'})
                let columns = [
                    {title: 'ID', dataKey: 'profile_id'},
                    {title: 'Name', dataKey: 'profile_name'},
                    {title: 'Actived', dataKey: 'profile_actived'}
                ]
                this.TableList()
                doc.autoTable(columns, this.rows)
            if (flag === 'download') {
                doc.save(pdfName + '.pdf')
            } else if (flag === 'preview') {
                this.dialog_loading = true
                this.urlpdf = doc.output('bloburl')+'#toolbar=0'
                this.dialog_loading = false
            }  else if (flag === 'newwindow') {
                this.dialog_loading = true
                this.dialog_preview_print = true
                this.urlpdf = doc.output('bloburl')+'#toolbar=0'
                this.dialog_loading = false
            } else if (flag === 'print') {
                this.dialog_loading = true
                doc.autoPrint()
                this.urlpdf = doc.output('bloburl')
                this.dialog_loading = false
            } 
        }
        },
        TableList (){
            let formData = new FormData()
            formData.append('profile_id', this.profile_id)
            formData.append('profile_name', this.profile_name)
            this.$axios.post(this.$functions.SafeURL('apiListProfile', ``), formData, {
            headers: {
                'Content-Type': 'multipart/form-data'
                }
            })
            .then(response => {
            this.rows = []
            for (let i = 0;i < response.data.length;i++) {
                this.rows.push({profile_id: response.data[i].profile_id,profile_name: response.data[i].profile_name,profile_actived: response.data[i].profile_actived})
            }
            })
            .catch(e => {
                console.log(e)
            })
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果您在模板中添加rows,Vue会检测到{{ rows }}的更改。

但是所做的更改不会自动导致urlpdf值的更改。

您可以在rows上添加watch来运行更新pdfurl的代码,但是在这种情况下,我将在处理响应并且行包含以下内容后调用Report()方法结果,像这样:

.then(response => {
  this.rows = []
  for (let i = 0;i < response.data.length;i++) {
    this.rows.push({profile_id: response.data[i].profile_id,profile_name: response.data[i].profile_name,profile_actived: response.data[i].profile_actived})
  }
  this.Report('preview') // <- here
})