Vue3+TypeScript+Element表格打印功能

1、下载依赖 

npm install vue3-print-nb --save

2、在main.ts中引用 "vue3-print-nb"   

import print from "vue3-print-nb";

3、编写一个公用的表格打印组件 Prints.vue

<template>

    <el-dialog v-model="addVisible" :title="titles+'打印预览'" width="60%" :before-close="handleClose" top="2vh">

        <div>

            <el-table :data="tableData" id="myTable1Prints" style="width: 100%" size="small" border>

                <el-table-column :prop="item.code" :label="item.name" :width="item.width" v-for="(item,index) in headers" :key="index" />

            </el-table>

        </div>

        <template #footer>

            <span class="dialog-footer">

                <el-button type="primary" size="small" v-print="printObj"> 确定打印 </el-button>

                <el-button  size="small" @click="close">取消</el-button>

            </span>

        </template>

    </el-dialog>

</template>



<script lang="ts" setup>

import { ref, reactive, defineProps, computed, defineEmits, onMounted, watch, toRaw } from 'vue'

import type { FormInstance, FormRules } from 'element-plus'

import { ElMessage } from 'element-plus'



const props = defineProps({

    addVisible: Boolean,

    info: Array

})

const titles=ref()

const printObj = ref(

    {

        id: "myTable1Prints",

        popTitle: "",

        extraCss: "https://cdn.bootcdn.net/ajax/libs/animate.css/4.1.1/animate.compat.css, https://cdn.bootcdn.net/ajax/libs/hover.css/2.3.1/css/hover-min.css",

        extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>',

        previewBeforeOpenCallback () {

            console.log('正在加载预览窗口')

        },

        previewOpenCallback () {

          console.log('已经加载完预览窗口')

        },

        beforeOpenCallback (vue:any) {

        //   vue.printLoading = true

          console.log('打开之前')

        },

        openCallback (vue:any) {

        //   vue.printLoading = false

          console.log('执行了打印')

        },

        closeCallback () {

          console.log('关闭了打印工具')

        },

        clickMounted (vue:any) {

          console.log('点击了打印按钮')

          emits("Closeprint")

          // vue.printSet.popTitle = vue.tableHead // 动态设置页眉

        }

    }

)

const tableData = ref<any>([])

const backData=ref()

const headers=ref<any>([])

//监听

watch(

    () => props.info,

    (newInfo, oldInfo) => {

        // console.log(newInfo)

        if (newInfo != undefined) {

            backData.value=JSON.parse(newInfo as any)

            tableData.value=backData.value.data

            titles.value=backData.value.title

            printObj.value.popTitle=backData.value.title

            headers.value=backData.value.header

        } else {

           

        }

    }

);

//defineEmits用于定义回调事件,里面是数组,可以定义多个事件

const emits = defineEmits(["Closeprint"])

const handleClose = (done: () => void) => {

    emits("Closeprint")

}



onMounted(() => {



})



const close = () => {

    emits("Closeprint")

}




</script>

<style lang="scss" scoped>



</style>

4、使用打印功能页面引用组件

<PrintsVue :addVisible="printVisible" :info="printinfo" @Closeprint="Closeprint"></PrintsVue>

import PrintsVue from './components/Prints.vue'

5、使用打印组件

const printVisible=ref(false)

const printinfo=ref()

const Closeprint = () =>{

    printVisible.value=false

}

const downloads = async (type:string) =>{

        //定义一个需要打印的数据变量

         let newArr=ref<any>([])

        //将表格中勾选的每行数据重新拼接成需要的数据格式

        let arr: any[] = multipleTableRef.value?.getSelectionRows()

        arr.forEach((element:any,key:any) => {

            newArr.value.push({

                index:key + 1,

                name:element.name,

            })

        });

    let dataObj={

        title:"打印标题",

        data:newArr.value,

        header:[//表格表头数据

            {name:'序号',code:'index',width:50},

            {name:'名称',code:'name'},

        ]

    }

    printinfo.value=JSON.stringify(dataObj)

    printVisible.value=true

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值